Forum
Filter multiple categories from select form
I have the following HTML to filter two categories Type and Area.
<form method="get" action="category.php?cat=">
<h2 class="catTitle">Sort by Type</h2>
<select id="cattype" name="cat">
<option value="type/retail/">Retail</option>
<option value="type/office/">Office</option>
<option value="type/industrial/">Industrial</option>
</select>
<h2 class="catTitle">Sort by Area</h2>
<select id="catarea" name="cat">
<option value="area/">Please Select</option>
<option value="area/harrogate/">Harrogate</option>
<option value="area/ilkley/">Ilkley</option>
<option value="area/yeadon/">Yeadon</option>
</select>
<input type="submit" value="Submit">
</form>
The page php is...
<form method="get" action="category.php?cat=">
<?php
// This create the catergory tags set
perch_categories(array(
'template' => 'category-type-select.html',
'set' => 'type',
));
perch_categories([
'set'=>'area',
'sort'=>'catTitle',
'template' => 'category-area-select.html',
'sort-order'=>'ASC',
'each'=>function($item)
{
if (PerchUtil::count(perch_content_custom('Properties', ['skip-template'=>true, 'category'=>$item['catPath']])) > 0) return $item;
}
]);
?>
<input type="submit" value="Submit" />
</form>
With two templates...
<perch:before>
<h2 class="catTitle">Sort by Type</h2>
<select id="cattype" name="cat">
<option value="area/"/>Please select</option>
</perch:before>
<option value="<perch:category id="catPath" />"/><perch:category id="catTitle" /></option>
<perch:after>
</select>
</perch:after>
and
<perch:before>
<h2 class="catTitle">Sort by Area</h2>
<select id="catarea" name="cat">
<option value="area/"/>Please Select</option>
</perch:before>
<perch:if id="catDepth" match="neq" value="0">
<option value="<perch:category id="catPath" />"/><perch:category id="catTitle" /></option>
</perch:if>
<perch:after>
</select>
</perch:after>
The category page has...
<?php
if (perch_get('cat')) {
perch_content_custom('Properties', array(
'template' =>'product_listing.html',
'page'=>'/properties.php',
'category' => perch_get('cat'),
'paginate' => true,
'count' => '5',
));
} else {
perch_categories(array(
'set' => 'type',
));
}
?>
But the problem is that an example query string creates... ../category.php?cat=type%2Fretail%2F&cat=area%2Fyeadon%2F
. Which does not work its looking at the last category query using the ?cat=
, as I can't extract other information of the duplicated id (or can I?).
- How do I create the second area category to use the
?area=
? - More importantly, how do I filter the two categories to list say all the
shops
inHarrogate
? - A side point, why is using a form/select modifying the
/
to be%2F
?
To answer the point that is specifically a Perch question:
You can pass an array to the
category
option:Thanks, but you've hardcoded the filter, the client would be adding their own categories so they need to be retrieved from the URL string. Which is the bit I'm stuck on.
I've only hard coded it to show you the syntax for specifying multiple categories. The point being you can pass in an array of multiple category paths.
Hello David,
This is not specific to Perch. This is a HTML question. You need to change the value of the
name
attribute on your<select>
.Does it matter?
%2F
is the same as/
.Thanks Hussein, I know about the select="name" I was meaning the usual Perch category being
?cat=
I can change this to anything, yes? So I'd have?type=
and?area=
in the URL query. So I just get these variables into Perch and filter like so...Or is there a better "Perch" way.
(keep on learning - bear with me)
Yes, you are correct. You can change it to whatever you want.
If you are using
?type=
and?area=
, you need to useperch_get('type')
andperch_get('area')
.You could probably do something like this:
This enables you to also filter by type alone or by area alone rather than always filter by both type and area.
Hope this helps.
Thanks, I'll have a play with this. With play comes understanding.
Hussein,
After looking at this it's not quite right, for example, a query listing
type=office
together witharea=harrogate
lists any office and anything in Harrogate "together" rather than specifically listing a shortened list of "offices in Harrogate".How can I target correctly?
Look at the
category-match
option.https://docs.grabaperch.com/perch/categories/filtering/
I think that just might have fixed things