Jump to content

Return current position of a page in page array


Recommended Posts

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 post
Share on other sites

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.

  • Like 2
Link to post
Share on other sites

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++;
}

 

  • Like 2
Link to post
Share on other sites

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. 

  • Like 2
Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By kalimati
      Processwire allows us to define predefined tags for images. What if I want to use a repeater field on the page for the purpose? How can I use values of this repeater field as the list of available tags for image fields?
      Thanks.
    • By jploch
      Hey guys! This is probably a total newbi question, but I cant figure it out myself.
      I use two links to get to the previous or next page. When the user is on the last page, I want the next link to show the first page.
      When the user is on the first page I want the previous link to show the last page. How can I do this with the API?
      This is my second site with PW, so Iam still trying to figure things out. Here is my code:
      <ul id="nav">   <li class="nav-left"><a href="<?php echo $page->prev->url; ?>" class="arrow"><img src="<?php echo $config->urls->templates?>img/site/arrow_left.svg"></a></li>   <li class="nav-right"><a href="<?php echo $page->next->url; ?>" class="arrow"><img src="<?php echo $config->urls->templates?>img/site/arrow_right.svg"></a></li>   </ul> Thx!
    • By Vineet Sawant
      Hello everyone,
      I'm creating a large scale application which will be used by thousands of people. Part of of the application requires storing temporary information about user's activities. For example, if user comments on certain topic, notification of same should be given to OP and people following the topic.
      I thought I'd create a subpage with a repeater field having few fields like user id, timestamp & notification status, read/unread (I just don't see why using page table is better in this case, if someone can enlighten me, that'd be great.).
      But I've read that every repeater item is same as new page, so I'm really confused whether I should just start keeping temporary pages or use a page with repeater field.
      But for every topic, there can be hundreds of notifications, which I think will be a problematic situation in future. So I am looking for any kind of temporary storage that will not included creating pages.
      Writing logs to file could be done but i'm not sure how good solution is that and how reliable it'll be.
      I'd really like a good conversation with someone who understands PW, so I'm here.
      Thank you all.
×
×
  • Create New...