Forum

Thread tagged as: Problem

User-filtered list not working

After successfully completing the 'Two page list and detail' tutorial (https://docs.grabaperch.com/perch/content/functions/two-page-list-and-detail/) and posting some items to test with I have moved on to the 'user-filtered list' tutorial (https://docs.grabaperch.com/perch/content/functions/user-filtered-lists/) to enable users to search for these items using a combination of up to 3 different category options.

My search form seems to be generating the correct url query string:

https://localhost/search.php?industry=food-beverage&application=cable&company=lapp

...but I’m not getting any results on my search page.

Perch diagnostic summary

Perch: 3.1.2, PHP: 7.1.5, MySQL: mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $, with PDO
Server OS: WINNT, apache2handler
Installed apps: content (3.1.2), assets (3.1.2), categories (3.1.2), perch_blog (5.6.1), perch_forms (1.12), perch_backup (1.2), perch_mailchimp (3.1)
App runtimes: <?php $apps_list = [ 'perch_blog', 'perch_mailchimp', 'content', 'categories', 'perch_forms', ]; ?>
PERCH_LOGINPATH: /perch
PERCH_PATH: C:\MAMP\htdocs\perch
PERCH_CORE: C:\MAMP\htdocs\perch\core
PERCH_RESFILEPATH: C:\MAMP\htdocs\perch\resources
Image manipulation: GD Imagick
PHP limits: Max upload 20M, Max POST 30M, Memory: 256M, Total max file upload: 20M
F1: 3b606135b33e6a102526838f4152a807
Resource folder writeable: Yes
HTTP_HOST: localhost
DOCUMENT_ROOT: C:/MAMP/htdocs
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php

My search form

<perch:form id="filter" method="get" action="/search.php">

    <div>
        <perch:label for="industry">Industry</perch:label>
        <perch:input type="select" id="industry" options=",food-beverage,machine-building,solar,automation-networking">
    </div>

    <div>
        <perch:label for="application">Application</perch:label>
        <perch:input type="select" id="application" options=",cable,glanding,dragchain,tools,safety,connectors,din-terminals,switchgear,hydraulic-ram-systems,industrial-pc,rail">
    </div>

    <div>
        <perch:label for="company">Company</perch:label>
        <perch:input type="select" id="company" options=",lapp,moxa,wieland,ls-industrial-systems,ecs">
    </div>

    <div><perch:input type="submit" value="Filter"></div>

</perch:form>

Code from tut edited for my categories 'industry', 'application' and 'company'.

// Create an empty array ready to add our filters to
                $filters = array();

                if (perch_get('industry')) {
                        // if 'industry' is on the URL, add a filter for industry
                        $filters[] = array(
                                'filter' => 'industry',
                                'match'  => 'eq',
                                'value'  => perch_get('industry'),
                        );
                }

                if (perch_get('application')) {
                        // if 'application' is on the URL, add a filter for application
                        $filters[] = array(
                                'filter' => 'application',
                                'match'  => 'eq',
                                'value'  => perch_get('application'),
                        );
                }

                if (perch_get('company')) {
                        // if 'company' is on the URL, add a filter for the company
                        $filters[] = array(
                                'filter' => 'company',
                                'match'  => 'eq',
                                'value'  => perch_get('company'),
                        );
                }

                // Unset the filters if none are used:
                if (!count($filters)) $filters=false;

                // Then get the list
                perch_content_custom('Downloads', array(
                        'template'   => 'listings/download_listing.html',
                        'sort'       => 'title',
                        'sort-order' => 'DESC',
                        'filter'     => $filters,
                        'filter-mode'=>'ungrouped',
                ));

Have tried with and without ('filter-mode'=>'ungrouped',) which I found in a solved issue thread (https://forum.grabaperch.com/forum/04-04-2017-problem-with-user-filtered-list-in-perch-3)

Any help would much appreciated. Thanks.

Gavin Mills

Gavin Mills 0 points

  • 2 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Gavin,

to search for these items using a combination of up to 3 different category options.

Category filtering works a little differently: https://docs.grabaperch.com/perch/categories/filtering/

So instead of using the filter option, you would use the category option:

Hi Hussein

Thanks for the suggestion but the Category filtering looks like it works as a hard-coded option so I'm not sure how to make it work in unison with my user-controlled search form?

I want the user to be able to select a category from each drop-down list in my search form at the same time so they can search for their own choice of category combinations.

For example they may want to see a list of catalogues from the Glanding Category (which is in one Set) that is also from the food-beverage Category (which is in another Set).

Is this possible?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

The examples in the docs are hard-coded to show you how it works. It doesn't have to be hard-coded.

Assuming the food-bevarage Category is in a Set called industries your category path would be industries/food-bevarage/. And the same applies for the other category options you have.

So you can do something like:

$categories = array();

if(perch_get('industry')) {
$categories[] = 'industries/'.perch_get('industry');
}

if(perch_get('company')) {
$categories[] = 'companies/'.perch_get('company');
}

if (!count($categories)) $categories=false;

perch_content_custom('Downloads', array(
'template' => 'listings/download_listing.html',
'category' => $categories
));

Thanks so much Hussein, that worked perfectly.

The only remaining problem is that one category doesn't seem to limit the other so if I select something like (Industry/food-beverage) + (Application/cable) + (Company/lapp), the returned result is all items from the food-beverage, cable and lapp categories, which means I end up with food-beverage and cable catalogues from multiple companies instead of just Lapp.

URL example: https://localhost/downloads.php?industry=food-beverage&application=cable&company=lapp

Is there a way to restrict the results to the user's specific category combination selection for proper filtering?

Here's my working code so far:

$categories = array();

                if(perch_get('industry')) {
                    $categories[] = 'industry/'.perch_get('industry');
                    }

                if(perch_get('application')) {
                    $categories[] = 'application/'.perch_get('application');
                    }

                if(perch_get('company')) {
                    $categories[] = 'company/'.perch_get('company');
                    }

                if (!count($categories)) $categories=false;

                if ($categories) {

                    perch_content_custom('Downloads', array(
                        'page' => ['/downloads.php', '/download-centre/*'],
                        'template' => 'listings/download_listing.html',
                        'category' => $categories
                    ));

                }

                elseif (perch_get('cat')) {

                    perch_content_custom('Downloads', array(
                        'template'  => 'listings/download_listing.html',
                        'page' => ['/downloads.php', '/download-centre/*'],
                        'category'  => perch_get('cat'),
                    ));

                }

                else {

                    perch_content_create('Downloads', array(
                         'template'     => 'listings/download_item.html',
                         'multiple'     => true,
                         'edit-mode'    => 'listdetail',
                        ));

                     perch_content_custom('Downloads', array(
                         'template'     => 'listings/download_listing.html',
                     ));
                }

Thanks again.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Is there a way to restrict the results to the user's specific category combination selection for proper filtering?

Yes, you can use the option category-match, which is documented here: https://docs.grabaperch.com/perch/categories/filtering/

perch_content_custom('Downloads', array(
'page' => ['/downloads.php', '/download-centre/*'],
'template' => 'listings/download_listing.html',
'category' => $categories,
'category-match' => 'all'
));

Thanks Hussein, it's working as it should now, so I think we can call this one solved.

For anyone else who wants to use or develop it for their own project - here's my final working code for my downloads page. It can return item lists based on category links - or - from the $categories array filters. You can also manually create your own custom items on the page to default to in your perch admin.

Downloads Page (downloads.php)

$categories = array();

                if(perch_get('industry')) {
                    $categories[] = 'industry/'.perch_get('industry');
                    }

                if(perch_get('application')) {
                    $categories[] = 'application/'.perch_get('application');
                    }

                if(perch_get('company')) {
                    $categories[] = 'company/'.perch_get('company');
                    }

                if (!count($categories)) $categories=false;

                if ($categories) {

                    perch_content_custom('Downloads', array(
                        'page' => ['/downloads.php', '/download-centre/*'],
                        'template' => 'listings/download_listing.html',
                        'category' => $categories,
                        'category-match' => 'all'
                    ));

                }

                elseif (perch_get('cat')) {

                    perch_content_custom('Downloads', array(
                        'template'  => 'listings/download_listing.html',
                        'page' => ['/downloads.php', '/download-centre/*'],
                        'category'  => perch_get('cat'),
                    ));

                }

                else {

                    perch_content_create('Downloads', array(
                         'template'     => 'listings/download_item.html',
                         'multiple'     => true,
                         'edit-mode'    => 'listdetail',
                        ));

                     perch_content_custom('Downloads', array(
                         'template'     => 'listings/download_listing.html',
                     ));
                }

Don’t forget the .htaccess mod_rewrite for category pages

RewriteRule ^download/([a-zA-Z0-9-/]+)$ /download.php?s=$1 [L]
RewriteRule ^downloads/([a-zA-Z0-9-/]+)$ /downloads.php?cat=$1 [L]

It’s not perfect yet hope this helps someone.

Thanks again Hussein I really appreciate all your help.