Forum

Thread tagged as: Problem, Error, Configuration

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?).

  1. How do I create the second area category to use the ?area=?
  2. More importantly, how do I filter the two categories to list say all the shops in Harrogate?
  3. A side point, why is using a form/select modifying the / to be %2F?
David Owen

David Owen 0 points

  • 5 months ago
Drew McLellan

Drew McLellan 2544 points
Perch Support

To answer the point that is specifically a Perch question:

More importantly, how do I filter the two categories to list say all the shops in Harrogate?

You can pass an array to the category option:

perch_content_custom('Properties', [
    'category' => ['type/retail', 'area/harrogate'],
]);

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.

Drew McLellan

Drew McLellan 2544 points
Perch Support

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,

How do I create the second area category to use the ?area=?

This is not specific to Perch. This is a HTML question. You need to change the value of the name attribute on your <select>.

<select name="area">

A side point, why is using a form/select modifying the / to be %2F?

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...

perch_content_custom('Properties', [ 'category' => ['$type', '$area'], ]);

Or is there a better "Perch" way.

(keep on learning - bear with me)

Hussein Al Hammad said:

Hello David,

How do I create the second area category to use the ?area=?

This is not specific to Perch. This is a HTML question. You need to change the value of the name attribute on your <select>.

I was meaning the usual Perch category being ?cat= I can change this to anything, yes?

Yes, you are correct. You can change it to whatever you want.

If you are using ?type= and ?area=, you need to use perch_get('type') and perch_get('area').

You could probably do something like this:

// default options
$opts = [
    'template' =>'product_listing.html',
    'page'=>'/properties.php',
    'paginate' => true, 
    'count' => '5', 
];


$categories = array();

// if ?type= exists, add it to $categories
if(perch_get('type')) {
    $categories[] = perch_get('type');
}

// if ?area= exists, add it to $categories
if(perch_get('area')) {
    $categories[] = perch_get('area');
}

// add the category option to $opts if $categories is not empty
if($categories) {
    $opts['category'] = $categories;
}

perch_content_custom('Properties', $opts);

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 with area=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?

Hussein Al Hammad said:

I was meaning the usual Perch category being ?cat= I can change this to anything, yes?

Yes, you are correct. You can change it to whatever you want.

If you are using ?type= and ?area=, you need to use perch_get('type') and perch_get('area').

You could probably do something like this:

// default options
$opts = [
   'template' =>'product_listing.html',
   'page'=>'/properties.php',
   'paginate' => true, 
   'count' => '5', 
];


$categories = array();

// if ?type= exists, add it to $categories
if(perch_get('type')) {
   $categories[] = perch_get('type');
}

// if ?area= exists, add it to $categories
if(perch_get('area')) {
   $categories[] = perch_get('area');
}

// add the category option to $opts if $categories is not empty
if($categories) {
   $opts['category'] = $categories;
}

perch_content_custom('Properties', $opts);

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.

Drew McLellan

Drew McLellan 2544 points
Perch Support

Look at the category-match option.

https://docs.grabaperch.com/perch/categories/filtering/

I think that just might have fixed things