Jump to content

$page->sort returns wrong index


gebeer
 Share

Recommended Posts

Hello, on a brandnew 3.0.35 install, I get wrong results for $page->sort.

My structure is like

Home
 -level1-1
  -level2-1
  -level2-2
  -level2-3
 -level1-2
 -level1-3

When I do 

foreach($homepage->children as $child) {
    echo $child->sort;
}

It gives me: 3, 4, 5 where it should be 0, 1, 2.

When I loop through the children's children, the values are correct

foreach($homepage->children as $child) {
    foreach($child->children as $ch) {
        echo $ch->sort;
    }
}

returns 0, 1, 2.

 

On level1 there never where more than 3 pages. So I don't know why it possibly could be starting with 3 instead of 0.

Is there any way to recreate the sort index? I tried moving level1 pages around in admin but that didn't change anything.

EDIT: I also tried $homepage->children('sort=sort'). Same results.

Link to comment
Share on other sites

Does $page even have a sort property? If you want to get the index of the $child you could also check its position in the PageArray using getItemKey(). Something like this:

$children = $homepage->children;
foreach($children as $child) {
	echo $children->getItemKey($child);
}

 

  • Like 1
Link to comment
Share on other sites

On 12/10/2016 at 4:02 AM, gebeer said:

Hello, on a brandnew 3.0.35 install, I get wrong results for $page->sort.

I can confirm. Looking at the Pages table in the DB there are no Home page children with sort values of 0, 1 or 2. Maybe raise a GitHub issue for this?

 

19 hours ago, arjen said:

Does $page even have a sort property?

It does. From the API reference...

$page->sort 	int 	Sort order of this page relative to siblings (applicable when manual sorting is used). 

 

 

Link to comment
Share on other sites

It's already like this in the installer sql script, so it shouldn't be much trouble to change the property for all pages with parent 1. I wonder, though, in which circumstances this would be an issue, since the sort order is still correct. So if anything, I'd regard this as a feature request and not a bug. Just my 2 cents.

  • Like 1
Link to comment
Share on other sites

@arjen

When I was looking 2 days ago I couldn't find it either.

@BitPoet

I agree. In the docs it says only sort order not index and we can use some PHP to get the index.

This code works for me:

foreach ($homepage->children as $key => $child) {
    $sortIndex = $key; // or $key + 1 if you don't want to start with 0
}

 

  • Like 2
Link to comment
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
 Share

×
×
  • Create New...