PHP output into a table - help with layout

Associate
Joined
24 Sep 2005
Posts
209
I have a PHP code which pulls out the pc status field from a database, and displays a Green square if its working, Amber if its awaiting attention and Red if its faulty.

The code at the minute displays 1 square below another - in separate table rows it appears. Is there any way of changing the layout so it presents the images in rows of 5 columns.

i.e.

R R R R R
G G R R G
A A A A A

etc. (R = Red, A = Amber, G = Green images).

Thanks.

Code:
<table>
<tr><th>Colour</th></tr>

<?php
$workstations = @mysql_query($select . $from . $where);
if (!$workstations) {
  echo '</table>';
  exit('<p>Error retrieving workstation data from database!<br />'.
      'Error: ' . mysql_error() . '</p>');
}

while ($computer = mysql_fetch_array($workstations)) {
  echo "<tr valign='top'>\n";
  $status = htmlspecialchars($computer['status']);
  
  if ($status=="WORKING") {
  echo "<td align=center><img src='green.jpg'></td>";
  }
  if ($status=="ATTENTION") {
  echo "<td align=center><img src='amber.jpg'></td>";
  }
  if ($status=="FAULTY") {
  echo "<td align=center><img src='red.jpg'></td>";
  }
  
}
?>

</table>
 
Beansprout said:
Also, you can reduce those 3 ifs to none by naming the images after the states, or naming the states after the images :)


The code supplied by Dj Jestar (thanks!) is just right for displaying the images in rows, and columns (just as I wanted :) )

How do I go about implementing your idea BeanSprout?
 
Quick followup to my question - I'm having difficulty displaying a variables data within the table cell.

I've changed the image ($status.jpg) to the table cell background, and wish to display the data stored by $stationid over it.

The code at the mo is:

Code:
	if(in_array($status,$statuses)){
		$str .= '<td width="70" height="70" align="center" background="' . $status. '.jpg", valign="center"> $stationid </td>';

Obviously this just outputs $stationid in the table cell. How do I get it to ACTUALLY output $stationid ?

Cheers
 
Back
Top Bottom