Posted By

FatFolderDesigner on 07/07/11


Tagged

php data awesome simple


Versions (?)

Simple inline PHP alternating rows


 / Published in: PHP
 

URL: http://fatfolderdesign.com/267/css/super-simple-inline-php-alternating-rows

Traditionally when you have lost lists of dat you want to alternate rows for usability. The first thing everybody makes to fix this is a simple if statement loop, but this adds a number of lines and is hardly a clean solution. Additionally it required you to have a “counter” or “switch” variable, usually placed at the bottom of the loop, overall very messy.

I had a boss who thought he was on the cutting edge by compressing the traditional if statement into a shorthand version (saving 2-3 lines of code), but the system was still using a $class variable and still requires a counting or switch variable. It was still a mess and added clutter to the code.

Earlier today I was working on outputting client records forafully customized application. Wanting to avoid such messy code as I looked for a better solution and with a little tweaking and playing I came up with this.

I've also included a simple example of how it's used in action. If you have any questions or concerns let me know either here or at the link (there's some more info about this method there too if your intreated).

  1. ($alt!=' class="alt" ' ? $alt=' class="alt" ' : $alt='')
  2.  
  3. /* implementation */
  4.  
  5. while($r=mysql_fetch_array($q)){
  6. echo '<tr '.($alt!=' class="alt" ' ? $alt=' class="alt" ' : $alt='').'><td>'.$r['firstname'].' '.$r['lastname'].'</td></tr>';
  7. }

Report this snippet  

You need to login to post a comment.