Jump to content

PageArray Sort by ID entry


Tom.
 Share

Recommended Posts

Here is a difficult one for you guys. 

I'm wanting to do a page step sort of thing. I'm creating an online repository where everything will be relationship based. I've created a page and template that will hold "Journeys" these "Journeys" have a Journey start field and a Journey field.

When a page is hit with Journey_start matched it will look at the Journey field to look at what pages should follow on to that. This is then set in a session:

$journey = $pages->findOne("template=journey, journey_start=$page")->journey;
if($journey->count) { 
     $journey = $journey->prepend($page);
     $session->journey = (string) $journey;
}

What it will do from here is check if the current page is part of the session, if it isn't reset the session. This is because a page can be part of a Journey but also the start of a Journey. So if you go away from the intended Journey it will reset ready to start a new one. 

So once it's on the next Journey page it does the following: 

if($journey->count) { 
     $journey = $journey->prepend($page);
     $session->journey = (string) $journey;
} else {
     $journey = $pages->find("id=$session->journey");
}

However this will return a random order and not the order the id's are set. Throwing the pagination out:

$inArray = false;
foreach($journey as $item) {
    if($item == $page) {
        $prev = $journey->getPrev($item);
        $next = $journey->getNext($item);
        echo "<a href='$prev->url'>$prev->title</a>";
        echo "<a href='$next->url'>$next->title</a>";
        $inArray = true;
     }
}
if(!$inArray) {
     $session->journey = '';
}

Anyone got any ideas? Am I going about this all wrong?

I believe if I can use $pages->find to return the pages in the order of id's set, it should work. 

Link to comment
Share on other sites


$journeyArray = $journey->getArray();

$idArray = explode("|", $session->journey);

usort($journeyArray, function($a, $b) use ($idArray) {

$keyA = array_search($a->id, $idArray);

$keyB = array_search($b->id, $idArray);

return $keyA < $keyB ? -1 : 1;

});

$journey->setArray($journeyArray);

  • Like 3
Link to comment
Share on other sites

$journeyArray = $journey->getArray();
$idArray = explode("|", $session->journey);

usort($journeyArray, function($a, $b) use ($idArray) {
  $keyA = array_search($a->id, $idArray);
  $keyB = array_search($b->id, $idArray);

  return $keyA < $keyB ? -1 : 1;
});

$journey->setArray($journeyArray);

Thank you very much, that worked. I can't say I understand what's going off there, but it worked! I'll will take some time to research it and figure it out :-) 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...