Created
November 2, 2011 16:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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