moondran Posted April 20, 2012 Share Posted April 20, 2012 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 WHERE (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"]; echo" <tr bgcolor="$bg"><td>$tank_name </td></tr>"; } Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/261293-change-bgcolor-on-variable-change/ Share on other sites More sharing options...
cyberRobot Posted April 20, 2012 Share Posted April 20, 2012 You could use a switch statement: <?php //... echo "<tr bgcolor='"; switch($tank_name) { case 'Tank 1': echo 'yellow'; break; case 'Tank 2': echo 'red'; break; case 'Tank 3': echo 'blue'; break; } echo "'><td>$tank_name</td></tr>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/261293-change-bgcolor-on-variable-change/#findComment-1339073 Share on other sites More sharing options...
moondran Posted April 23, 2012 Author Share Posted April 23, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261293-change-bgcolor-on-variable-change/#findComment-1339741 Share on other sites More sharing options...
PFMaBiSmAd Posted April 23, 2012 Share Posted April 23, 2012 <?php $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 if(!isset($used[$row['tank_name']])){ // 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/261293-change-bgcolor-on-variable-change/#findComment-1339745 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.