OrganizedFellow Posted August 8, 2013 Share Posted August 8, 2013 Can I get some help with my terrible PHP logic? I am attempting to generate alternating CSS classes, even and odd. So I wrote this little function: function doAlt($n) { if ($n % 2) { echo('odd'); }else{ echo('even'); } } I figured how to output like this: for ($i = 0; $i <= 10; $i++) { echo doAlt($i).$i."<br>"; } Resulting HTML is this: even0 odd1 even2 odd3 even4 odd5 even6 odd7 even8 odd9 even10 So far, so good! Here is where I inserted it into my code: <section> <ul> <?php $blogpages = $pages->get("/blog/"); $children = $blogpages->children; $children->prepend($blogpages); foreach($children as $child) { // my function to print even or odd for alternating CSS classes for($i=0;$i<=10;$i++){ echo "<li>"; echo "<h3>{$child->title}</h3> \n"; echo doAlt($i).$i; echo "{$child->snippet} \n"; echo "</li> \n"; } } ?> </ul> </section> The resulting HTML is each blog entry repeating multiple times, in this case $i=<10, so ten times. I did it like that to see how it would print/echo out before I stuck it in my CSS. How can I give each entry a different CSS class? Link to comment Share on other sites More sharing options...
nik Posted August 8, 2013 Share Posted August 8, 2013 You can use a slightly different form of foreach to get also the index on each iteration, like this: foreach($children as $i => $child) { echo doAlt($i) . $i; } That should do the trick. Without the inner foreach of course. 1 Link to comment Share on other sites More sharing options...
OrganizedFellow Posted August 8, 2013 Author Share Posted August 8, 2013 You can use a slightly different form of foreach to get also the index on each iteration, like this ... That should do the trick. Without the inner foreach of course. SUPERB! My PHP is still at a beginner level. Thank you! Regarding the function. I read it as: if $n is divisible by 2 echo('odd'); }else (if not){ echo('even'); But I expect if $n is indeed divisible by 2, then the first each should read even, not odd. Or am I reading it wrong? Link to comment Share on other sites More sharing options...
arjen Posted August 8, 2013 Share Posted August 8, 2013 $count = 1; foreach($children as $child) { $type = $count % 2 == 0 ? "even" : "odd"; echo $type; $count++; } No need for a function Or pure css: section ul li:nth-child(even) { /* Do stuff on even list items */ } Written in the browser, but you might get the idea. 2 Link to comment Share on other sites More sharing options...
nik Posted August 8, 2013 Share Posted August 8, 2013 Regarding the function. I read it as: if $n is divisible by 2 echo('odd'); }else (if not){ echo('even'); But I expect if $n is indeed divisible by 2, then the first each should read even, not odd. Or am I reading it wrong? You are reading it wrong . '%' is a modulus operator giving you the remainder of the division of its operands. For example 10 % 3 = 1 (10 = 3 * 3 + 1). So your first if actually means "if $n is NOT divisible by 2". 1 Link to comment Share on other sites More sharing options...
Soma Posted August 8, 2013 Share Posted August 8, 2013 jQuery: $('li:odd').addClass("odd"); done. 4 Link to comment Share on other sites More sharing options...
SiNNuT Posted August 8, 2013 Share Posted August 8, 2013 In my opinion CSS3 selectors (nth-child) are really the way to go for this kind of presentational stuff. Browser support is widespread ( http://caniuse.com/#search=nth ), but if you still care about <IE9 you're out of luck unless you use something like http://selectivizr.com/ or go with pure jQuery as Soma showed. 3 Link to comment Share on other sites More sharing options...
Recommended Posts