Jump to content

Grouping Unique Results / Categories and Subcategories, help?


Recommended Posts

Hi,

I've tried a good while to come up with a solution to this, but, it's driving me nuts. Maybe someone could help?

I have 3 categories.

All Business

  • Dr. Adams
  • Dr. Smith
  • Mary's Flower Shop

Primary Categories

  • Physicians
  • Gifts

Secondary Categories

  • General Practice
  • Mental Health
  • Flowers

Both Doctors have a page reference fields to the Primary Category->Physicians

However, Dr. Adams has a page reference to Secondary Categories->General Practice, and Dr. Smith has a page reference to Second Categories->Mental Health

In short, using this method I can assign multiple primary and secondary categories using Page References, to each business.

The trouble I'm having is I'm trying to output only the Primary and Secondary categories that are in use by any businesses, without duplicating the Primary categories, like so:

Physicians

  • General Practice
  • Mental Health

Gifts

  • Flowers

 

I've been using a call like this:

$businesses = $pages->find("template=business-directory-abusiness,bus_region=$region,bus_cat_1=$maincat,bus_cat_2=$subcat")->unique();

Where my problem lies, is this causes the below, but, I'm trying to group them as shown above.

Physicians

  • General Practice

Physicians

  • Mental Health

Gifts, Flowers

 

I've also tried running a for loop to create an array, ($maincat=>id, $subcat=>id, $maincat=>title), then run array_unique(). Still no luck.

 

 

 

 

Link to post
Share on other sites

Hi John W.,

hoping not to misunderstand your data structure:
I'm sure that the Drs. are not in the Gifts businesss, and Mary's Flower shop ist not involved in physicians' tasks.
So the hierarchy of your categories should be just like your wanted output (see the attached image).

Then the individuals (Dr. Adams, Dr. Smith, Mary's Flower Shop) need only to have a reference field to the Secondary Categories - not to the Primary Categories.
(Since Mental Health is a subcategory of Physicians, Dr. Smith automatically is categorized as Physician...)

categories.png

  • Like 2
Link to post
Share on other sites
  • 2 weeks later...

Thanks. I used your suggestion. I also have a Region page reference, for like counties, in each Business listing. Whereas I can also list by region -> main category -> subcategory.

It was a bit tricky, but, I got it and learned a few more PW methods along the way. 🙂

 

cheers! 🍻

  • Like 1
Link to post
Share on other sites

Just in addition... this is what I use for similar tasks.
 

<?php

// Page sub-select - find pages based on previous selected pages
// example: find all brand based on products
// https://processwire.com/talk/topic/3553-handling-categories-on-a-product-catalogue/?do=findComment&comment=37833
// https://github.com/webmanufaktur/processwire-snippets/blob/master/api/api-page-subselect.php
// define array for brands
$brands = new PageArray();

foreach($pages->find("template=productTemplate") as $product) {

	// add brand from product page to brands array
	$brands->add($product->brandField); 

};

 

  • Like 1
Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...