Jump to content

Loop Through Selected Pages and Display Data From Parents and Grandparents (Page Tree?)


Jim Bailie
 Share

Recommended Posts

After an hour or so, I'm still not sure how to start in on this.

How to loop through selected pages displaying some of their parent and grandparent data as well.

In this case we have the following page structure along with the search results yielding all function types "Dinner Plated" along with all the function types of "Venue River"

My concern is that looping through the result set and getting the parent/grandparent info in each iteration would get unwieldy at some point.

I'm grateful if anyone can talk me onto the right path.

EDIT: Ok, I found this which may be my starting point: https://processwire.com/api/ref/page/parents/

Venue Location Blue
    Venue Rock
        Function Type - Breakfast
        Function Type - Lunch
        Function Type - Dinner Plated
        Function Type - Dinner Buffet
    Venue River
        Function Type - Breakfast
        Function Type - Lunch
        Function Type - Dinner Plated
        Function Type - Dinner Buffet
Venue Location Red
    Venue South
        Function Type - Breakfast
        Function Type - Lunch
        Function Type - Dinner Plated
        Function Type - Dinner Buffet
    Venue North
        Function Type - Breakfast
        Function Type - Lunch
        Function Type - Dinner Plated
        Function Type - Dinner Buffet
Venue Location Green
    Venue Pool
        Function Type - Breakfast
        Function Type - Lunch
        Function Type - Dinner Plated
        Function Type - Dinner Buffet
    Venue Restautant
        Function Type - Breakfast
        Function Type - Lunch
        Function Type - Dinner Plated
        Function Type - Dinner Buffet
        
Display Search Results:

Venue Location Blue
    Venue River
        Function Type - Breakfast
        Function Type - Lunch
        Function Type - Dinner Plated
        Function Type - Dinner Buffet
Venue Location Red
    Venue South
        Function Type - Dinner Plated
    Venue North
        Function Type - Dinner Plated
Venue Location Green
    Venue Pool
        Function Type - Dinner Plated
    Venue Restautant
        Function Type - Dinner Plated
   

 

Link to comment
Share on other sites

Disclaimer: As someone new to PW, I wouldn't consider this an answer but a little help on moving forward.

Check out the selector section of docs: Using selectors in ProcessWire CMS

An example find - would get items that match River on the "parent" and items that match Dinner-Plated on the child.

$results = $pages->find('template=venue-area|function-type, (function=River), (type=Dinner-Plated)');

There are endless possibilities with the selectors so that's just one way. Each result (say you're looping $result as $item) would have things like $item->parent, or $item->children. For example, you can do $item->parent->title.

I don't know if you're using repeaters, page refs or any of the fields so the selectors could vary wildly. Either way, that selector page has a lot of great details.

As for unwieldly results, hopefully a more seasoned person can add some guidance. It may be best to do more than one selector depending on the structure and complexity of searches. How many sections, search criteria and results are you expecting?

As a parting note, I would strongly suggest you have Tracy Debugger installed > Tracy Debugger (TracyDebugger) - ProcessWire Module. The page dumps alone are lifesavers.

 

  • Like 2
Link to comment
Share on other sites

I'd love to help, but I don't think I understand the question.

On 8/16/2023 at 4:32 AM, Jim Bailie said:

How to loop through selected pages displaying some of their parent and grandparent data as well.

This would be very easy:

<?php
foreach($pagearray as $page) {
  echo "<div>Parent: ".$page->parent->title."</div>";
  echo "<div>Grand-Parent: ".$page->parent->parent->title."</div>";
}

But I think that was not your question, was it?

I guess your code-block with all the venues has some meaning as well, but I can't imagine what exactly you are asking for. And I also don't understand what exactly you mean by things becoming "unwieldy" at some point?

If you are asking for some kind of search engine that's a whole other topic and can quickly become a lot more complex depending on what you need or want, but then again you'd have to be a lot more specific with your questions (at least for me to understand, maybe it's clear for others already).

In general looping through pages and requesting data from parent or grandparent pages via API means that you load pages into memory. That can be inefficient if you are dealing with lots of pages. That's why I have built RockFinder and later we got findRaw() in the core. Both can load lots of data with an efficient and fast SQL query rather than loading everything from PHP. That's especially handy if you work with tabular data like data grids or such where pagination and sorting is done on the client side.

For search results you could also add pagination and limit the number of results to 20 for example, then it shouldn't really matter if you load data via SQL directly or you loop some pages via PHP.

  • Like 1
Link to comment
Share on other sites

@bernhard Ugh! This seems pretty straightforward as well. I'll be digging back into this this afternoon so, thank you.

In a nutshell, the user is searching for a certain type of event (Breakfast, Dinner, Dinner and Show, etc). The result shows the desired events, no problem.

Well these events can be held at many different venues of course.

And these venues are scattered and grouped at many different locations.

So I have to return a result that loops through these event types, and shows what venue they're at and the location that the venue resides. Simple right?

But when I loop through these desired event types I need to show some images and data from the parent venues and grand-parent venue locations. So basically, what's the Processwire way of doing this?

I think I'm getting close though...

And thanks for the heads-up on Rock Finder! That may come in very useful with logging.

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