0

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;

2 Answers 2

2

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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?

6 Comments

assume....to put an alternate row color we know...we use somthing like this formulat...and make alternate row colors $tr_color = $i%2;
The background-color property can be in the rgb(0-255, 0-255, 0-255) format or in hex color codes.
what should i put instead of last time condition is true
what is the test case i should put there.
Could I see your while loop condition?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.