foreach - PHP Loop to list all items in array, not just last -
i have bit of php code gets information csv file, stores in array, displays data in html table. code uses series of loops display both table headers (keys) , table contents (steps). problem having cannot work out how code loop through , list each array of information, @ moment shows last 1 (amount display specified $x variable in each loop.
if me more 1 single value using foreach loop in table appriciated, code follows:
<?php // deals potential mac line endings ini_set("auto_detect_line_endings", true); // define function read info .csv function readcsv($csvfile) { $file_handle = fopen($csvfile, 'r'); while (!feof($file_handle) ) { $line_of_text[] = fgetcsv($file_handle, 1024); } fclose($file_handle); return $line_of_text; } // locate .csv read $csvfile = '500.csv'; $csv = readcsv($csvfile); // uses first line of .csv keys $keys = $csv[0]; // loop set how many arrays print ($x=1; $x <=2; $x++) { $step = $csv[$x]; foreach ($step $k => $v) { $array[$keys[$k]] = $v; } echo '<pre>'; print_r($array); echo '</pre>'; } // use data arrays build html table print "<table>"; // header print "<tr>"; ($x=0; $x <=10; $x++) { print "<th>$keys[$x]</th>"; } print "</tr>"; //field data print "<tr>"; ($x=0; $x <=10; $x++) { print "<td>$step[$x]</td>"; } print "</tr>";
change $array[$keys[$k]] = $v;
$array[$x][$keys[$k]] = $v;
, use $array
instead of $step
in last for
.
and keep small:
print "<table>"; print "<tr><th>".implode("</th><th>",$keys)."</th></tr>"; foreach ($array $arr){ print "<tr><td>".implode("</td><td>",$arr)."</td></tr>"; } print "</table>";
Comments
Post a Comment