Hello all, I have a little question that im hoping someone can lead me in the right direction to answer. I have a PHP script that pulls information from a database and displays it. Simple. What I would like to do NOW, is based on a field value integer (0 - 3) I would like to make the field's different colors.

I will post my code below, for some perusal. Just some quick facts...

The name of the field is "VarCustom03"
The numbers are 0, 1, 2, 3. Each with a different color.

I was thinking of doing something like this...

<?php if ($x_VarCustom03 = 0) {?>  
<tr bgcolor="#FFFFCC">
<?php } else if ($x_VarCustom03 = 1) {?>
<tr bgcolor="#66FF33">
<?php } else if ($x_VarCustom03 = 2) {?>
<tr bgcolor="#FFFF33">
<?php } else if ($x_VarCustom03 = 3) {?>
<tr bgcolor="red">
<?php } else { ?>
<tr bgcolor="#FFFFCC">
<?php } ?>

But that does not seem to work. The first "match" that the PHP code finds it makes all the other fields that color. In this case green. Could someone help?!

The snipplet is here..

<--SNIP-->

<?php
$recCount = $startRec - 1;
@mysql_data_seek($rs, $recCount);
while (($row = mysql_fetch_assoc($rs)) && ($recCount < $stopRec))
{
	$recCount++;
	if (intval($recCount) >= intval($startRec))
    {
		//set row color
		$bgcolor="#FFFFFF";
?>
<?php 
$x_MsgUnique = $row["MsgUnique"];
$x_MsgDate = $row["MsgDate"];
$x_MsgTime = $row["MsgTime"];
$x_MsgPriority = $row["MsgPriority"];
$x_MsgHostname = $row["MsgHostname"];
$x_MsgText = $row["MsgText"];
?>

<tr bgcolor="<?php echo $bgcolor ?>">
<td><font size="-1">
<b><?php echo $x_MsgUnique ?></b>&nbsp;
</font></td>
<td><font size="-1">
<?php echo $x_MsgDate ?>&nbsp;
</font></td>
<td><font size="-1">
<?php echo $x_MsgTime ?>&nbsp;
</font></td>
<td><font size="-1">
<?php echo $x_MsgPriority ?>&nbsp;
</font></td>
<td><font size="-1">
<a href="telnet://<?php echo $x_MsgHostname ?>"><?php echo $x_MsgHostname ?></a>&nbsp;
</font></td>
<td><font size="-1">
<?php echo str_replace(chr(10),"<br>",$x_MsgText . "") ?>&nbsp;
</font></td>
<td><a href="<?php
if ($row["MsgUnique"] != NULL)
{ echo "mantic_msgsview.php?key=".urlencode($row["MsgUnique"]); }
else
{ echo "javascript:alert('Invalid Record! Key is null.');"; }
?>"><img src="images/view.gif" alt="View" width="16" height="16" border="0"></a></td>
<td><a href="<?php
if ($row["MsgUnique"] != NULL)
{ echo "mantic_msgsedit.php?key=".urlencode($row["MsgUnique"]); }
else
{ echo "javascript:alert('Invalid Record! Key is null.');"; }
?>"><img src="images/edit.gif" alt="Edit" width="16" height="16" border="0"></a></td>
</tr>

    <?php if ($x_VarCustom03 = 0) {?>

    <tr bgcolor="#FFFFCC">

    <?php } else if ($x_VarCustom03 = 1) {?>

    <tr bgcolor="#66FF33">

    <?php } else if ($x_VarCustom03 = 2) {?>

    <tr bgcolor="#FFFF33">

    <?php } else if ($x_VarCustom03 = 3) {?>

    <tr bgcolor="red">

    <?php } else { ?>

    <tr bgcolor="#FFFFCC">

    <?php } ?>

    The reason it always selects the first color is because you are setting the var = 0 every time.

    if ($var = 0) <-- will set $var = 0

    if ($var == 0) <-- double = key tells php to COMPARE, not set.

    Basically, set your = to == in your if statement and it should work.

      Thanks acid. I changed the code as you instructed.

      
      <?php if ($x_VarCustom03 == 0) {?>   
      <tr bgcolor="#FFFFCC">
      <?php } else if ($x_VarCustom03 = 1) {?>
      <tr bgcolor="#66FF33">
      <?php } else if ($x_VarCustom03 = 2) {?>
      <tr bgcolor="#FFFF33">
      <?php } else if ($x_VarCustom03 = 3) {?>
      <tr bgcolor="red">
      <?php } else { ?>
      <tr bgcolor="#FFFFCC">
      <?php } ?>

      Now this is interesting. It seems that now, whatever field is NOT 0, it turns to a 1 and hightlights it green?

      Right now i have 3 lines in the DB. all with diffrent numbers. 0, 1, 2. When i put the == in there, whatever is NOT a 0 is a 1 and highlights green?

      Insight?

        you have to change the = to == in your else if statements as well.

          Hey!

          Did that...now everything that is not 0 is 3, RED..?

          <?php if ($x_VarCustom03 == 0) {?>

          <tr bgcolor="#FFFFCC">

          <?php } else if ($x_VarCustom03 == 1) {?>

          <tr bgcolor="#66FF33">

          <?php } else if ($x_VarCustom03 == 2) {?>

          <tr bgcolor="#FFFF33">

          <?php } else if ($x_VarCustom03 == 3) {?>

          <tr bgcolor="red">

          <?php } else { ?>

          <tr bgcolor="#FFFFCC">

          <?php } ?>

          What am i missing?

          http://66.80.198.52/mantic/mantic_msgslist.php

            <?php
            if ($x_VarCustom03 == 0)
            	{
            		$rowcolor = "#FFFFCC";
            	}
            elseif ($x_VarCustom03 == 1)
            	{
            		$rowcolor = "#66FF33";
            	}
            elseif ($x_VarCustom03 == 2)
            	{
            		$rowcolor = "#FFFF33";
            	}
            elseif ($x_VarCustom03 == 3)
            	{
            		$rowcolor = "red";
            	}
            else
            	{
            		$rowcolor = "#FFFFCC";
            	}
            ?>
            
            
            Now, in your html do this:
            
            <tr bgcolor="<? echo($rowcolor); ?>">
            

              This would be easier with an array and the modulus operator:
              <?php
              $colors = array ("#FFFFCC", "#66FF33", "#FFFF33");

              for ($x=1; $x < 16; $x++) {
              echo $colors[$x % 3] . '<br>';
              }

              ?>

                This is strange. I just copied the code that you sent. Same thing. Seems that now, everything that is NOT 0, is Red? Would you like me to post the whole code? I dont understand it. What i did also was put a little PHP code to list the numbers. Without the code new code in there, the numbers are diffrent. Meaning that i have 0, 3, 2 in the fields. When i past the code, all the NONE 0's, turn into 3's and are red?

                ?>
                <?php 
                $x_MsgUnique = $row["MsgUnique"];
                $x_MsgDate = $row["MsgDate"];
                $x_MsgTime = $row["MsgTime"];
                $x_MsgPriority = $row["MsgPriority"];
                $x_MsgHostname = $row["MsgHostname"];
                $x_MsgText = $row["MsgText"];
                $x_VarCustom03 = $row["VarCustom03"];
                ?>
                
                <?php 
                if ($x_VarCustom03 == 0) 
                    { 
                        $rowcolor = "#FFFFCC"; 
                    } 
                elseif ($x_VarCustom03 == 1) 
                    { 
                        $rowcolor = "#66FF33"; 
                    } 
                elseif ($x_VarCustom03 == 2) 
                    { 
                        $rowcolor = "#FFFF33"; 
                    } 
                elseif ($x_VarCustom03 == 3) 
                    { 
                        $rowcolor = "red"; 
                    } 
                else 
                    { 
                        $rowcolor = "#FFFFCC"; 
                    } 
                ?>
                
                <tr bgcolor="<? echo($rowcolor); ?>">
                
                <td><font size="-1">
                <b><?php echo $x_MsgUnique ?></b>&nbsp;
                </font></td>
                <td><font size="-1">
                <?php echo $x_MsgDate ?>&nbsp;
                </font></td>
                <td><font size="-1">
                <?php echo $x_MsgTime ?>&nbsp;
                </font></td>
                <td><font size="-1">
                <?php echo $x_MsgPriority ?>&nbsp;
                </font></td>
                <td><font size="-1">
                <a href="telnet://<?php echo $x_MsgHostname ?>"><?php echo $x_MsgHostname ?></a>&nbsp;
                </font></td>
                <td><font size="-1">
                <?php echo str_replace(chr(10),"<br>",$x_MsgText . "") ?>&nbsp;
                </font></td>
                <td><font size="-1">
                <?php echo $x_VarCustom03 ?>&nbsp;
                </font></td>
                <td><a href="<?php
                if ($row["MsgUnique"] != NULL)
                { echo "mantic_msgsview.php?key=".urlencode($row["MsgUnique"]); }
                else
                { echo "javascript:alert('Invalid Record! Key is null.');"; }
                ?>"><img src="images/view.gif" alt="View" width="16" height="16" border="0"></a></td>
                <td><a href="<?php
                if ($row["MsgUnique"] != NULL)
                { echo "mantic_msgsedit.php?key=".urlencode($row["MsgUnique"]); }
                else
                { echo "javascript:alert('Invalid Record! Key is null.');"; }
                ?>"><img src="images/edit.gif" alt="Edit" width="16" height="16" border="0"></a></td>
                </tr>
                

                  NEVERMIND!!! ITs working....!!!

                  Thanks!!! You guys are the BOMB!!!!!!!!!!!

                    2 years later

                    Hello everyone, i was the original poster here, and im having some issues integrating this into a new version of code. Could someone please help? I would like to do the same thing, based on a value in a feild, highlight that feild with a certain color..

                    Here is the code (without edit):

                    		// Set row color
                    		$sItemRowClass = " bgcolor=\"#FFFFFF\"";
                    		$sListTrJs = "";
                    		$x_ID = $row["ID"];
                    		$x_Site = $row["Site"];
                    		$x_Down_Date_Time = $row["Down_Date_Time"];
                    		$x_Up_Date_Time = $row["Up_Date_Time"];
                    		$x_Total_Downtime = $row["Total_Downtime"];
                    		$x_Priority = $row["Priority"];
                    		$x_Exclusion = $row["Exclusion"];
                    		$x_Comments = $row["Comments"];
                    		$x_Last_Updated = $row["Last_Updated"];
                    	$bEditRow = (($_SESSION["outages_Key_ID"] == $x_ID) && ($nEditRowCnt == 0));
                    	if ($bEditRow) {
                    		$nEditRowCnt++;
                    		$sItemRowClass = " bgcolor=\"#FFFF99\"";
                    		$sListTrJs = "";
                    	}
                    ?>
                    	<!-- Table body -->
                    	<tr<?php echo $sItemRowClass; ?><?php echo $sListTrJs; ?>>
                    <?php if ($bEditRow) { ?>
                    <input type="hidden" id="x_ID" name="x_ID" value="<?php echo htmlspecialchars(@$x_ID); ?>">
                    <?php } ?>
                    

                    And here is what i tried to do, but it did not work:

                    			// Set row color
                    			$sItemRowClass = " bgcolor=\"#FFFFFF\"";
                    			$sListTrJs = "";
                    			$x_ID = $row["ID"];
                    			$x_Site = $row["Site"];
                    			$x_Down_Date_Time = $row["Down_Date_Time"];
                    			$x_Up_Date_Time = $row["Up_Date_Time"];
                    			$x_Total_Downtime = $row["Total_Downtime"];
                    			$x_Priority = $row["Priority"];
                    			$x_Exclusion = $row["Exclusion"];
                    			$x_Comments = $row["Comments"];
                    			$x_Last_Updated = $row["Last_Updated"];
                    		$bEditRow = (($_SESSION["outages_Key_ID"] == $x_ID) && ($nEditRowCnt == 0));
                    		if ($bEditRow) {
                    			$nEditRowCnt++;
                    			$sItemRowClass = " bgcolor=\"#FFFF99\"";
                    			$sListTrJs = "";
                    		}
                    	?>
                    
                    <?php
                    if ($x_Exclusion == 0)
                    {
                    $rowcolor = "#FFFFCC";
                    }
                    elseif ($x_Exclusion == 1)
                    {
                    $rowcolor = "#66FF33";
                    }
                    elseif ($x_Exclusion == 2)
                    {
                    $rowcolor = "#FFFF33";
                    }
                    elseif ($x_Exclusion == 3)
                    {
                    $rowcolor = "red";
                    }
                    else
                    {
                    $rowcolor = "#FFFFCC";
                    }
                    ?>
                    
                    <tr bgcolor="<? echo($rowcolor); ?>"
                    		<!-- Table body -->
                    		<tr<?php echo $sItemRowClass; ?><?php echo $sListTrJs; ?>>
                    	<?php if ($bEditRow) { ?>
                    	<input type="hidden" id="x_ID" name="x_ID" value="<?php echo htmlspecialchars(@$x_ID); ?>">
                    	<?php } ?>
                    

                    Any help would be grealty appreciated.

                      Write a Reply...