kyleldi Posted September 7, 2010 Share Posted September 7, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/212763-zebra-tables-with-php-mysql/ Share on other sites More sharing options...
Adam Posted September 7, 2010 Share Posted September 7, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/212763-zebra-tables-with-php-mysql/#findComment-1108244 Share on other sites More sharing options...
kyleldi Posted September 7, 2010 Author Share Posted September 7, 2010 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)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/212763-zebra-tables-with-php-mysql/#findComment-1108263 Share on other sites More sharing options...
kickstart Posted September 7, 2010 Share Posted September 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/212763-zebra-tables-with-php-mysql/#findComment-1108265 Share on other sites More sharing options...
Adam Posted September 7, 2010 Share Posted September 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212763-zebra-tables-with-php-mysql/#findComment-1108273 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.