Jump to content

Group pages by field?


OpenBayou
 Share

Recommended Posts

https://processwire.com/talk/topic/2047-group-by/#comment-19162

Got it somewhat working.

<?php $pa = $pages->find("has_parent=/promos/"); $groups = array(); foreach($pa as $p) $groups["$p->podcast_shows"][] = $p->id;?>

<?php echo "<ul>";
foreach($groups as $key => $gr){
   $count = count($gr);
   echo "<li>$key";
   if($count){
       echo "<ul>";
       foreach($gr as $key => $pid){
           $r = $pages->get($pid);
           echo "<a href='$r->url'>$r->title</a>";
       }
       echo "</ul>";
   }
   echo "</li>";
}
echo "</ul>";?>

The problem is this line    echo "<li>$key"; It shows the page ID and I need to show the title. I tried '$key->title' and that didn't work.

  • Like 1
Link to comment
Share on other sites

3 hours ago, OpenBayou said:

Tried '$p->podcast_shows->title' that didn't work.

Not meaning to be too critical here, but it would be really helpful if you could start providing a little more info. Just telling us it didn't work makes it a lot more difficult to help. It would be great if you could do a little debugging first. Do you have debug mode turned on? What about TracyDebugger - do you have that installed and the debug bar turned on? Those will both help you get more info on why something isn't working.

I seem to recall in another one of your recent posts you had an issue with a multi vs single page field. Remember that you can't get the title from a multi page field like that - you need to specify which one of the selected pages. It might be as simple as $p->podcast_shows->first()->title but without more info, it's hard for us to know.

  • Like 4
Link to comment
Share on other sites

Ok,

I have TracyDebugger on, have no idea what I'm looking at. Did a var dump on $key and all I got was int(1155). When do '$p->podcast_shows->title' I get internal errors.

I'm sorry if I'm not making any sense. I'm not used to ProcessWire, I'm used to WordPress and I though it would be great idea to diversify my portfolio using different CMSs. I had a deadline on this last Friday and I'm working on little sleep and I can't think properly. And I never done something this complex like grouping posts and I'm not a good programmer, all I do is search for my problem and see if someone has the same the same problem and hope there's a solution. If there is, I try my best to get it working that satisfies what I need to accomplish.

Link to comment
Share on other sites

1 minute ago, OpenBayou said:

I'm not a good programmer

That's a relative thing - we could all say that relative to someone like Linus Torvalds. The key thing is that you are trying :)

Can you confirm that podcast_shows is in fact a Page field? Is it set to multiple or single? Did you try my suggestion of ->first()->title?

  • Like 3
Link to comment
Share on other sites

Here is an example of grouping news items under date headings according to a "post_date" datetime field. The headings are the dates (formatted as per the "Date Output Format" specified in the datetime field settings), unless the date equates to "Today" or "Yesterday", in which case those strings are used.

You should be able to adapt this to your needs.

$news_items = $pages->find("template=news_item, sort=-post_date");
$previous_item_date = '';
foreach($news_items as $news_item) {
    if($news_item->post_date !== $previous_item_date) {
        $timestamp = $news_item->getUnformatted('post_date');
        if($timestamp >= strtotime('today')) {
            $date = 'Today';
        } elseif($timestamp >= strtotime('yesterday')) {
            $date = 'Yesterday';
        } else {
            $date = $news_item->post_date;
        }
        echo "<h3>$date</h3>"; // output the date heading
    }
    echo "<p>$news_item->title</p>"; // output whatever fields of the item
    $previous_item_date = $news_item->post_date;
}

 

  • Like 2
Link to comment
Share on other sites

FIXED IT!

Changed

echo "<li>$key";

to

echo $pages->get($key)->title;

And it displays the title of the show. Thanks everyone for helping and understanding me. This is the final question as the project I've been working on is now complete. I'll post what I did sometime next month.

Thanks again!

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...