Jump to content

Change bgcolor on variable change.


Recommended Posts

Hi Guys/Girls

I’m using a while loop to get data from a database, The databsa consists of a tank_name and tank_reading fields. What I want to do is to make the bgcolor of each tank_name te same color. So in other words when the tank_name change then the bgcolor must change but all the tank_names that are the same must have the same bgcolor.

In example: this comes out of the db via while loop


Tank 1 bgcolor yellow

Tank 2 bgcolor red

Tank 3 bgcolor blue

Tank 2 bgcolor red

Tank 2 bgcolor red

Tank 1 bgcolor yellow


$sql1 = "select a.*, b.plant_nr AS plant_name, d.plant_nr AS tank_name 
from `table_tankReading` AS a 
LEFT JOIN `db_plant` AS b ON b.id=a.plant_id 
LEFT JOIN `db_plant_inc` AS c ON (c.plant_id=a.plant_id AND c.plant_category='Diesel Tank Type')
LEFT JOIN `db_plant` AS d ON d.id=tank 
(a.tank_reading_mine_id='$mine_id' AND a.tank_reading_month='$month_string' AND a.tank_reading_year='$year') 
ORDER BY a.tank+0, a.date_tank_reading
$result1 = $mysqli->query($sql1);
while($myrow1 = $result1->fetch_assoc()){
$tank_name = $myrow1["tank_name"];
<tr bgcolor="$bg"><td>$tank_name


Thanks for the help


Link to comment
Share on other sites

Thanks for the reply cyber. An switch might work yes, the only problem I have is that I don’t know what $tank_name value will be. So in short what I’m looking for is a method to test the value of $tank_name if it is the same as in one the pervious statement in the loop keep bg the same else change it to something new.

Link to comment
Share on other sites

$colors = array('yellow','red','blue','green'); // define enough colors for the number of different tank names

$fake[] = array('tank_name'=>'Tank 1'); // some fake data for demo purposes - actual data retrieved from database query
$fake[] = array('tank_name'=>'Tank 2');
$fake[] = array('tank_name'=>'Tank 3');
$fake[] = array('tank_name'=>'Tank 2');
$fake[] = array('tank_name'=>'Tank 2');
$fake[] = array('tank_name'=>'Tank 1');

$color = 0; // start at the first available color
$used = array(); // remember which colors have been used for each value
echo "<table>";
foreach($fake as $row){ // your while loop would replace this line
	// no color exists for this tank name, assign it
	$used[$row['tank_name']] = $colors[$color++];
$bg = $used[$row['tank_name']]; // get the color assignment for the tank name
echo"<tr bgcolor='$bg'><td>{$row['tank_name']}</td></tr>";
echo "</table>";

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • 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.