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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    CSS Styling A PHP Generated Table

    I have a layout on my table cells which I guess some people call a zebra layout. All the odd rows are say blue and all the even are blue. So say the first row blue, second red and so on... It's easy to do this in a simple table, but for my php page which shows the results do they all have to be odd? Of anyone can tell me a simple way of doing this...

    Code:
      <tr valign="top" class="odd">
        <td><?php echo $qry['FirstName']; ?></td>
    	<td><?php echo $qry['LastName']; ?></td>
        <td><?php echo date('d/m/Y', strtotime($qry['DateOfBirth'])); ?></td>
        <td><?php echo($qry['loginDateTime']?date('d/m/Y H:i:s', strtotime($qry['loginDateTime'])):'N/A'); ?></td>
        <td><?php echo $qry['State'];?></td>
        <td><?php echo $q['Name'];?></td>
      </tr>
    <?php
       }
    }
    
    echo '</table>';
    ?>

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ok I tried this but no luck... Can anyone take a look?

    PHP Code:
    <?php

    /* connect to the mysql database and use a query to get the members info */

    include 'configure.php';
    include 
    'open.php';
    //navigation

    include("nav.php");

    //assign the bday variable.
    $month $_REQUEST['month'];

    $color1 "#CCFFCC";  
    $color2 "#BFD8BC";  
    $row_count 0;

    //birthday search
    $info mysql_query("SELECT * FROM `tblmembers` WHERE (MONTH(DateOfBirth) = '{$month}') AND `MemberApproved`='A'");
    ?>

    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
    Brithday Month: <select name="month">
      <option>Select A Month</option>
      <option value="1">January</option>
      <option value="2">February</option>
      <option value="3">March</option>
      <option value="4">April</option>
      <option value="5">May</option>
      <option value="6">June</option>
      <option value="7">July</option>
      <option value="8">August</option>
      <option value="9">September</option>
      <option value="10">October</option>
      <option value="11">November</option>
      <option value="12">December</option>
    </select>
    <input type="submit" value="Search">
    </form>

    <?php
    echo '<table width="700" border="0" cellspacing="0" cellpadding="0">
      <caption>MEMBERS - BIRTHDAYS SEARCH Download Details</caption>
      <thead>
      <tr>
        <th scope="col" class="left">FIRST NAME</th>
        <th scope="col">LAST NAME</th>
        <th scope="col">DATE OF BIRTH</th>
        <th scope="col">LAST LOGIN</th>
        <th scope="col" class="right">STATE</th>
      </tr>
      </thead>
      <tr class="odd">
        <td>On Hold </td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>'
    ;

    if (
    mysql_num_rows($info) < 1) {
    echo 
    '<tr valign="top">
      <td colspan="5>There are no members that match the query. Please go back and try again</td>
        </tr>'
    ;
    }

    else {
       while (
    $qry mysql_fetch_array($info)) {

    $rep $qry['rep_NBR'];

        
    $repInfo mysql_query("SELECT * FROM `tblrepresentatives` WHERE `rep_NBR`='$rep'");
        
    $q mysql_fetch_array($repInfo);
        
    $row_color = ($row_count &#37; 2) ? $color1 : $color2; 

    //create the layout
    ?>
    <link href="cs_style.css" rel="stylesheet" type="text/css" />

      <tr valign="top">
        <td bgcolor="$row_color" ><?php echo $qry['FirstName']; ?></td>
        <td bgcolor="$row_color"><?php echo $qry['LastName']; ?></td>
        <td bgcolor="$row_color"><?php echo date('d/m/Y'strtotime($qry['DateOfBirth'])); ?></td>
        <td bgcolor="$row_color"><?php echo($qry['loginDateTime']?date('d/m/Y H:i:s'strtotime($qry['loginDateTime'])):'N/A'); ?></td>
        <td bgcolor="$row_color"><?php echo $qry['State'];?></td>
        <td bgcolor="$row_color"><?php echo $q['Name'];?></td>
      </tr>
    <?php
    $row_count
    ++;

       }

    }

    echo 
    '</table>';
    ?>

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Code:
    &#37; 2)
    should be % 2
    Last edited by Nightfire; 04-03-2007 at 10:19 AM. Reason: GRRRR keeps getting converted

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You are also better off putting this into CSS (the color).

    PHP Code:
    ...
    $rowColor $rowCount "odd" "even";
    echo 
    '<tr class="' $rowColor '">';
    ... 
    Code:
    tr.odd {
        background-color : #CCFFCC;
    }
    
    tr.even {
        background-color : #BFD8BC;
    }
    Remember, PHP produces your HTML, HTML contains your information and CSS handles what it looks like.

  • #6
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I always found a simpler method so you dont have to use count

    PHP Code:
    $class $class == "odd" "even" "odd"

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    But you are repeating a string!

    Just kidding

    There's many ways of doing it. I was mostly using what he had already in there.


  •  

    Posting Permissions

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