Table formatting

Solved/Closed
BrianGreen Posts 1005 Registration date Saturday January 17, 2015 Status Moderator Last seen September 30, 2021 - Sep 30, 2021 at 07:50 AM
HelpiOS Posts 14521 Registration date Friday October 30, 2015 Status Moderator Last seen November 22, 2024 - Sep 30, 2021 at 11:00 AM
Hi,

I have a problem that I cant find a solution to. Could one of you kind people help with this issue please?

Im trying to create a "table" in PHP to display a 3 column 6 row bit of data on my web page. the first, third, and fifth rows are a "heading" to describe what the data immediately below it is. Here is the code I have so far ...


<?php

$result = mysqli_query($conn, "SELECT * FROM holiday_admin_bank_holiday
WHERE year=2021");

echo "<table id='open'>";
echo "<tr class='header'>";
echo "<th>Year</td>";
echo "<th>New Year</td>";
echo "<th>Easter</td></tr>";
echo "<th>Good Friday</td>";
echo "<th>May Day</td>";
echo "<th>Whitsun</td></tr>";
echo "<th>August</td>";
echo "<th>Christmas</td>";
echo "<th>Boxing Day</td>";
echo "</tr>";

while($row = mysqli_fetch_array($result)) {

echo "<tr>";
echo "<td>".$row['year']."</td>";
echo "<td>".$row['new_year']."</td>";
echo "<td>".$row['easter']."</td></tr>";
echo "<td>".$row['good_friday']."</td>";
echo "<td>".$row['may_day']."</td>";
echo "<td>".$row['whitsun']."</td></tr>";
echo "<td>".$row['august']."</td>";
echo "<td>".$row['christmas_day']."</td>";
echo "<td>".$row['boxing_day']."</td>";

}
?>



This gives a 3 column 6 row table with the headings on the first 3 rows and the data in the last 3 rows. Can anyone suggest a way of getting 1 row of heading, then 1 row of data, then 1 row of the next heading followed by the next row of data, and then the final 2 rows in the same way please?
Related:

1 response

BrianGreen Posts 1005 Registration date Saturday January 17, 2015 Status Moderator Last seen September 30, 2021 149
Updated on Sep 30, 2021 at 08:38 AM
Wow - that was easy ... just changed the code a tiny bit to ...

<?php 

$result = mysqli_query($conn, "SELECT * FROM holiday_admin_bank_holiday
WHERE year=2021");

while($row = mysqli_fetch_array($result)) {

echo "<table id='open'>";
echo "<tr class='header'>";
echo "<th>Year</td>";
echo "<th>New Year</td>";
echo "<th>Easter</td></tr>";

echo "<td>".$row['year']."</td>";
echo "<td>".$row['new_year']."</td>";
echo "<td>".$row['easter']."</td></tr>";

echo "<th>Good Friday</td>";
echo "<th>May Day</td>";
echo "<th>Whitsun</td></tr>";

echo "<td>".$row['good_friday']."</td>";
echo "<td>".$row['may_day']."</td>";
echo "<td>".$row['whitsun']."</td></tr>";

echo "<th>August</td>";
echo "<th>Christmas</td>";
echo "<th>Boxing Day</td></tr>";

echo "<td>".$row['august']."</td>";
echo "<td>".$row['christmas_day']."</td>";
echo "<td>".$row['boxing_day']."</td>";

}
?>


I hope this helps someone else :^)

1
HelpiOS Posts 14521 Registration date Friday October 30, 2015 Status Moderator Last seen November 22, 2024 1,868
Sep 30, 2021 at 11:00 AM
Thanks for sharing your solution!
0