Jump to content

Display page data on other page depending on a field


brandy
 Share

Recommended Posts

Hi!

This is my page tree in Processwire:

Home
- Hotels
 - - Hotel 1
 - - Hotel 2
 - - Hotel 3
...

Every hotel has its fields like name, adress and so on.
It also has a dropdown-field for the region, in which the hotel is located.

Now I want to make another page, where the hotel should appear if the region matches with the region in the hotel-data.
There should be a table where some regions are shown and below there should be the matching hotels.

Is it possible to link from that new page to an array of all children of "hotels" and if the region matches the hotel should displayed?

What is the right tag for that?

Link to comment
Share on other sites

It's pretty straight forward. Instead of defining textual options, you create a page for every selectable option. Preferably, you group them under their own parent, and you'll usually want to assign them their own template, both to make queries easier and so you can add more fields to the template later.

In your case, you can start with a (backend-)template "region", no need to add a PHP template file since the region pages are only for filling the dropdown.

You'll also want a template "regions" for the regions' parent page. This one will be the overview page you're trying to build and therefore need a PHP template.

In the first step, just create both templates with the minimum fields PW suggests (i.e. just a "title" field which will be the label in your dropdown).

Then create the pages.

Home
- Regions
- - Region 1
- - Region 2
- - ...

In your hotel template, add a page reference field for the region, and on the "Input" tab in the field's configuration, go to "Selectable pages". Under "Templates", select "region". Edit your hotels and assign a region to each.

Now you can easily iterate over all available regions in your regions.php template file, output the region's details and a list with the hotels for that region. A little PHP snippet as a starting point:

<?php namespace ProcessWire;

foreach($page->children() as $region) {
  echo "<h3>{$region->title}<h3>";
  echo "<ul>";
  $hotels = $pages->find("parent=/hotels/, region=$region");
  foreach($hotels as $hotel) {
    echo "<li><a href='{$hotel->url}'>{$hotel->title}</a></li>";
  }
  echo "</ul>";
}

 

  • Like 5
Link to comment
Share on other sites

Thanks for the page reference hint - this works fine.
But I forgot that I have another layer:

I have hotels with the regions-field - every hotel is has one region (made with page references).

But now I want to give some tour-recommendations with matching hotels in different regions - for example you can make a tour and sleep in two regions.
Now you should see for example:

Tour A:
Region 1
- Hotel 1
- Hotel 2
Region 2
- Hotel 1
- Hotel 2

Tour B:
Region 3
- Hotel 7
- Hotel 8
Region 4
- Hotel 3
- Hotel 4

The page reference with the regions is working in the tour-recommendations.
But I don´t know how to connect the region from the tour-recommendation with the region of the hotel, so that the hotel-data is shown.

Thanks a lot!

 

Link to comment
Share on other sites

I´ve tried once again, but I can´t find the right way.

As said I have made some pages with tour recommendations.

Recommendations
- recommendation 1
- recommendation 2
...

These recommendations consist of text fields for "kilometer" and "hohenmeter" and page reference fields for the regions where the hotels are.

The output with this one works:

						foreach($page->children() as $vorschlag) {
						  echo "<h3>{$vorschlag->title}<h3>
						  			<p>{$vorschlag->tour_start->title}</p>
						  			<p>{$vorschlag->tour_kilometer_1} km | {$vorschlag->tour_hohenmeter_1} HM</p>
									<p>{$vorschlag->tour_ubernachtung_1->title}</p>
									<p>{$vorschlag->tour_kilometer_2} km | {$vorschlag->tour_hohenmeter_2} HM</p>
									<p>{$vorschlag->tour_ubernachtung_2->title}</p>
									<p>{$vorschlag->tour_kilometer_3} km | {$vorschlag->tour_hohenmeter_3} HM</p>
						  			<p>{$vorschlag->tour_start->title}</p>";
                            

Now I want to have shown the hotels from the parent "hotels" - matching to the page reference field "tour_start", "tour_ubernachtung_1" and "tour_ubernachtung_2", which defines the region, where the hotel is.
I hope you understand.

I know it´s just a small adaption from the code form BitPoet, but I´m no sure about the find-part with region:

				<?php
				
						foreach($page->children() as $vorschlag) {
						  echo "<h3>{$vorschlag->title}<h3>
						  			<p>{$vorschlag->tour_start->title}<br/>";
						  			
									  $hotels = $pages->find("parent=/unterkuenfte/, region=$tour_start");
									  foreach($hotels as $hotel) {
									    echo "{$hotel->title}";
									  }
									  echo "</p>";
						  			
						  			
						echo "<p>{$vorschlag->tour_kilometer_1} km | {$vorschlag->tour_hohenmeter_1} HM</p>
									<p>{$vorschlag->tour_ubernachtung_1->title}</p>
									<p>{$vorschlag->tour_kilometer_2} km | {$vorschlag->tour_hohenmeter_2} HM</p>
									<p>{$vorschlag->tour_ubernachtung_2->title}</p>
									<p>{$vorschlag->tour_kilometer_3} km | {$vorschlag->tour_hohenmeter_3} HM</p>
						  			<p>{$vorschlag->tour_start->title}</p>";
				}?>

Thanks a lot!

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