Return to Snippet

Revision: 48670
at July 7, 2011 12:24 by FatFolderDesigner


Initial Code
($alt!=' class="alt" ' ? $alt=' class="alt" ' : $alt='')

/* implementation */

while($r=mysql_fetch_array($q)){
    echo '<tr '.($alt!=' class="alt" ' ? $alt=' class="alt" ' : $alt='').'><td>'.$r['firstname'].' '.$r['lastname'].'</td></tr>';
}

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

Initial Description
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).

Initial Title
Simple inline PHP alternating rows

Initial Tags
php, data, simple

Initial Language
PHP