We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Looping through data using if and mod

Development and Programming

Mat's avatar
Mat
11 posts
14 years ago
Mat's avatar Mat

I’m trying to loop through entries and on every 5th one change a class in a li tag,if I use:

<?php
     for($i=0;$i<10;$i++){ 
?>

<?php 
     if($i % 5){
?>
<li class="start">
    {title}
</li>
<?php  }else{ ?>
  <li class="end">
    {title}
  </li>
<?php } ?>
<?php } ?>

So basically this just doesn’t work :( I do get that it wont work with entries, it is a loop already, but how can I do the above in EE? Thanks

       
Mat's avatar
Mat
11 posts
14 years ago
Mat's avatar Mat

ok so I figured I can use switch but how can I check the value of switch in a if statment.

       
the3mus1can's avatar
the3mus1can
426 posts
14 years ago
the3mus1can's avatar the3mus1can

You should not have to use a conditional:

<li class="{switch="end|end|end|end|start"}">
       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Are you referring to EE’s {switch} tag? I’m not sure I understand the question - but you can use the {switch} tag like this:

<li class="{switch='one|two|three|four|five'}">

In a 10 entry result, that would print the word “one” on the 1st and 6th entry, the word “two” in the 2nd and 7th entry, etc.

That in mind, you can leave the first 4 empty to just have something printed each 5th entry:

<li class="{switch='||||five'}">

Edit: I forgot to hit submit then noticed you got an answer already 😊

       
Mat's avatar
Mat
11 posts
14 years ago
Mat's avatar Mat

Thanks Erik & the3mus1can all working perfectly now.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.