Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts

    PHP CSS Alternating Row Colors

    I have a query and a do while loop. I am trying to alternate row colors and think I can do it if I can pull in a number with each result returned, but am not sure what direction to head with it. Any assistance would be greatly appreciated.

    Here is my query:

    PHP Code:
    $documents mysql_query("SELECT docid, docname, docdescription, docbegindate, docenddate, docstatus, doctypeicon, doctypename, docfile, emp_name, emp_email, docstatusname, docorder FROM documentsmain WHERE (docstatus = '2' or docstatus = '1') AND docfolder = '$dcparent'") or die(mysql_error());
    $row_documents mysql_fetch_array($documents);
    $row_documents_total mysql_num_rows($documents); 
    Here is my loop:

    PHP Code:
    <?php do {?><tr>
                      <td width="5" class="list_item_boxleft"><table width="10" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td class="icon_box"><a href="uploads/<?php echo $row_documents['docfile']; ?>" target="_blank"><img src="images/file_icons/<?php echo $row_documents['doctypeicon']; ?>" alt="<?php echo $row_documents['docdescription']; ?>" width="16" height="16" border="0"></a></td>
                        </tr>
                      </table>                    <a href="uploads/<?php echo $row_documents['docfile']; ?>" target="_blank"></a><a href="uploads/<?php echo $row_documents['docfile']; ?>"></a></td>
                      <td class="list_item_box"><a href="uploads/<?php echo $row_documents['docfile']; ?>" target="_blank" title="<?php echo $row_documents['docdescription']; ?>"><?php echo $row_documents['docname']; ?></a></td>
                      <td class="list_item_box"><?php echo $row_documents['docbegindate']; ?></td>
                      <td class="list_item_box"><?php echo $row_documents['docenddate']; ?></td>
                      <td class="list_item_box"><?php echo $row_documents['emp_name']; ?></td>
                      <td class="list_item_box"><?php echo $row_documents['docstatusname']; ?></td>
                      <td width="50" height="3" nowrap class="list_item_box"><form name="form3" method="post" action="">
                   
                        <input name="docorder" type="text" id="docorder" value="<?php echo $row_documents['docorder']; ?>" size="3" maxlength="3">
                        <input name="submit" type="image" src="/images/refresh.png" alt="Approve Time Off Request" align="middle" width="16" height="16">
                        

                          </form>                  </td>
                      <td class="list_item_boxright"><div align="center">&nbsp;</div></td>
                    </tr><?php } while ($row_documents mysql_fetch_array($documents)); ?>

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    You could set a counter $counter and increment it for every iteration. Test if $counter%2 == 0 or 1 then call in a style for odd and even.

    eg:

    PHP Code:
    $counter 0
    do {
    if (
    $counter%2==0){
    echo 
    '<tr class="even">';
    } else {
    echo 
    '<tr class="odd">';
    }
    counter++; 
    CSS:

    Code:
    tr.odd td {
     background-color: #c00;
    }
    tr.even td {
     background-color: #efefef;
    }
    Or whatever colours you needed.
    You could of course use tertiary statements but I find this is simpler to understand.
    Hope this helps.
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com


  •  

    Posting Permissions

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