Jump to content

background color depending on variable


esoteric

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/246568-background-color-depending-on-variable/
Share on other sites

<?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

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>

working  8)

<?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>

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>

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>


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.