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>
 
What would each row and column represent - each column is an attribute and each row is a PC?

Also, you can reduce those 3 ifs to none by naming the images after the states, or naming the states after the images :)
 
Code:
echo "<table>\n<tr valign='top'>\n";
$counter = 0;
while ($computer = mysql_fetch_array($workstations)) {
  if (($counter % 5) == 0) {
    echo "</tr>\n<tr valign='top'>";
  }
  $counter++;
  $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>";
  }
  
}

echo "</tr>\n</table>";
 
Last edited:
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?
 
I'll modify DJ's code :)

Code:
echo "<table>\n<tr valign='top'>\n";
$statuses = array("WORKING","ATTENTION","FAULTY");
$counter = 0;
$str = "";
while ($computer = mysql_fetch_assoc($workstations)) {
	if (($counter % 5) == 0) {
		$str .= '</tr>\n<tr valign="top">';
	}
	$counter++;
	$status = htmlentities($computer['status']);

	if(in_array($status,$statuses)){
		$str .= '<td align="center"><img src="' . strtolower($status). '.jpg"></td>';
	} else {
		$str .= '<td align="center">-</td>';
	}

}

$str .= '</tr>\n</table>';
echo $str;
Spot the difference :D

I changed mysql_fetch_array to mysql_fetch_assoc since you're using the associative key, not the numeric key.

Then I added the $statuses array which is the allowed states - firstly so that only those three JPG filenames every get written out, and secondly to provide error handling in the form of a '-' should the status be something unexpected.

Then I changed htmlspecialchars() to htmlentities(), because htmlentities() changes everything necessary - htmlspecialchars() only does a few. Genereally htmlentities() is the recommended option of the two, I think.

Then, I changed all the 'echo "str";' statements to append to a string, $str. Using single quotes around a string is quicker and neater - you can put the double quotes around the values (though you can also use single quotes....but..but I like double quotes). The whole string is then echo'd at the end :)

Then I did that JPG thing I mentioned earlier. Basically it writes out the status as the name of the JPG file, all in lowercase to ease case problems (pain). You need to rename green/amber/red.jpg to each of the respective statuses. To me it makes more sense, because then you don't have to remember what each colour means (red = FAULTY, etc) - if you forget you can look at the filename.
 
Last edited:
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