Henrik Posted June 25, 2016 Share Posted June 25, 2016 How can I identify the current position of a page in a page array? I just want to know if the position is odd or even, to alternately place an image left or right. Lets say I have this code: if($page->children) { foreach($page->children as $child) { // ... if($child->... position is odd ...) { echo "<div class='image_left'> ..."; } if($child->... position is even ...) { echo "<div class='image_right'> ..."; } // ... } } How can I identify if the current position of the page in the page array is odd or even? Thank you Link to comment Share on other sites More sharing options...
arjen Posted June 25, 2016 Share Posted June 25, 2016 Hey Hendrik, In this thread you'll find different solutions. No pun intended and I don't know if you've searched, but next time you could try Google first. The forum is loaded with great examples. Try some variations on keywords. 2 Link to comment Share on other sites More sharing options...
diogo Posted June 25, 2016 Share Posted June 25, 2016 See Arjen's post for more info, but for your case would be more than enough to use the PHP classical way: $i = 1; foreach($page->children as $child) { if($i % 2 == 0) { // even } else { // odd } $i++; } 2 Link to comment Share on other sites More sharing options...
Henrik Posted June 25, 2016 Author Share Posted June 25, 2016 Thanks, Arjen for your help! I found a solution in your post: I guess this is developer-basics. Now I learned this one too. Thank you! And yes, you are right. I've indeed searched, but not so thoroughly I probably should. I'm a little bit in a hurry right now. Sorry and Thanks again! @diogo: Thank you too Didn't saw your post. This was what I did. 2 Link to comment Share on other sites More sharing options...
arjen Posted June 25, 2016 Share Posted June 25, 2016 No problem mate. Glad you got it working. 2 Link to comment Share on other sites More sharing options...
LostKobrakai Posted June 25, 2016 Share Posted June 25, 2016 There's not even the need to manually count the number. foreach($page->children->getValues() as $key => $child) { if($key % 2 == 0) { // even } else { // odd } } 5 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now