Home > OS >  Taking certain parts of a line and putting chosen elements into a table, it's only reading the
Taking certain parts of a line and putting chosen elements into a table, it's only reading the

Time:03-24

Let's say I have multiple lines in a text file where every line looks like this:

2, Ann, Johnsson, [email protected], University of smth, street 1, state, country, 49008, 12202202220

I am reading the data from said textfile and trying to put it into a table. I have taken the data and put it into an array and then to separate the data I don't want, I echo all the elements that I wanna put into the table.

           <table >
             <tr>
               <th>Name</th>
               <th>Email</th>
               <th>University</th>
               <th>City</th>
             </tr>
             <?php  

               $file = fopen("customers.txt", "r"); {
               $line = fgets($file);
               $customers = explode(",", $line);
               list($studentid, $firstname, $lastname, $email, $university, $adress, $city, $country, $phonenumber) = $customers;
                  echo $firstname;
                  echo $lastname;
                  echo $email;
                  echo $university;
                  echo $city;
               

               foreach($customers as $customer) {
                  echo '<tr>';
                  echo '<td>'.'<a>' . $firstname, " ", $lastname . '</a>'.'</td>';
                  echo '<td>' . $email . '</td>';
                  echo '<td>' . $university. '</td>';
                  echo '<td>' . $city . '</td>';
                  echo '</tr>    ';    
               } 
            } 
            ?>
         </table>
      </div>

Can someone explain what I am missing, my table only outputs the first line of the text file.

CodePudding user response:

fgets causing this error.just change it to file_get_contents. It will return all of the content of the filem

CodePudding user response:

When defining $customers it looks like you're splitting that single line into it's relevant "cells" or "fields". Then you're looping over the cells instead of the lines.

If you're instead trying to loop over each line, try something like this:

<?php

$file = fopen("customers.txt", "r");
while ($line = fgets($file)) {
    $customer = explode(",", $line);
    list($studentid, $firstname, $lastname, $email, $university, $adress, $city, $country, $phonenumber) = $customer;
    ?>
    <tr>
        <td><a><?php echo $firstname . " " . $lastname; ?></a></td>
        <td><?php echo $email; ?></td>
        <td><?php echo $university; ?></td>
        <td><?php echo $city; ?></td>
    </tr>
    <?php
}
?>
  • Related