Skip to content

Instantly share code, notes, and snippets.

@ifthenelse
Created July 9, 2012 09:47
Show Gist options
  • Save ifthenelse/3075390 to your computer and use it in GitHub Desktop.
Save ifthenelse/3075390 to your computer and use it in GitHub Desktop.
A function to set CSS classes to an html element such as "first", "last", "odd", "even" and "active"
<?php
/* Sets classes such as "first", "last", "odd", "even" and "active"
* @param $articles An array of articles to count
* @param $index The index to watch for
* @param $currentArticle The index of the current article
* @return string
**/
function setCSSClasses($index = null, $articles = null, $currentArticle = null) {
return ((is_int($index) && ($index == 1)) ? " first" : "").(((is_int(count($articles))) && ($index == count($articles))) ? " last" : "").((is_int($index) && ($index % 2 == 0)) ? " even" : " odd").(($index == $currentArticle)) ? " active" : "");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment