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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

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