alternative row colors in table with records

Discussion in 'PHP' started by Kyriakos, Apr 22, 2008.

  1. #1
    hi guys,

    i want to have alternative row color in my table. this is the asp code and it's working fine. can anyone convert this to php for me?
    <table>
    	<%Dim rowCount
    	rowCount=0%>
    	<%While not rs.EOF%>
    		<tr bgcolor="<%if (rowCount mod 2)=0 then response.write("#ffffff") else response.write("#bcd8f5")%>>
    			<td></td><td></td> 
    		</tr> 
    		<%rowCount = rowCount + 1%>
    	<%rs.MoveNext%>
    	<%Wend%>
    </table>
    HTML:
    thanks in advance.
     
    Kyriakos, Apr 22, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    
    <table>
    <?php $count = 0; 
    
    while(NOT_SURE_WHAT_DATA_YOU_ARE_LOOPING_WITH): ?>
    
    	<tr bgcolor="<?php if($count%2==0){ echo "ffffff"} else { echo"bcd8f5"};?>">
    		<td></td><td></td>
    	</tr>
    
    <?php $count++; 
    endwhile; ?>
    
    </table>
    
    
    PHP:
    ALso I took out the # before the color. I'm pretty sure you can't use #ffffff for the bgcolor property. You could alternately use style="background:#ffffff;"
     
    jestep, Apr 22, 2008 IP
  3. CPURules

    CPURules Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Replace the 10 in "$rowcount < 10" with the number of rows you have :)

    
    <?php
    echo "<TABLE>";
    For($rowcount = 0; $rowcount < 10; $rowcount++) {
    echo "<tr bgcolor=\"" . (($rowcount%2) == 0 ? "#FFFFFF" : "#BCDF85") . "\"><td></td></tr>";
    }
    echo "</TABLE>";
    ?>
    
    PHP:
     
    CPURules, Apr 22, 2008 IP
  4. jkwiz

    jkwiz Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?php
    for ($i = 0; $i <= 14; ++$i)
    {
    	$color = ($i&1) ? "#bcd8f5" : "#ffffff";
    }
    ?>
    PHP:
     
    jkwiz, Apr 22, 2008 IP
  5. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    <table>
    <?PHP
    $rowCount=0;
    while($sqlFetch=mysql_fetch_array($sqlQuery))
    {
    ?><tr bgcolor="#<?PHP echo (rowCount%2==0?'ffffff'):"bcd8f5";?>">
    <td></td><td></td> 
    </tr> 
    <?PHP
    $rowCount++;
    }
    ?>
    </table> 
    
    PHP:
     
    NatalicWolf, May 3, 2008 IP