esoteric Posted September 6, 2011 Share Posted September 6, 2011 how can i set the color of a table background depending on what a variable is in my database? I currently have a table which echos data from my database, but i want the background too be a color depending on the value of a variable called 'status' (there are 3 variations) the variations are; order placed, processing and complete so for example: if (status == placed) { background would be red } if (status == processing) { background would be orange } if (status == complete) { background would be green } appreciate any help. thank you. Quote Link to comment https://forums.phpfreaks.com/topic/246568-background-color-depending-on-variable/ Share on other sites More sharing options...
Pikachu2000 Posted September 6, 2011 Share Posted September 6, 2011 Use 3 different CSS classes and set the value of the element's class= attribute conditionally, based on the value of status. Quote Link to comment https://forums.phpfreaks.com/topic/246568-background-color-depending-on-variable/#findComment-1266122 Share on other sites More sharing options...
voip03 Posted September 6, 2011 Share Posted September 6, 2011 <?php if (status == placed){$background_color ='red';} if (status == processing){$background_color ='orange'} if (status == complete){$background_color ='green'} ?> <table width="100" border="1" cellspacing="0" cellpadding="0" bgcolor="<?php echo $background_color; ?>"> also try with switch() statement Quote Link to comment https://forums.phpfreaks.com/topic/246568-background-color-depending-on-variable/#findComment-1266125 Share on other sites More sharing options...
codefossa Posted September 6, 2011 Share Posted September 6, 2011 Basically the same as if/else. $background = $status == 'placed' ? 'red' : ($status == 'processing' ? 'orange' : ($status == 'complete' ? 'green' : null)); Quote Link to comment https://forums.phpfreaks.com/topic/246568-background-color-depending-on-variable/#findComment-1266129 Share on other sites More sharing options...
esoteric Posted September 6, 2011 Author Share Posted September 6, 2011 thanks for the replies, i tried both suggestions but it doesn't seem to work. Can you see anything im doing wrong here> <? database stuff ?> <?php $background = $status == 'placed' ? '#FFCACA' : ($status == 'processing' ? '#FFD89D' : ($status == 'complete' ? '#BFFFBF' : null)); ?> <table width="600" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="<?php echo $background; ?>"> <tr> <td><?php $sql="SELECT * FROM orderTable WHERE userId = '{$_SESSION['user_id']}'"; if ($query=@mysql_query($sql)) { if (mysql_num_rows($query) > 0) { while ($req=mysql_fetch_array($query)) { ?></td> <td> </td> </tr> <tr> <td width="223">Invoice - <?php echo $req['orderNum'];?><br> <br> <?php echo $req['orderDate'];?> <br> <br> £<?php echo $req['total'];?> </td> <td width="377" align="center" valign="top"> <textarea name="textarea" cols="60" rows="5" disabled id="textarea"><?php echo $req['strMessageBody'];?></textarea></td> </tr> <tr> <td colspan="2" class="description"><?php } } else { echo "No orders found."; } } else { echo "Query failed ".mysql_error(); } ?></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/246568-background-color-depending-on-variable/#findComment-1266135 Share on other sites More sharing options...
jcbones Posted September 6, 2011 Share Posted September 6, 2011 look at your page source, that should tell you if the PHP variable is working. Other than that, you should be using CSS, and not depreciated attributes. Quote Link to comment https://forums.phpfreaks.com/topic/246568-background-color-depending-on-variable/#findComment-1266157 Share on other sites More sharing options...
voip03 Posted September 6, 2011 Share Posted September 6, 2011 working <?php $status='placed'; if($status == 'placed'){$background_color='red';} if($status == 'processing'){$background_color ='orange';} if($status == 'complete'){$background_color ='green';} ?> <table width="100" border="1" cellspacing="0" cellpadding="0" bgcolor="<?php echo $background_color; ?>"> <tr> <td> </td> <td> </td> <td> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/246568-background-color-depending-on-variable/#findComment-1266166 Share on other sites More sharing options...
Pikachu2000 Posted September 6, 2011 Share Posted September 6, 2011 And still another way: <?php $background_color = array( 'none' => '', 'placed' => 'red', 'processing' => 'orange', 'complete' =>'green'); $status = (!empty($status) && array_key_exists($status, $background_color)) ? $status : 'none'; ?> <table width="100" border="1" cellspacing="0" cellpadding="0" bgcolor="<?php echo $background_color[$status]; ?>"> <tr> <td> </td> <td> </td> <td> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/246568-background-color-depending-on-variable/#findComment-1266185 Share on other sites More sharing options...
voip03 Posted September 6, 2011 Share Posted September 6, 2011 Switch Method <?php $status='complete'; switch ($status) { case 'placed': $background_color='red'; break; case processing: $background_color ='orange'; break; case 'complete': $background_color ='green'; break; } ?> <table width="100" border="1" cellspacing="0" cellpadding="0" bgcolor="<?php echo $background_color; ?>"> <tr> <td> </td> <td> </td> <td> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/246568-background-color-depending-on-variable/#findComment-1266209 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.