Jump to content

Zebra Tables with PHP & MySQL


kyleldi

Recommended Posts

I'm creating a staff directory that I'd like to use alternating background colors (odd/even) to make it easier to read.  All of the information is stored in sql, which I can pull to the page with a repeat function, but I'm not sure how to get the zebra effect to alternate with the output.  Is there any easy way to do this?  I'm using CSS to change the background of the table itself.

 

<table width="100%" border="0" cellpadding="1" cellspacing="0" class="staff_b">
        <tr>
          <td width="35%"><div class="staff_h">Name</div></td>
          <td width="35%"><div class="staff_h">Department</div></td>
          <td width="30%"><div class="staff_h">Extension</div></td>
        </tr>
        <tr class="staff_e">
          <td>1</td>
          <td>2</td>
          <td>3</td>
        </tr>
        <tr class="staff_o">
          <td>4</td>
          <td>5</td>
          <td>6</td>
        </tr>
      </table>

Link to comment
Share on other sites

Yeah it's pretty straight forward. First define a variable you can switch between odd/even (true/false):

 

$alt = 0;

 

Within the loop echo the right class name:

 

<tr class="<?php echo ($alt) ? 'staff_o' : 'staff_e'; ?>">

 

 

Then before you close the loop switch the variable:

 

$alt = ($alt) ? 0 : 1;

Link to comment
Share on other sites

Thank you for the valuable information!  I put together the code (below), but it seems as though the alternating does not take place.  Instead it just outputs each TR class at

<tr class="staff_e">

 

 

<?php do { ?>
        <?php $alt = 0; ?>
        <tr class="<?php echo ($alt) ? 'staff_o' : 'staff_e'; ?>">
          <td><?php echo ucwords($row_rs_staff['name']); ?></td>
          <td><?php echo $row_rs_staff['dept']; ?></td>
          <td><?php echo $row_rs_staff['ext']; ?></td>
        </tr>
        <?php $alt = ($alt) ? 0 : 1; ?>
        <?php } while ($row_rs_staff = mysql_fetch_assoc($rs_staff)); ?>

Link to comment
Share on other sites

Hi

 

I normally use a line number variable, and then mod it to alternate the table rows class.

 

$LineNo = 0;
while ($row = mysql_fetch_array($rs))
{
echo "<tr class='RowClass".(($LineNo++) % 2)."'><td>".$row['SomeVariable']."</td></tr>";
}

 

This way the class will alternate between RowClass0 and RowClass1.

 

All the best

 

Keith

Link to comment
Share on other sites

Thank you for the valuable information!  I put together the code (below), but it seems as though the alternating does not take place.  Instead it just outputs each TR class at

<tr class="staff_e">

 

 

<?php do { ?>
        <?php $alt = 0; ?>
        <tr class="<?php echo ($alt) ? 'staff_o' : 'staff_e'; ?>">
          <td><?php echo ucwords($row_rs_staff['name']); ?></td>
          <td><?php echo $row_rs_staff['dept']; ?></td>
          <td><?php echo $row_rs_staff['ext']; ?></td>
        </tr>
        <?php $alt = ($alt) ? 0 : 1; ?>
        <?php } while ($row_rs_staff = mysql_fetch_assoc($rs_staff)); ?>

 

You need to define $alt outside of the loop, otherwise at the start you set it back to the same value every time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.