How to generated zebra row with php

Discussion in 'PHP' started by junandya, Nov 14, 2007.

  1. #1
    Hello,...

    please look at this

    <?PHP
    $sourceData = mysql_query(" select * from member where loc='a'",$connection);
    
    echo "
    		<TABLE width=\"500\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
    			<TR>
    				<TD width=\"10\" class=\"header\">No</TD>
    				<TD width=\"140\" class=\"header\">Name</TD>
    				<TD width=\"350\" class=\"header\">Address</TD>
    			</TR>
    	";
    while($theMember = mysql_fetch_array($sourceData))
    {
    	echo"
    			<TR>
    				<TD class=\"content\">" . $theMember["id"] . "</TD>
    				<TD class=\"content\">" . $theMember["memName"] . "</TD>
    				<TD class=\"content\">" . $theMember["memAddress"] . "</TD>
    			</TR>
    		";
    }
    
    echo "
    		</TABLE>
    	";
    ?>
    PHP:
    my question is, how can i give a bgcolor="#f5f5f5" on odd row and give a bgcolor="#c3c3c3" on even row,...so the result rows will has different color for eact it encrease.

    can i do it with "while" or i should do it with "do"

    could anyone here help me with an example script, please. i really appriciate it..

    Best regards
     
    junandya, Nov 14, 2007 IP
  2. GolfHos

    GolfHos Member

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    How about something like this:


    <?PHP
    $sourceData = mysql_query(" select * from member where loc='a'",$connection);
    $color="#f5f5f5";
    
    echo "
    		<TABLE width=\"500\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
    			<TR>
    				<TD width=\"10\" class=\"header\">No</TD>
    				<TD width=\"140\" class=\"header\">Name</TD>
    				<TD width=\"350\" class=\"header\">Address</TD>
    			</TR>
    	";
    while($theMember = mysql_fetch_array($sourceData))
    {
    	echo"
    			<TR bgcolor=" . $color . ">
    				<TD class=\"content\">" . $theMember["id"] . "</TD>
    				<TD class=\"content\">" . $theMember["memName"] . "</TD>
    				<TD class=\"content\">" . $theMember["memAddress"] . "</TD>
    			</TR>
    		";
    		
    	if($color=="#f5f5f5"){
        $color="#c3c3c3";
      } else {
        $color="#f5f5f5";
      }	
    }
    
    echo "
    		</TABLE>
    	";
    ?>
    PHP:
     
    GolfHos, Nov 14, 2007 IP
  3. junandya

    junandya Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    thanx GolfHos, it's working properly, i really appricate it..thanx
     
    junandya, Nov 14, 2007 IP
  4. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Watch out if you are using that Javscript which allow you to sort the table by clicking the table headers It totally fucks up the alternating backgrounds as each row is moved around.
     
    tamen, Nov 15, 2007 IP