Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

highlighting a mysql row if == 1

Status
Not open for further replies.

sophielois

Technical User
Sep 8, 2005
66
GB
Is it possible to change the colour of a certain row within a mysql result.

ill try and explain what i mean
Code:
<?php

$result = mysql_query("SELECT * FROM candidate ORDER BY first_name");

	while($row = mysql_fetch_object($result))
		{
			echo "<tr  bgcolor='#ffffff'>";
			echo "<td>$row->first_name $row->last_name</td>";
			echo '<td>';
    		if ($row->level_2 == 1) {
        	echo "Car 2";
    		} else{
        	echo "Car 3";
    		}
    		echo '</td>';
			echo "<td>$row->reg_num</td>";
			echo "<td>$row->wrk_place</td>";
			echo "<td>$row->ass_name</td>";
			echo "<td>$row->i_name</td>";
			echo "<td>$row->signup_date</td>";
			echo '<td>';
    		if ($row->completed == 1) {
        	echo "YES";
    		} else{
        	echo "NO";
    		}
    		echo '</td>';
			echo "<td><a href=\"/admin/can/can_view.php?userid=$row->userid\">View</a></td>";
			echo "</tr>";
			
		}
	
?>

How would i make
a row appear shaded if $row->completed == 1

eg The row is a different colour to the rest if $row->completed == 1

Is this possible???

Soph
 
Easily, Instead of echoing out everything, save them to a variable (minus the tr and /tr) and then at the end if completed == 1 then do an echo with a different tr color.
 
This is a basic conditional statement that creates HTML output:
Code:
 while($row = mysql_fetch_object($result))
        {
            # decide on the color
            $color = ($row->completed==1)?'#CC0000':'#FFFFFF';
            echo "<tr  bgcolor='$color'>";
            echo "<td>$row->first_name $row->last_name</td>";
            echo '<td>';
 #etc.

I used the ternary operator here. It really is nothing else than
Code:
if ($row->completed == 1){
    $color = '#CC0000';
} else {
    $color = '#FF0000';
}
 
Thanks,

thats great... easy when you know how :)

Soph
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top