Very simple <td> background color alternation script!

Discussion in 'PHP' started by scottlpool2003, Jul 20, 2010.

  1. #1
    <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):
     
    scottlpool2003, Jul 20, 2010 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    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.
     
    plog, Jul 20, 2010 IP
  3. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #3
    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.
     
    Rainulf, Jul 20, 2010 IP
  4. bencummins

    bencummins Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    bencummins, Jul 20, 2010 IP
  5. bencummins

    bencummins Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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 '
     
    bencummins, Jul 20, 2010 IP
  6. bmhrules

    bmhrules Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    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.
     
    bmhrules, Jul 20, 2010 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    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. :)
     
    joebert, Jul 21, 2010 IP
  8. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #8
    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:
     
    gapz101, Jul 21, 2010 IP
  9. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #9
    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.
     
    scottlpool2003, Jul 22, 2010 IP
  10. pmwow

    pmwow Member

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #10
    use % operate
     
    pmwow, Aug 2, 2010 IP
  11. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #11
    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:
     
    Last edited: Aug 2, 2010
    MyVodaFone, Aug 2, 2010 IP