SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    574
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need alternating rows in table

    I'm trying to create a dynamic table where one row is shaded and the next is not like you see in accounting kinds of situations. I don't know how to write the code so the first time through the loop, it gives a td class="shaded" and the next time, it's just a plain td. Here's the code I have so far:

    PHP Code:
    <?php
        
    for($i 0$i count($clients); $i++)    {
        
    ?>
            <tr class="shaded">
                <td class="rightDarkerBorder"><input type="checkbox" name="clientid[]" value="<?php echo $clients[$i]["clientID"]; ?>" /></td>
                <td scope="col" align="center"><a href="singleClient.php?clientid=<?php echo $clients[$i]["clientID"];?>" ><img src="<?php echo URL ?>_images/editIcon.gif" alt="Edit" /></a></td>
                <td><?php echo $clients[$i]["lastName"];?></td>
                <td><?php echo $clients[$i]["firstName"];?></td>
                <td><?php echo $clients[$i]["phoneCell"];?></td>
                <td><?php echo $clients[$i]["phoneHome"];?></td>
                <td><?php echo $clients[$i]["phoneWork"];?></td>
                <td><?php echo $clients[$i]["address1"];?></td>
                <td><?php echo $clients[$i]["city"];?></td>                
                <td><?php echo $clients[$i]["state"];?></td>
                <td><?php echo $clients[$i]["zip"];?></td>        
                <td><?php echo $clients[$i]["birthday"];?></td>        
                <td><?php echo $clients[$i]["therapist"];?></td>
            </tr>
            <?php
        
    }
    Help?

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
        $color1 
    "#CCFFCC"
        
    $color2 "#BFD8BC"
        
    $row_count 0
        for(
    $i 0$i count($clients); $i++)    {
        
    $row_color = ($row_count &#37; 2) ? $color1 : $color2; 

        
    ?>

            <tr class="shaded">

                <td class="rightDarkerBorder" bgcolor="<?=$row_color?>"><input type="checkbox" name="clientid[]" value="<?php echo $clients[$i]["clientID"]; ?>" /></td>

                <td scope="col" align="center" bgcolor="<?=$row_color?>"><a href="singleClient.php?clientid=<?php echo $clients[$i]["clientID"];?>" ><img src="<?php echo URL ?>_images/editIcon.gif" alt="Edit" /></a></td>

                <td bgcolor="<?=$row_color?>"><?php echo $clients[$i]["lastName"];?></td>

                <td bgcolor="<?=$row_color?>"><?php echo $clients[$i]["firstName"];?></td>

                <td bgcolor="<?=$row_color?>"><?php echo $clients[$i]["phoneCell"];?></td>

                <td bgcolor="<?=$row_color?>"><?php echo $clients[$i]["phoneHome"];?></td>

                <td bgcolor="<?=$row_color?>"><?php echo $clients[$i]["phoneWork"];?></td>

                <td bgcolor="<?=$row_color?>"><?php echo $clients[$i]["address1"];?></td>

                <td bgcolor="<?=$row_color?>"><?php echo $clients[$i]["city"];?></td>                

                <td bgcolor="<?=$row_color?>"><?php echo $clients[$i]["state"];?></td>

                <td bgcolor="<?=$row_color?>"><?php echo $clients[$i]["zip"];?></td>        

                <td bgcolor="<?=$row_color?>"><?php echo $clients[$i]["birthday"];?></td>        

                <td bgcolor="<?=$row_color?>"><?php echo $clients[$i]["therapist"];?></td>

            </tr>

            <?php
          $row_count
    ++;

        }
    ?>

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Easier to just assign a css class to the <tr> instead of the <td>s
    PHP Code:
    <?php
    $counted 
    count($clients); // pre-calculate, is faster in php, otherwise php runs count() every iteration

    for( $i=0$i $counted$i++ )
    {
       ...
       if ( 
    $i == )
          
    $tr_class 'class="rw1"';
      
    // else
      //    $tr_class = 'class="rw2"';
    ?>
       <tr <?php echo $tr_class;?>>
    You can also do column colors, using <colgroup> and <col>, example below 3, 6, 9 will be pink
    HTML Code:
    <style type="text/css">
      .pink { background: pink; }
    </style>
    <table summary="">
      <colgroup>
        <col></col>
        <col></col>
        <col class="pink"></col>
      </colgroup>
      <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
      </tr>
      <tr>
        <td>4</td>
        <td>5</td>
        <td>6</td>
      </tr>
      <tr>
        <td>7</td>
        <td>8</td>
        <td>9</td>
      </tr>
    </table>

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I usually do like this:
    Code:
    <style type="text/css">
    	.bgPink{
    		background-color:#FF9999;
    		color:#000000;
    	}
    	.bgBlack{
    		background-color:#000000;
    		color:#FFFFFF;
    	}
    </style>
    PHP Code:
    echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
    for(
    $i=0;$i<4;$i++){
    $bgCls = ($bgCls == "bgPink") ? "bgBlack" "bgPink";

      echo 
    '<tr class="' $bgCls'">';
      echo 
    '<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
      echo 
    '</tr>'
    }
    echo 
    '</table>'
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    NL, Rotterdam
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by abb View Post
    Easier to just assign a css class to the <tr> instead of the <td>s
    [php]
    <?php
    You can also do column colors, using <colgroup> and <col>, example below 3, 6, 9 will be pink
    HTML Code:
    <style type="text/css">
      .pink { background: pink; }
    </style>
    <table summary="">
      <colgroup>
        <col></col>
        <col></col>
        <col class="pink"></col>
      </colgroup>
      <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
      </tr>
    </table>
    Thanks for the colgroup example, that somehow never got into my html-repertoire.

    As for the TS's issue: go for the css class in the tr, you can control every descendant element by that. Suggestion: go for generic names like 'odd'/'even' or 'row1'...'row4'.

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    574
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the tips. My table will soon have zebra stripes.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •