unemployment Posted February 16, 2011 Share Posted February 16, 2011 I need to add a class to the div at the end of my for each. How do I say if the function breaks, add in class="lastmessage" <?php $counter = 0; foreach ($conversations as $conversation) { if(++$counter == 5) break; ?> <div class="(add class here)"> <a href="blah"><?php echo blah; ?></a> </div> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/227908-add-class-at-end-of-for-each/ Share on other sites More sharing options...
unemployment Posted February 16, 2011 Author Share Posted February 16, 2011 I need to add a class to the div at the end of my for each. How do I say if the function breaks, add in class="lastmessage" <?php $counter = 0; foreach ($conversations as $conversation) { if(++$counter == 5) break; ?> <div class="(add class here)"> <a href="blah"><?php echo blah; ?></a> </div> <?php } ?> Edit: I don't just want to say to add it at the end of the foreach in the event that their are less than 5 messages. Quote Link to comment https://forums.phpfreaks.com/topic/227908-add-class-at-end-of-for-each/#findComment-1175202 Share on other sites More sharing options...
Pikachu2000 Posted February 16, 2011 Share Posted February 16, 2011 That logic seems a bit funky. What are you trying to achieve? Will there only ever be 5 elements in the array? A better explanation would help. Quote Link to comment https://forums.phpfreaks.com/topic/227908-add-class-at-end-of-for-each/#findComment-1175203 Share on other sites More sharing options...
unemployment Posted February 16, 2011 Author Share Posted February 16, 2011 That logic seems a bit funky. What are you trying to achieve? Will there only ever be 5 elements in the array? A better explanation would help. I have a list of messages in sidebar. The most that will display is 5 of them. I want to set a border-bottom to the last element in the foreach loop. Sometimes you may have less than 5 messages. If your inbox only has 3, 3 will be displayed. Does that help? Quote Link to comment https://forums.phpfreaks.com/topic/227908-add-class-at-end-of-for-each/#findComment-1175208 Share on other sites More sharing options...
Pikachu2000 Posted February 16, 2011 Share Posted February 16, 2011 OK. Something like this, then? $i = 1; foreach ($conversations as $conversation) { $class = $i === 5 ? 'class="class_name"' : ''; echo "<div $class>$conversation</div>"; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/227908-add-class-at-end-of-for-each/#findComment-1175217 Share on other sites More sharing options...
KevinM1 Posted February 16, 2011 Share Posted February 16, 2011 Just use a normal for-loop: $numConversations = count($conversations); for($i = 0; $i < $numConversations; ++$i) { if(($i + 1) == $numConversations) // adding 1 to the index because arrays are ZERO-indexed, so $conversations[0] is your first one { // add your CSS class } } Quote Link to comment https://forums.phpfreaks.com/topic/227908-add-class-at-end-of-for-each/#findComment-1175220 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.