Skip to content

Instantly share code, notes, and snippets.

@robwilkerson
Created November 2, 2011 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwilkerson/1334183 to your computer and use it in GitHub Desktop.
Save robwilkerson/1334183 to your computer and use it in GitHub Desktop.
PHP: Adding (even|odd), first & last classes to markup in a foreach loop.
<ul>
<?php $c_schools = count( $district['School'] ) ?>
<?php foreach( $district['Plan'] as $i => $plan ): ?>
<?php $classes = array( $i % 2 == 0 ? 'odd' : 'even' ) # Adjusted for 0-based indexing ?>
<?php array_push( $classes, $i == 0 ? 'first' : false ) ?>
<?php array_push( $classes, $i == $c_schools - 1 ? 'last' : false )?>
<li class="<?php echo join( ' ', array_filter( $classes ) ) ?>">
PLAN NAME HERE
</li>
<?php endforeach; ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment