tags:

views:

34

answers:

2

How to change background color for last render row in while loop ....

that is

row1
row2
**row3**

If row4 is added then it should

row1
row2
row3
**row4**

that is star represents some bgcolor red...

this loop am following,,,, $i = 1; while ($row = mysql_fetch_assoc($sql)) {

$out_data[] = "<p>$i {$row['news']} </p>";

$i++;
}

$divider = '<div class="border"></div>';
$output = implode ($divider, $out_data);

echo $output;
A: 

EDIT: You'd need to change the formatting for the last element of $out_data like this: (once the while loop completes)

…
$out_data[ count($out_data) - 1 ] = "<div style='background-color:red'>" . end($out_data) . "</div>"

$divider = '<div class="border"></div>';
$output = implode ($divider, $out_data);

echo $output;

how does that work?

pop850
but this is dynamic..not an static one...
Bharanikumar
assume....to put an alternate row color we know...we use somthing like this formulat...and make alternate row colors $tr_color = $i%2;
Bharanikumar
The background-color property can be in the rgb(0-255, 0-255, 0-255) format or in hex color codes.
pop850
what should i put instead of last time condition is true
Bharanikumar
what is the test case i should put there.
Bharanikumar
Could I see your while loop condition?
pop850
just i added...plz chk my snippet...
Bharanikumar
am not understand about the above while loop..bcoz in while i should use the (LOOP) while($row=mysql_fetch_array) .....how i proceed for my question...
Bharanikumar
nevermind about the while loop thing, see my edited code
pop850
yes...i got it man...thanks a lot
Bharanikumar
+2  A: 

you can use mysql_num_rows to get the number of rows you have before the while loop.

 $row_count = mysql_num_rows($sql)

inside the while loop

 if ($i==$row_count)
    // put color $out_data[] = "<p style="colorsomething">$i {$row['news']} </p>";
 else
    // dont put color $out_data[] = "<p>$i {$row['news']} </p>";

does this help?

Anand