PHP Loop Through CSV and Print Out Table
In this Post , will show you how to Loop through an external CSV file , save the information into an Array.
Finally Print out the Array into the browser.
//Styling <style> table, th, td { border: 1px solid black; } </style> <?php // Open the File $file = fopen("C:/xampp/htdocs/testFile.txt", "r"); $i=0; // as long as is not end of file continue loop through while(!feof($file)){ // get the file string by line $thisLine = fgets($file); // Explode the line when there is a ", " $personData[$i]= explode(",",$thisLine); $i++; } // close the File fclose($file); // Number of Rows $numRows = sizeof($personData); // Loop Through the Array and print out the Tabular Table echo'<table style="width:100%">'; echo'<tr>'; echo'<th>id</th>'; echo'<th>First Name</th>'; echo'<th>Last Name</th>'; echo'<th>Email</th>'; echo'<th>Register Date</th>'; echo'</tr>'; $i=0; while ($i<$numRows){ echo'<tr>'; echo'<td>'.$personData[$i][0].'</td>'; echo'<td>'.$personData[$i][1].'</td>'; echo'<td>'.$personData[$i][2].'</td>'; echo'<td>'.$personData[$i][3].'</td>'; echo'<td>'.$personData[$i][4].'</td>'; echo'</tr>'; $i++; } echo'</table>'; ?>
Leave a Reply