Jump to content

tr bg color by value


hyster

Recommended Posts

im trying to get a row color coded by a $var that is pulled from mysql.

if $val = yes the <tr> = green else red

 

im only concerned with the table row color atm. the rest of the code is still wip

 

relavent section of code

<tr bgcolor= <?php "$bgc"; ?>>  // this is were i want the $var placed
<td> <?php echo $rows['sku'] ; ?></td>
<td> <?php echo $rows['rsku']; ?></td>
<td> <?php echo $rows['make']; ?></td>
<td> <?php echo $rows['model']; ?></td>
<td> <?php echo $rows['comments']; ?></td>
<td> <?php echo $rows['sound']; ?></td>
<td> <?php echo $rows['gfx']; ?></td>
<td> <?php echo $rows['recovery']; ?></td>
<td> <?php echo $rows['date']; ?></td>
<td> <?php echo $rows['valby']; ?></td>


<td width="41" align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
<td width="36" align="center"><a href="delete_ac.php?id=<?php echo $rows['id']; ?>">delete</a></td>
</tr>



<?php

// table bg colour
$choice = strtoupper($rows['gfx']);
if($choice == "PASS"){
$bgc = "#7FFF00" ; 
}else{
$bgc = "#8B8989" ; }

}

?>

 

full code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Modify DataBase</title>
</head>

<body>
<p align="center"> </p>
<table width="400" border="1" align="center">
  <tr>
    <td TD BGCOLOR="#FF9900">
<?php

include("config.php"); 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$tbl_name="dsgi_serval"; 


// Retrieve data from database 
$search = trim($_POST['search']);
if ($search == '' || $search == ' '){

echo "Search Field is Empty - Please Input an SKU Number!";
die;
}
else{
$sql="select * from $tbl_name WHERE sku LIKE '%$search%'";
$result=mysql_query($sql) or die (mysql_error());
}

?>
</td>
  </tr>
</table>

<table width="800" height="72" border="1" align="center" cellpadding="3" cellspacing="0">
  
  
<tr>
<td width="113" align="center"><strong>Orig SKU</strong></td>
<td width="66" align="center"><strong>Recon SKU</strong></td>
<td width="90" align="center"><strong>Make</strong></td>
<td width="169" align="center"><strong>Model</strong></td>
<td width="58" align="center"><strong>comments</strong></td>
<td width="161" align="center"><strong>Sound</strong></td>
<td width="66" align="center"><strong>Graphics</strong></td>
<td width="90" align="center"><strong>Recovery</strong></td>
<td width="169" align="center"><strong>Date</strong></td>
<td width="58" align="center"><strong>Validated By</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr bgcolor= <?php "$bgc"; ?>>
<td> <?php echo $rows['sku'] ; ?></td>
<td> <?php echo $rows['rsku']; ?></td>
<td> <?php echo $rows['make']; ?></td>
<td> <?php echo $rows['model']; ?></td>
<td> <?php echo $rows['comments']; ?></td>
<td> <?php echo $rows['sound']; ?></td>
<td> <?php echo $rows['gfx']; ?></td>
<td> <?php echo $rows['recovery']; ?></td>
<td> <?php echo $rows['date']; ?></td>
<td> <?php echo $rows['valby']; ?></td>


<td width="41" align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
<td width="36" align="center"><a href="delete_ac.php?id=<?php echo $rows['id']; ?>">delete</a></td>
</tr>



<?php

// table bg colour
$choice = strtoupper($rows['gfx']);
if($choice == "PASS"){
$bgc = "#7FFF00" ; 
}else{
$bgc = "#8B8989" ; }

}

?>
</table>
<p> </p>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/218274-tr-bg-color-by-value/
Share on other sites

sorry to be a total noob but i cannot figure ou where to place the code.

i have change it slightly to

 

<?php
while ($rows=mysql_fetch_array($result)){
$tr = "$bgc"; 
?>
<tr bgcolor= <?php echo "$tr"; ?>>

 

any help on how/where to place this would be great.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Modify DataBase</title>
</head>

<body>
<p align="center"> </p>
<table width="400" border="1" align="center">
  <tr>
    <td TD BGCOLOR="#FF9900">
<?php

include("config.php"); 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$tbl_name="dsgi_serval"; 


// Retrieve data from database 
$search = trim($_POST['search']);
if ($search == '' || $search == ' '){

echo "Search Field is Empty - Please Input an SKU Number!";
die;
}
else{
$sql="select * from $tbl_name WHERE make LIKE '%$search%'";
$result=mysql_query($sql) or die (mysql_error());
}

?>
</td>
  </tr>
</table>

<table width="800" height="72" border="1" align="center" cellpadding="3" cellspacing="0">
  
  
<tr>
<td width="113" align="center"><strong>Orig SKU</strong></td>
<td width="66" align="center"><strong>Recon SKU</strong></td>
<td width="90" align="center"><strong>Make</strong></td>
<td width="169" align="center"><strong>Model</strong></td>
<td width="58" align="center"><strong>comments</strong></td>
<td width="161" align="center"><strong>Sound</strong></td>
<td width="66" align="center"><strong>Graphics</strong></td>
<td width="90" align="center"><strong>Recovery</strong></td>
<td width="169" align="center"><strong>Date</strong></td>
<td width="58" align="center"><strong>Validated By</strong></td>
</tr>
<?php
while ($rows=mysql_fetch_array($result)){
$tr = "$bgc"; 
?>
<tr bgcolor= <?php echo "$tr"; ?>>
<td> <?php echo $rows['sku'] ; ?></td>
<td> <?php echo $rows['rsku']; ?></td>
<td> <?php echo $rows['make']; ?></td>
<td> <?php echo $rows['model']; ?></td>
<td> <?php echo $rows['comments']; ?></td>
<td> <?php echo $rows['sound']; ?></td>
<td> <?php echo $rows['gfx']; ?></td>
<td> <?php echo $rows['recovery']; ?></td>
<td> <?php echo $rows['date']; ?></td>
<td> <?php echo $rows['valby']; ?></td>


<td width="41" align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
<td width="36" align="center"><a href="delete_ac.php?id=<?php echo $rows['id']; ?>">delete</a></td>
</tr>



<?php

// table bg colour
$choice = strtoupper($rows['sku']);
if($choice == "NIALL"){
$bgc = "#7FFF00" ; 
}else{
$bgc = "#EE2C2C" ; }

}

?>
</table>
<p> </p>
</body>
</html>

Lets step through (the relevant bits of) your code:

 

[*]Get first result from database

[*]Output the first result

[*]Determine what the background colour should be

[*]Get the second result

[*]Output the second result

[*]Determine what the background colour should be

[*]...

 

Hopefully you can see the problem there. The solution is to simply move the few lines of code which decide what the background colour should be to a place above where you use it in the HTML.

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.