<table><?php $new = "SELECT * FROM `` ORDER BY `` DESC LIMIT 5"; $new_res = mysql_query($new) or die(mysql_error()); $rowcount = 0; while($new_row = mysql_fetch_assoc($new_res)) { $rowcount++; if ($rowcount == 1) { $bg = ("red"); } if ($rowcount == 2) { $bg = ("yellow"); } if ($rowcount == 3) { $bg = ("red"); } if ($rowcount == 4) { $bg = ("yellow"); } if ($rowcount == 5) { $bg = ("#red"); } echo"<tr><td style="background:$bg;;'>".$new_row['title']."</td></tr>"; PHP: } ?> </table> To add more rows, simply copy and paste one of the other if ($rowcount == 5) { $bg = ("#red"); } Code (markup):
I'm really torn right now. Obviously your a new to coding and don't deserved to be totally reamed. However, you're implicitly asking for it by pompously posting code that is trivial and inefficent. So, remember, however terse my response seems, I am holding back. Here goes: Why are you posting this? While it may work, its nothing to be proud of. Further, a solution to a coding problem or to expand its functionality is to never copy and paste repetitive code into it. Its too late to impress me, but you can raise my opinion of you if you rework your code so that it works for an infinite amount of rows without any copying and pasting.
That's cool yo. But I don't think it would add more rows by simply copy and paste one of the other if ($rowcount == 5) { $bg = ("#red"); } Code (markup): The condition you have on your while is to keep adding rows until the end of $new_res.
A more efficient way to do this... would be something like: <table> <? $colors = Array("#1181FF", "#FF0000", "#999999"); for ($i = 1; $i < 30; $i++) { $color = $colors[$i%count($colors)]; // this is the "magic" echo "<tr><td bgColor='" . $color. "'>hello</tr>\n"; } ?> </table> PHP:
Other things wrong/inefficient about your original post are; - you have multiple ifs, rather then elseifs - you have two ";" in the style tag - you start the style tag with a " and end with a '
if(0==$i%2) $bg = "odd"; else $bg = "even"; echo '<tr class="'.$bg.'">'; $i++; Code (markup): My take on this script... This will rotate between two colors until the while loop is done running rather than setting it at a set value... You can define i if you want before the while loop, but eh.
I've been using a generic alternator object (explaination) that works for any two strings, be they colors, CSS class names, style attributes, etc. If you can help it, you really want to try and stay away from using PHP to generate HTML with alternating row colors. It works, but if you use something that sorts table rows by columns, your alternated row backgrounds will be out of sync. Unfortunately neither IE8 or FF3 support the CSS nth-child() selector so depending on CSS alone isn't feasible. What you can do though, is use the same javascript you're sorting table rows with to apply the styles. Though, if you're not using any sort of table sorting features or anything of that sort, the PHP alternator does work I suppose.
try this: <table><?php $new = "SELECT * FROM `` ORDER BY `` DESC LIMIT 5"; $new_res = mysql_query($new) or die(mysql_error()); $rowcount = 0; $colors = array("red","yellow","green","blue"); while($new_row = mysql_fetch_assoc($new_res)) { if($rowcount >= count($colors)) { $rowcount = 0; } $bg = $colors[$rowcount]; $rowcount++; echo '<tr><td style="background:'.$bg.'";>'.$new_row['title']."</td></tr>"; PHP:
Thanks for your improved scripts guys. I remember a while back looking for something simple but couldn't quite find it. I understand mine was quite noobish but you have provided some great examples.
This is how I do it. config.php $dvd_row1 = "#FFFFFF"; // Row 1 background color $dvd_row2 = "#E6E6E6"; // Row 2 background color PHP: index.php $color_ctr = 1; //Alternate row color if ($color_ctr == 1) { $dvd_row = $dvd_row1; $color_ctr = 2; } else { $dvd_row = $dvd_row2; $color_ctr = 1; } PHP: table: echo "<tr valign=\"top\" bgcolor=\"$dvd_row\">\n"; PHP: