Jump to content

Selectors: Get architects from city buildings


ptjedi
 Share

Recommended Posts

Hello everyone,

I am struggling here with a problem for I wish there is a more elegant solution. Picking up the Skyscrapers demo example, if I wanted to list all architects that had any kind of participation on a specific city, what would be the correct process?

I can always list all buildings, pick up their architects, store them in a variable and get rid of duplicates, but I am pretty sure PW has a more interesting approach to the problem. Can anyone help me out on this one?

Thanks!

Link to comment
Share on other sites

I can always list all buildings, pick up their architects, store them in a variable and get rid of duplicates, but I am pretty sure PW has a more interesting approach to the problem. Can anyone help me out on this one?

If we're using the skyscrapers example literally, like the demo site, then there is no structural connection between cities and architects since they are in different branches, not part of the same family:

/architects/

/cities/

So the only connection of an architect to a city is via a page reference field on a building. That means that somewhere, the process you described has to take place, like this:

$architects = new PageArray();
foreach($page->children as $building) {
 $architects->add($building->architects);
}
$architects->sort('title');
echo $architects->render();

On the individual architect pages, if you wanted to list the cities they are active in, you could do this:

$buildings = $pages->find("architects=$page");
$cities = new PageArray();
foreach($buildings as $building) {
 $cities->add($building->parent);
}
$cities->sort('title');
echo $cities->render();

In either case, you don't need to worry about duplicate removal is ProcessWire is already taking care of that for you.

Link to comment
Share on other sites

Thank you Ryan for your quick assistance. I've tried to reproduce what you suggest but with little success. My own example is similar to the Skyscrapper one that I mentioned, but I think it's better to explain everything from bottom-up:

This is a catalog that has 2 types of entities (templates): products and attributes. Both are pages (of course) and I have Dropdown menus inside products that let me choose from the attributes created outside the tree of pages. My tree looks like this:

  • Home
  • Page 1
  • Page 2
  • Catalog
    • Products
      • Product 1
      • Product 2
      • Product 3
      • ...

      [*]Types

      • Type Attribute 1
      • Type Attribute 2
      • ...

      [*]Areas

      • Area Attribute 1
      • Area Attribute 2
      • ...

So, for each product I can associate with a Type and multiple Areas.

What I need is: given the Area, list all Types that are selected inside that area's products. This, inside a page OUTSIDE the catalog, so I need to point everything in there.

Here's the code I am using:

$area = $pages->get("/catalog/area/")->find("title*=Hospital"); // gets page that contains "Hospital" in title (will return only one)

$products = $pages->get("/catalog/products/")->find("catalog_product_area=$area"); // lists all products that matches that attribute

$types = new PageArray();
foreach($products as $product) {
   $types->add($product->parent);
}
$types->sort('title');
echo $types->render();

The result shows the following:

Produtos
1
2
3
4
5
6
7
Next

Any idea?

Link to comment
Share on other sites

Not sure what exactly you need. Those numbers with the "next" looks like a pager? Is that coming from where? The $types->render() causes it? However this is not what you need, you just have to foreach the types and output them any way you like.

$types = new PageArray();
foreach($products as $product) {
 // get the types selected, assuming your page field is "catalog_product_type"
 // add() will import a set of page array or a single as of latest PW, if older PW you can
 // use import() method to import arrays
 $types->add($product->catalog_product_type);
}
echo "<ul>";
foreach($types->sort('title') as $type) {
 echo "<li>$type->title, $type->url, $type->id</li>";
}
echo "</ul>";
  • Like 1
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...