Table formatting

Solved
Posts
1010
Registration date
Saturday January 17, 2015
Status
Moderator
Last seen
September 30, 2021
-
Posts
11776
Registration date
Friday October 30, 2015
Status
Moderator
Last seen
January 24, 2022
-
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?

1 reply

Posts
1010
Registration date
Saturday January 17, 2015
Status
Moderator
Last seen
September 30, 2021
146
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 :^)

Posts
11776
Registration date
Friday October 30, 2015
Status
Moderator
Last seen
January 24, 2022
1,801
Thanks for sharing your solution!