I am trying to create a webpage that does a simple query of a table and displays the table to a page, but with one complex twist that has got me confused. Here are the column names and one example record from the table separated by commas:
R_ID , B_ID , R_No , RoomName , showers , eyewashPlumbed , EyewashBottles
1 , 609 , 609 , Hazardous Waste Shed , 1 , 1 , 1
I need to print each row of this table, but also print multiple rows if either showers, eyewashPlumbed, or EyewashBottles is greater than 0. For example, I would print this row three times. If showers was 0 I would only print it two times (one for eyewashPlumbed, one for EyewashBottles, and 0 for showers). If showers was 2 I would print it 4 times, etc.
The code I’m using to print is as follows:
<?php while ($row = mysql_fetch_array($result, MYSQL_ASSOC)): ?>
<tr>
<td><?php print $row["B_ID"];?></td>
<td><?php print $row["R_No"];?></td>
<td><?php print $row["RoomName"];?></td>
<td><?php print $row["Showers"];?></td>
<td><?php print $row["eyewashPlumbed"];?></td>
<td><?php print $row["EyewashBottles"];?></td>
</tr>
<?php endwhile; ?>
The problem is that I don’t know how to interrupt the while loop in order to print the same row multiple times. It goes onto the next row as it pulls from the mysql_fetch_array.
You really should switch to PDO / mysqli, but with your current code it would be something like (for
showersas in your example):You probably want to change
$row["shower_no"]to something like$i + 1as that column does not appear in your database.