Jump to content

How to check if page has values associated from a PageTable field


alexcapes
 Share

Recommended Posts

Hi,

Working on a festival site which has artists pages and area pages.

On the artist page you can associate area pages with that particular artist by way of a PageTable field.

On the line up page you can then filter the artists by area. As shown below:

<select name="area" onchange="this.form.submit()">
                <option value="">All Areas</option>
                <?php
                foreach($pages->find("template=area_page") as $a) {
                  $select = $a->name == $input->get->area ? " selected=\"selected\"" : "";
                  echo "<option$select value=\"{$a->name}\">{$a->title}</option>";
                }
                ?>
              </select>

All working fine but I'd like to be able to only show the areas that actually have artists associated with them. ie. currently some areas are empty so shouldn't appear in the dropdown.

I'm sure there must be an easy way to check if the areas have artists but I can't think how to do it.

Any help would be hugely appreciated as always.

Link to comment
Share on other sites

Pagefields are one way connections, so there's no way for "get me all areas which are at least in one pagefield", but there is "get me all pages of those pagetables".

$artists = $pages->find("template=artist");

$used_areas = new PageArray();

foreach($artists as $artist){
  $used_areas->import($artist->areas); //areas is the pageTable;
}

$used_areas = $used_areas->unique(); // Remove duplicates

Edit: or this:

foreach($pages->find("template=area_page") as $a) {
  if($pages->get("template=artist, areas=$a")->id){
    // Found a page with this area so it's used
  }
}
  • Like 1
Link to comment
Share on other sites

foreach($pages->find("template=area_page") as $a) {
  if($pages->get("template=artist, areas=$a")->id){
    // Found a page with this area so it's used
  }
}

^ this worked perfectly.

The one-way connection of a PageTable field was the thing that threw me, I was trying to work a way back but couldn't do it.

Thank you!

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

×
×
  • Create New...