terry Posted January 3, 2014 Share Posted January 3, 2014 (edited) Is there any built-in field that would return a page's index/sorted number? As far as I know what I'm looking for doesn't strictly fall under pagination (apologies if I'm incorrect about this).I currently have a parent page with n child pages, these are sorted by date (custom date field). e.g.Gamma (2014)Alpha (2013)Omega (2012) Beta (2011) So on page Gamma, I want to be able to return the int 1. Alpha, return int 2, Omega, return int 3, etc. (These would obviously change if the date field was modified).Is there any simple way to do this?Thanks.EDIT: Not sure if this was in the right forum. Managed to get it working, although there may be a simpler way, code for anyone interested: $a = $page->siblings('include=all'); foreach($a as $b) { $i++; if ($b == $page) { echo $i; } } Edited January 3, 2014 by terry Link to comment Share on other sites More sharing options...
Soma Posted January 3, 2014 Share Posted January 3, 2014 Since you have custom sorting, no there's no index. $page->sort would be the sort index from the tree but that would be different once you sort on a custom field possible with ASC or DESC. I'm not sure I understand the logic/context of your "if on Gamma, return 1 for Alpha". Also I'm not sure why you have "include=all" in your query. Just will return all pages including those that user would have no access, unpublished pages etc.. Your script would be one way to do numbering, but $i isn't defined and you would get a notification. $a = $page->siblings('include=all'); $i = 0; foreach($a as $b) { $i++; if ($b == $page) { echo $i; } } So this would give you, if on Gamma page "1" If on Alpha page: "1" Is that what you want? Another way to number pages is using a $key in foreach (zero based): foreach ($page_array as $key => $p) { echo ($key + 1); } Link to comment Share on other sites More sharing options...
kongondo Posted January 3, 2014 Share Posted January 3, 2014 I'm not sure I understand the logic/context of your "if on Gamma, return 1 for Alpha". ====== SNIP ========= So this would give you, if on Gamma page "1" If on Alpha page: "1" Is that what you want? Not exactly...I don't think so. I think, in essence, terry wants to sort by the custom date field (the 20XX) and...the most recent child page to be assigned Page number 1, etc, etc (ASC). So: Gamma (2014) - when I view this page, it will have integer 1 (i.e. page 1) Alpha (2013) - ....page 2 Omega (2012)-.....page 3 Beta (2011) - ......page 4 If the custom dates change..... Alpha (2014) - page 1 Omega (2013) - page 2 Gamma (2012) - page 3 Beta (2011) - page 4 Or so I think... Link to comment Share on other sites More sharing options...
Pete Posted January 4, 2014 Share Posted January 4, 2014 terry - your amended code would work with any array, it's not ProcessWire specific and is just a case of having a counter that increments as you iterate through an array. I would set $i = 0; before your foreach though just so it knows what $i is before you start incrementing it - with stricter error reporting turned on it would probably throw you an error otherwise. The code is puzzling though as it doesn't really relate to anything in the PageArray. Since you can sort based on date ascending or descending, by title asc and desc etc etc just having this counter doesn't seem to achieve much as $i doesn't relate to anything in the array - it's just a running count of how many items there are. Is it being used for anything in particular? 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