I have a table of destinations ( states ) which contain primary level categories and subcategories for activities. My ultimate goal is to output xml navigation links so I can link to:
- Each destination permalink ( ordered alphabetically by destination slug name )
- Each category underneath
- Each subcategories underneath the category
The below is what I'll generate with the sql query, incase it has any bearing on the query but I don't need xml help for this question:
<item href="/destinations/alabama/">
<list>
<item href="/destinations/alabama/category/">
<list>
<item href="/destinations/alabama/category/sub-category/"></item>
</list>
</item>
</list>
</item>
<item href="/destinations/maryland/">
<list>
<item href="/destinations/maryland/category/">
<list>
<item href="/destinations/maryland/category/sub-category/"></item>
</list>
</item>
</list>
</item>
Currently I have a query that grabs subcategories but it doesn't account for active destinations.
SELECT
subcategories.name AS subcategory_name,
subcategories.slug AS subcategory_slug,
categories.name AS category_name,
categories.slug AS category_slug
FROM
subcategories
LEFT JOIN destinations_subcategories ON
destinations_subcategories.subcategory_id = subcategories.id
LEFT JOIN categories ON
destinations_subcategories.category_id = categories.id
WHERE
1=1 AND
subcategories.is_active = 1 AND
categories.is_active = 1
Which returns:
subcategory_name subcategory_slug category_name category_slug
Fly Fishing fly-fishing Fishing fishing
But this query doesn't account for destinations which are active, since it's possible to map relationships between inactive destinations and active categories. I'm thinking I need to maybe have a subquery grabbing all active destinations, then do the joining?
SCHEMA/row samples:
id name slug active
7 Maryland maryland 1
My categories table, sample row:
id name slug active
1 Fishing fishing 1
Subcategories table sample row:
id name slug active
3 Fly-Fishing fly-fishing 1
And 2 tables to store relationships, the first maps subcategories to categories:
id category_id subcategory_id
5 1 3
The second, destinations_subcategories
maps destinations to subcategories:
id destination_id category_id subcategory_id
5 7 1 3