How to solve this simple PHP forloop issue?
- by Londonbroil Wellington
Here is the content of my flat file database:
Jacob | Little | 22 | Male | Web Developer *
Adam | Johnson | 45 | Male | President *
Here is my php code:
<?php
$fopen = fopen('db.txt', 'r');
if (!$fopen) { echo 'File not found'; }
$fread = fread($fopen, filesize('db.txt'));
$records = explode('|', $fread);
?>
<table border="1" width="100%">
<tr>
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Sex</th>
<th>Occupation</th>
</thead>
</tr>
<?php
$rows = explode('*', $fread);
for($i = 0; $i < count($rows) - 1; $i++)
{
echo '<tr>';
echo '<td>'.$records[0].'</td>';
echo '<td>'.$records[1].'</td>';
echo '<td>'.$records[2].'</td>';
echo '<td>'.$records[3].'</td>';
echo '<td>'.$records[4].'</td>';
echo '</tr>';
}
fclose($fopen);
?>
</table>
Problem is I am getting the output of the first record repeated twice instead of 2 records one for Jacob and one for Adam. How to fix this?