How to solve this simple PHP forloop issue?

Posted by Londonbroil Wellington on Stack Overflow See other posts from Stack Overflow or by Londonbroil Wellington
Published on 2011-01-02T21:47:07Z Indexed on 2011/01/02 21:53 UTC
Read the original article Hit count: 286

Filed under:

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?

© Stack Overflow or respective owner

Related posts about php