Forum Moderators: coopster
<?php$filename = "data1.txt"; //here's the filename
$id = fopen($filename, "r"); //open the file
while ($data = fgetcsv($id, filesize($filename))) //start a loop
$table[] = $data; //put each line into its own entry in the $table array
fclose($id); //close file
echo "<table>\n";
foreach($table as $row)
{
echo "<tr>";
foreach($row as $data)
echo "<td style='border:1px solid #000;'>$data</td>";
echo "</tr>\n";
}
echo "</table>\n";
?>
Now say I want to get each of these & include them in a tables in certain places as I call them. What I need is to be able to get the data as varibles eg:$alias, $date & $data. Then for each row I want a new table. The CSV file layout is Alias¦Date¦Data
So say the first two rows where,
333¦1902¦qwerty
444¦1949¦ytrewq
I would want the info in tables like so,
This is a 333 It was made 1902 & here's the qwerty.
This is a 444 It was made 1949 & here's the ytrewq.
After that I'm going to want to be able to choose which row I start on and how many entries to get.
that's not too complicated. the point you need to know is, that $table and $row are both arrays. you can access each value in a row by using a number. the first field (in your example alias) can be accessed by number 0 (zero), the second is 1 (one)...
for example you can change the output at the end of your script to something like this, and i highlighted in bold where the numbers are placed:
echo "<table>\n";foreach($table as $row)
{
echo "<tr>";
echo '<td style="border:1px solid #000;">'This is a '.$row[[b]0[/b]].' It was made '.$row[[b]1[/b]].' & here's the '.$row[[b]2[/b]].'</td>';
echo "</tr>\n";
}echo "</table>\n";
another example:
echo "<table>\n";foreach($table as $row)
{
echo "<tr><th>Alias</th><th>Date</th></tr>\n";
echo "<tr>";
echo '<td>'.$row[[b]0[/b]].'</td>';
echo '<td>'.$row[[b]1[/b]].'</td>';
echo "</tr>\n";
echo "<tr>";
echo '<td colspan="2" style="border:1px solid #000;">'.$row[[b]2[/b]].'</td>';
echo "</tr>\n";
}echo "</table>\n";
There are examples on that page in the PHP manual for parsing HTML, and a few discussions on this forum that are relative.
HTML extraction [webmasterworld.com]
html parsing [webmasterworld.com]
Help with fread() - Limit size of file being read? [webmasterworld.com]
Extracting parts of an html page [webmasterworld.com]