I have built a table from a MYSQL database using PHP. There are 4 columns that are checked whether they have yes in them and only displayed if so. The only problem is that when the first column repair has nothing in it, but a subsequent column has a yes, they display outputs the yes into the repair column instead of leaving a blank spot there and putting it under it’s correct column. The column header appears correctly, its just getting the data to appear in the correct column. Not sure how to go about this so any help appreciated.
//build table for results
echo "<table><tr>";
echo "<th>Part Number</th>";
echo "<th>Serial Number</th>";
echo "<th>Date</th>";
//check whether each element has data
//and only display is data present
if ($repair=="yes") echo "<th>Repair</th>";
if ($upgrade=="yes") echo "<th>Upgrade</th>";
if ($pm=="yes") echo "<th>PM</th>";
if ($nofault=="yes") echo "<th>No Fault</th></tr>";
// printing table rows
while($row = mysql_fetch_array($result))
{
$part=$row['part'];
$serial=$row['serial'];
$date=$row['date'];
$Repair=$row['repair'];
$Upgrade=$row['upgrade'];
$PM=$row['pm'];
$NoFault=$row['nofault'];
echo "<tr>";
echo "<td>$part</td>";
echo "<td>$serial</td>";
echo "<td>$date</td>";
if ($Repair=="yes") echo "<td>YES</td>";
if ($Upgrade=="yes") echo "<td>YES</td>";
if ($PM=="yes") echo "<td>YES</td>";
if ($NoFault=="yes") echo "<td>YES</td>";
echo "</tr>";
}
echo "</table>";
you should include empty cells for when the if evaluates to false. something like this:
You could also echo the value directly in the td in this case. Something like this: