Jump to content

Comparing fields and changing table cell colour based in comparison


karl_009

Recommended Posts

Hello,

 

I have a table that has information from a MySQL DB, there are two fields current stock and minimum stock.

 

I would like some help with making the current stock table colour change depending want the minimum stock level is.

 

So if the minimum stock is 2 and the current stock is 3 then the colour is green and in the current stock is 2 or 1 then the colour is red.

 

Here is the code that I am currently using;

 

 
<table bgcolor="#0066FF" border="0" width="75%" cellspacing="1" cellpadding="2">
  
<?php
  
// Query for displaying data from computers table
$query = "SELECT * FROM inv_consumable ORDER BY stockloc";

$result = @mysql_query ($query, $connection);

// FOR loop, loops though each line of data to be displayed
for ($i = 0; $i < @mysql_num_rows ($result); $i++)
{
	$consid = mysql_result ($result, $i, "consid");
	$consname = mysql_result ($result, $i,"consname");
	$conserial = mysql_result ($result, $i,"conserial");
	$currstock = mysql_result ($result, $i,"currstock");
	$minstock = mysql_result ($result, $i,"minstock");
	$stockloc = mysql_result ($result, $i,"stockloc");

	$rows = ($result);

	// Alternates the colour of the rows for the table
	if ($i % 2)
	{
		$bg_color="#EEEEEE";
	}
	else
	{
		$bg_color="#AAAAAA";
	}

	// Displays the table
	echo '
		<tr>
			<td width="15%" bgcolor="'.$bg_color.'">
				<font face="arial" size="2">
					<b>Consumable Name:</b> <a href="consumable_read.php?consid='.$consid.'">'.$consname.'</a>
					</font>
				<td>
				<td width="20%" valign="top" bgcolor="'.$bg_color.'">
					<font face="arial" size="2">
						<b> Serial Number: </b> '.$conserial.'
					</font>
				</td>
				<td>
				<td width="20%" valign="top" bgcolor="'.$bg_color.'">
					<font face="arial" size="2">
						<b> Current Stock: </b> '.$currstock.'
					</font>
				</td>	
				<td>
				<td width="15%" valign="top" bgcolor="'.$bg_color.'">
					<font face="arial" size="2">
						<b> Minimum Stock: </b> '.$minstock.'
					</font>
				</td>
				<td>
				<td width="20%" valign="top" bgcolor="'.$bg_color.'">
					<font face="arial" size="2">
						<b> Stock Location: </b> '.$stockloc.'
					</font>
				</td>
				<td>
				<td width="5%" valign="top" align="center" bgcolor="'.$bg_color.'"><a href="consumable_edit.php?consid='.$consid.'"><img src="buttons/update.png" title="Update"></a>
					<font face="arial" size="2">
					</font>
				</td>
				<td>
				<td width="8%" valign="top" align="center" bgcolor="'.$bg_color.'"><a href="consumable_del_ac.php?consid='.$consid.'"><img src="buttons/delete.png" title="Delete"></a>
					<font face="arial" size="2">
					</font>
				</td>
			</tr>
		';
}
?>
</table>

 

Thanks for any help

Karl

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.