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("\n", $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("\n", $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 [email protected]
Matthew rando [email protected] has signed in.
Matthew rando [email protected] has signed in.
Matthew rando [email protected] has signed in.
Person Anon [email protected] has signed in.
Person AnonyMouse [email protected] has signed in.
Person Name [email protected] has signed in.
Test Person [email protected] has signed in.
Test PersonTwo [email protected] has signed in.
random name [email protected]
test personand [email protected] 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.
'<tr><td>'; echo $firstName; echo '</td></tr>'. That is 1 cell per row instead of 3 per row. It should be<tr><td></td><td></td><td></td></tr>. Second, why is itforeach($array as $arrayOut)instead offoreach($arrayExplode as $arrayOut)Third, what is the result ofvar_dump($arrayExplodeTwo)?