PHP:将数组输出到表中

For my PHP Project, I need to print a sorted array into a table. We can not use premade sorting functions to do this, and were instructed to make our own sorting function. The sorting works, but it will not go into a html table format the way we need it.

    <?php
    $fileInput = "guestBook.txt";
    $fileInputInData = file_get_contents($fileInput);
    $array=explode("
", $fileInputInData);

    for($j = 0; $j < count($array); $j ++) 
    {
      for($i = 0; $i < count($array)-1; $i ++)
      {

        if($array[$i] > $array[$i+1])
        {
        $temp = $array[$i+1];
        $array[$i+1]=$array[$i];
        $array[$i]=$temp;

        }       
      }
     }
     echo "<th>Firstname</th><th>Lastname</th><th>Email Address</th>";
     $arrayExplode = explode("
", $array);
     $k = 0;
     foreach($array as $arrayOut)
     {
     $arrayExplodeTwo = explode(" ", $arrayExplode[$k]);
     $firstName =  $arrayExplodeTwo[0];
     $lastName = $arrayExplodeTwo[1];
     $email = $arrayExplodeTwo[2];
     echo '<tr><td>'; echo $firstName; echo '</td></tr>';echo '<tr><td>'; 
     echo $lastName; echo '</td></tr>';
     echo '<tr><td>'; echo $email; echo '</td></tr>';
     $k++;  
     }


     ?>

The code outputs like so:

           Firstname Lastname Email

The code doesn't want to print data into the table properly. Taking out the specific breakup of the explode in the foreach loop outputs it into a table. However, doing so does not break them up into the spaces of the table, just a single box per row, leaving the other 2 boxes empty. It prints the data into the table all in the Firstname column, with the data per row. Putting the data to format it into specific lines results in it printing nothing into the columns, just an empty table.

Here is how my code is outputting to the web browser, before adding the explode function into the foreach loop near the bottom of the code.

       Firstname                            Lastname    Email Address
       Anon emous anon@email.com
       Matthew rando rando@gmail.com has signed in.
       Matthew rando rando@gmail.com has signed in.
       Matthew rando rando@gmail.com has signed in.
       Person Anon Anon@emailaddr.com has signed in.
       Person AnonyMouse AnonyMouse@emailaddr.com has signed in.
       Person Name randomaddr@example.com has signed in.
       Test Person thispersonisatest@fake.com has signed in.
       Test PersonTwo thispersonisatestTwo@fake.com has signed in.
       random name randomname@example.com
       test personand testPersonAnd@testemail.com has signed in.

After adding the explode function in the foreach loop, results in it printing like so:

       Firstname    Lastname    Email Address

with no data in the table period, outputted as a blank table.

Thank you in advance for any help.

I made some assumptions about your guestBook.txt file and got the following bit of code working.


<?php

# Read and parse the file
$fileInput = "guestBook.txt";
$fileInputInData = file_get_contents($fileInput);
$array=explode("
", $fileInputInData);

# Sort the data
for($j = 0; $j < count($array); $j ++)
{
   for($i = 0; $i < count($array)-1; $i ++)
   {
      if($array[$i] > $array[$i+1])
      {
         $temp = $array[$i+1];
         $array[$i+1]=$array[$i];
         $array[$i]=$temp;
      }
   }
}

# Check the sort results
#var_dump($array);

# Echo the table header
echo "<tr><th>Firstname</th><th>Lastname</th><th>Email Address</th></tr>";

# Loop through each element of the sorted array
foreach($array as $arrayOut)
{
   # Explode the current array element by spaces
   $arrayExplode = explode(" ", $arrayOut);

   # Assign the data
   $firstName =  $arrayExplode[0];
   $lastName = $arrayExplode[1];
   $email = $arrayExplode[2];

   # Echo out the results
   echo '<tr>';
   echo ' <td>'; echo $firstName; echo '</td>';
   echo ' <td>'; echo $lastName; echo '</td>';
   echo ' <td>'; echo $email; echo '</td>';
   echo '</tr>';
}  
?>

Some comments regarding your original code:

  1. In your code: $array=explode(" ", $fileInputInData); and $arrayExplode = explode(" ", $array);

were doing the same thing twice and throwing an error.

  1. As @PatrickSJ mentioned, your HTML output was writing firstName, lastName, and email on 3 separate rows instead of on one row.

    <tr><td>$firstName</td></tr>
    <tr><td>$lastName</td></tr>
    <tr><td>$email</td></tr>
    

vs

    <tr>
     <td>$firstName</td>
     <td>$lastName</td>
     <td>$email</td>
    <tr>

Hope this help!

Pretty much the same solution as suggested by @dchao5. Was working on it right as he submitted. The major difference is using php's built in templating syntax so you are not mixing html code inside of business logic. It's always cleaner to do your processing first, generate the elements that the page needs to render, then use the templating syntax to render them in page. Makes maintenance much more clear.

<?php

    // all the same did not touch
    $fileInput = "guestBook.txt";
    $fileInputInData = file_get_contents($fileInput);
    $array=explode("
", $fileInputInData);

    for($j = 0; $j < count($array); $j ++) 
    {
      for($i = 0; $i < count($array)-1; $i ++)
      {

        if($array[$i] > $array[$i+1])
        {
        $temp = $array[$i+1];
        $array[$i+1]=$array[$i];
        $array[$i]=$temp;

        }       
      }
     }
    //--------------------------------------


     $formatted = array();
     foreach($array as $row)
     {

        if($row != ''){

            $columns = explode(" ", $row);

            $formatted[] = array(
                'first'     => $columns[0],
                'last'      => $columns[1],
                'email'     => $columns[2]
            );

        }

     }
?>

<table>
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($formatted as $row): ?>
        <tr>
            <td><?php echo $row['first']; ?></td>
            <td><?php echo $row['last']; ?></td>
            <td><?php echo $row['email']; ?>td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>