Jump to content


Photo

Sorting results of find() by admin order


  • Please log in to reply
6 replies to this topic

#1 Lars282

Lars282

    Newbie

  • Members
  • PipPipPip
  • 76 posts
  • 3

  • LocationLondon, UK; Frankfurt, Germany

Posted 28 May 2012 - 04:14 PM

Hey!

I just tried this:

... you may still tell the $pages->find() to sort by the parent's order by specifying "sort=sort" in your selector. This is the same as saying "sort by whatever order I dragged the pages to in the admin." But note that if the results have multiple parents, the resulting order isn't likely to be useful.


Result: http://aglobalvillag.../global-health/

The grey bars are the titles of the parent pages (to be exact, the parents of parents) ... somehow this does not seem to be sorted as in the admin ... ?? If it was as in admin, surely they should be grouped by common parents?

How can I group all the results by the parent pages? I.e. how can I make the find() return the pages as they appear in admin order?

Thanks,

Lars

#2 Lars282

Lars282

    Newbie

  • Members
  • PipPipPip
  • 76 posts
  • 3

  • LocationLondon, UK; Frankfurt, Germany

Posted 28 May 2012 - 04:20 PM

Is there a way of using sort("property.subproperty")? If so, how? Don't really understand what it says on the cheatsheat!

Thanks!

#3 MadeMyDay

MadeMyDay

    Sr. Member

  • Members
  • PipPipPipPip
  • 139 posts
  • 125

Posted 29 May 2012 - 01:37 AM

Can you show your code and your intended result?

#4 Lars282

Lars282

    Newbie

  • Members
  • PipPipPip
  • 76 posts
  • 3

  • LocationLondon, UK; Frankfurt, Germany

Posted 29 May 2012 - 08:05 AM

The intended result is that above is displayed ordered by say Issue 7, then Issue 6 etc ... and not in apparently random order.

Code:

  foreach ($pages->find("$page->topic_name=1") as $p) {
//Check wether the parent title from last one was different. If so, display new heading.
   if ($i != $p->parents->get("template=issue")->title) {
	$i = $p->parents->get("template=issue")->title;
	echo '<div class="topic-issue">'.$i.' &ndash; '.$p->parents->get("template=issue")->issue_date.'</div>';
   }
   echo '<div class="title"><a href="' . $p->path . '">' . $p->article_title;
   if ($p->article_subtitle) {
	echo ': ' . $p->article_subtitle . '</a></div>';
   }
   else {
	echo '</a></div>';
   }
   echo '<div class="author">' . $p->article_author_name . ', ' . $p->article_author_affiliation . '</div>';
   }
  unset($p);

How can I order this by template=issue as in admin and then children as in admin?

Screenshot of my page tree Posted Image

#5 MadeMyDay

MadeMyDay

    Sr. Member

  • Members
  • PipPipPipPip
  • 139 posts
  • 125

Posted 29 May 2012 - 08:13 AM

Well, I think you have to itereate twice. First for the parents (issues), then for the articles. Like (pseudocode)

$issues = $pages->find("template=issue,sort=sort")
foreach $isssues as $issue{
  echo $issue->title;
  $articles = $issue->find("topic_name=1,sort=sort")
  foreach $articles as $article
    echo $article->article_subtitle;
  }
}


#6 ryan

ryan

    Hero Member

  • Administrators
  • 5,812 posts
  • 3141

  • LocationAtlanta, GA

Posted 29 May 2012 - 11:33 AM

Since the structure is already there, you probably want to use that structure and you won't need to worry about sort order at all, since the sort order is implicit in the structure. I'll adapt MadeMyDay's good example towards the structure from your screenshot. This may not be exactly what you are trying to achieve, but maybe it's close. But the point I'm trying to get across is just that it's better to use this structure for retrieving pages when your output results need it. It's also more efficient to use the structure rather than issuing separate find() queries to pluck stuff out of it arbitrarily.

$issues = $pages->get("/journal/")->children(); 
foreach($issues as $issue) {
    echo $issue->title . '<br />';
    $articles = $issue->children();
    foreach($articles as $article) {
        echo ' - ' . $article->title . '<br />';
    }
}


#7 MadeMyDay

MadeMyDay

    Sr. Member

  • Members
  • PipPipPipPip
  • 139 posts
  • 125

Posted 29 May 2012 - 11:36 AM

It's also more efficient to use the structure rather than issuing separate find() queries to pluck stuff out of it arbitrarily.


hehe, when posting my answer the structure screenshot wasn't there. If there are only children of a kind it is easier/better to use ->children() ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users