Forum

Thread tagged as: Runway

perch_categories category filtering

I have a category set called 'products' with several categories and subcategories:

products/axles/
products/axles/non-drive-steer-axles/
products/axles/planetary-rigid-axles/
products/axles/planetary-rigid-tandem-axles/
products/axles/planetary-steer-axles/
products/axles/portal-axles/
products/axles/ta-series-axles/
products/brakes/
products/carriers/
products/carriers/carrier-modules/
products/carriers/core-free-carrier-assemblies/
products/components/
products/components/bearings/
products/components/gears/
products/components/electronics/
products/electric-solutions/
products/electric-solutions/axles/
products/suspensions-modules/
products/suspensions-modules/independent-suspensions/
products/suspensions-modules/wheel-end-modules/
products/transfer-cases/
products/transfer-cases/two-speed-transfer-cases/

I'm trying to put all of the subcategory slugs for a particular category into an array so I can loop through the collection items associated with the subcategories. This is what I've tried:

          $prod_cats = perch_categories([
            'category'=>'products/axles',
            'category-match'=>'all',
            'template'=>'_cat_slug.html',
            'skip-template'=>true,
          ]);
          print "<pre>";
          print_r($prod_cats);
          print "</pre>";

_cat_slug.html:

<perch:category id="catSlug" type="slug" for="catTitle" />

and this is the output:

Array
(
    [0] => Array
        (
            [desc] => 
            [catID] => 44
            [setID] => 5
            [catParentID] => 0
            [catTitle] => Axles
            [catSlug] => axles
            [catPath] => products/axles/
            [catDisplayPath] => Axles
            [catOrder] => 1
            [catTreePosition] => 005-001
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [1] => Array
        (
            [catID] => 56
            [setID] => 5
            [catParentID] => 44
            [catTitle] => Non-Drive Steer Axles
            [catSlug] => non-drive-steer-axles
            [catPath] => products/axles/non-drive-steer-axles/
            [catDisplayPath] => Axles › Non-Drive Steer Axles
            [catOrder] => 1
            [catTreePosition] => 005-001-001
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [2] => Array
        (
            [catID] => 57
            [setID] => 5
            [catParentID] => 44
            [catTitle] => Planetary Rigid Axles
            [catSlug] => planetary-rigid-axles
            [catPath] => products/axles/planetary-rigid-axles/
            [catDisplayPath] => Axles › Planetary Rigid Axles
            [catOrder] => 2
            [catTreePosition] => 005-001-002
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [3] => Array
        (
            [catID] => 58
            [setID] => 5
            [catParentID] => 44
            [catTitle] => Planetary Rigid Tandem Axles
            [catSlug] => planetary-rigid-tandem-axles
            [catPath] => products/axles/planetary-rigid-tandem-axles/
            [catDisplayPath] => Axles › Planetary Rigid Tandem Axles
            [catOrder] => 3
            [catTreePosition] => 005-001-003
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [4] => Array
        (
            [catID] => 59
            [setID] => 5
            [catParentID] => 44
            [catTitle] => Planetary Steer Axles
            [catSlug] => planetary-steer-axles
            [catPath] => products/axles/planetary-steer-axles/
            [catDisplayPath] => Axles › Planetary Steer Axles
            [catOrder] => 4
            [catTreePosition] => 005-001-004
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [5] => Array
        (
            [catID] => 60
            [setID] => 5
            [catParentID] => 44
            [catTitle] => Portal Axles
            [catSlug] => portal-axles
            [catPath] => products/axles/portal-axles/
            [catDisplayPath] => Axles › Portal Axles
            [catOrder] => 5
            [catTreePosition] => 005-001-005
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [6] => Array
        (
            [catID] => 61
            [setID] => 5
            [catParentID] => 44
            [catTitle] => T/A Series Axles
            [catSlug] => ta-series-axles
            [catPath] => products/axles/ta-series-axles/
            [catDisplayPath] => Axles › T/A Series Axles
            [catOrder] => 6
            [catTreePosition] => 005-001-006
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [7] => Array
        (
            [desc] => 
            [catID] => 45
            [setID] => 5
            [catParentID] => 0
            [catTitle] => Brakes
            [catSlug] => brakes
            [catPath] => products/brakes/
            [catDisplayPath] => Brakes
            [catOrder] => 2
            [catTreePosition] => 005-002
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [8] => Array
        (
            [catID] => 52
            [setID] => 5
            [catParentID] => 0
            [catTitle] => Carriers
            [catSlug] => carriers
            [catPath] => products/carriers/
            [catDisplayPath] => Carriers
            [catOrder] => 3
            [catTreePosition] => 005-003
            [catDynamicFields] => []
            [catDepth] => 1
        )

    [9] => Array
        (
            [catID] => 62
            [setID] => 5
            [catParentID] => 52
            [catTitle] => Carrier Modules
            [catSlug] => carrier-modules
            [catPath] => products/carriers/carrier-modules/
            [catDisplayPath] => Carriers › Carrier Modules
            [catOrder] => 1
            [catTreePosition] => 005-003-001
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [10] => Array
        (
            [catID] => 63
            [setID] => 5
            [catParentID] => 52
            [catTitle] => Core-Free Carrier Assemblies
            [catSlug] => core-free-carrier-assemblies
            [catPath] => products/carriers/core-free-carrier-assemblies/
            [catDisplayPath] => Carriers › Core-Free Carrier Assemblies
            [catOrder] => 2
            [catTreePosition] => 005-003-002
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [11] => Array
        (
            [desc] => 
            [catID] => 48
            [setID] => 5
            [catParentID] => 0
            [catTitle] => Components
            [catSlug] => components
            [catPath] => products/components/
            [catDisplayPath] => Components
            [catOrder] => 4
            [catTreePosition] => 005-004
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [12] => Array
        (
            [catID] => 64
            [setID] => 5
            [catParentID] => 48
            [catTitle] => Bearings
            [catSlug] => bearings
            [catPath] => products/components/bearings/
            [catDisplayPath] => Components › Bearings
            [catOrder] => 1
            [catTreePosition] => 005-004-001
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [13] => Array
        (
            [catID] => 65
            [setID] => 5
            [catParentID] => 48
            [catTitle] => Gears
            [catSlug] => gears
            [catPath] => products/components/gears/
            [catDisplayPath] => Components › Gears
            [catOrder] => 2
            [catTreePosition] => 005-004-002
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [14] => Array
        (
            [catID] => 66
            [setID] => 5
            [catParentID] => 48
            [catTitle] => Electronics
            [catSlug] => electronics
            [catPath] => products/components/electronics/
            [catDisplayPath] => Components › Electronics
            [catOrder] => 3
            [catTreePosition] => 005-004-003
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [15] => Array
        (
            [catID] => 50
            [setID] => 5
            [catParentID] => 0
            [catTitle] => Electric Solutions
            [catSlug] => electric-solutions
            [catPath] => products/electric-solutions/
            [catDisplayPath] => Electric Solutions
            [catOrder] => 5
            [catTreePosition] => 005-005
            [catDynamicFields] => []
            [catDepth] => 1
        )

    [16] => Array
        (
            [catID] => 67
            [setID] => 5
            [catParentID] => 50
            [catTitle] => Axles
            [catSlug] => axles
            [catPath] => products/electric-solutions/axles/
            [catDisplayPath] => Electric Solutions › Axles
            [catOrder] => 1
            [catTreePosition] => 005-005-001
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [17] => Array
        (
            [desc] => 
            [catID] => 46
            [setID] => 5
            [catParentID] => 0
            [catTitle] => Suspensions & Modules
            [catSlug] => suspensions-modules
            [catPath] => products/suspensions-modules/
            [catDisplayPath] => Suspensions & Modules
            [catOrder] => 6
            [catTreePosition] => 005-006
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [18] => Array
        (
            [catID] => 68
            [setID] => 5
            [catParentID] => 46
            [catTitle] => Independent Suspensions
            [catSlug] => independent-suspensions
            [catPath] => products/suspensions-modules/independent-suspensions/
            [catDisplayPath] => Suspensions & Modules › Independent Suspensions
            [catOrder] => 1
            [catTreePosition] => 005-006-001
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [19] => Array
        (
            [catID] => 69
            [setID] => 5
            [catParentID] => 46
            [catTitle] => Wheel End Modules
            [catSlug] => wheel-end-modules
            [catPath] => products/suspensions-modules/wheel-end-modules/
            [catDisplayPath] => Suspensions & Modules › Wheel End Modules
            [catOrder] => 2
            [catTreePosition] => 005-006-002
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [20] => Array
        (
            [desc] => 
            [catID] => 47
            [setID] => 5
            [catParentID] => 0
            [catTitle] => Transfer Cases
            [catSlug] => transfer-cases
            [catPath] => products/transfer-cases/
            [catDisplayPath] => Transfer Cases
            [catOrder] => 7
            [catTreePosition] => 005-007
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [21] => Array
        (
            [catID] => 70
            [setID] => 5
            [catParentID] => 47
            [catTitle] => Two-Speed Transfer Cases
            [catSlug] => two-speed-transfer-cases
            [catPath] => products/transfer-cases/two-speed-transfer-cases/
            [catDisplayPath] => Transfer Cases › Two-Speed Transfer Cases
            [catOrder] => 1
            [catTreePosition] => 005-007-001
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [22] => Array
        (
            [desc] => 
            [catID] => 35
            [setID] => 6
            [catParentID] => 0
            [catTitle] => Agriculture
            [catSlug] => agriculture
            [catPath] => markets/agriculture/
            [catDisplayPath] => Agriculture
            [catOrder] => 1
            [catTreePosition] => 006-001
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [23] => Array
        (
            [desc] => 
            [catID] => 36
            [setID] => 6
            [catParentID] => 0
            [catTitle] => Aircraft Ground Support
            [catSlug] => aircraft-ground-support
            [catPath] => markets/aircraft-ground-support/
            [catDisplayPath] => Aircraft Ground Support
            [catOrder] => 2
            [catTreePosition] => 006-002
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [24] => Array
        (
            [desc] => 
            [catID] => 37
            [setID] => 6
            [catParentID] => 0
            [catTitle] => Construction
            [catSlug] => construction
            [catPath] => markets/construction/
            [catDisplayPath] => Construction
            [catOrder] => 3
            [catTreePosition] => 006-003
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [25] => Array
        (
            [desc] => 
            [catID] => 38
            [setID] => 6
            [catParentID] => 0
            [catTitle] => Defense
            [catSlug] => defense
            [catPath] => markets/defense/
            [catDisplayPath] => Defense
            [catOrder] => 4
            [catTreePosition] => 006-004
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [26] => Array
        (
            [desc] => 
            [catID] => 39
            [setID] => 6
            [catParentID] => 0
            [catTitle] => Forestry
            [catSlug] => forestry
            [catPath] => markets/forestry/
            [catDisplayPath] => Forestry
            [catOrder] => 5
            [catTreePosition] => 006-005
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [27] => Array
        (
            [desc] => 
            [catID] => 40
            [setID] => 6
            [catParentID] => 0
            [catTitle] => Material Handling
            [catSlug] => material-handling
            [catPath] => markets/material-handling/
            [catDisplayPath] => Material Handling
            [catOrder] => 6
            [catTreePosition] => 006-006
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [28] => Array
        (
            [desc] => 
            [catID] => 41
            [setID] => 6
            [catParentID] => 0
            [catTitle] => Mining
            [catSlug] => mining
            [catPath] => markets/mining/
            [catDisplayPath] => Mining
            [catOrder] => 7
            [catTreePosition] => 006-007
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [29] => Array
        (
            [catID] => 49
            [setID] => 6
            [catParentID] => 0
            [catTitle] => Refuse
            [catSlug] => refuse
            [catPath] => markets/refuse/
            [catDisplayPath] => Refuse
            [catOrder] => 8
            [catTreePosition] => 006-008
            [catDynamicFields] => []
            [catDepth] => 1
        )

    [30] => Array
        (
            [desc] => 
            [catID] => 42
            [setID] => 6
            [catParentID] => 0
            [catTitle] => Specialty Vehicles
            [catSlug] => specialty-vehicles
            [catPath] => markets/specialty-vehicles/
            [catDisplayPath] => Specialty Vehicles
            [catOrder] => 9
            [catTreePosition] => 006-009
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [31] => Array
        (
            [desc] => 
            [catID] => 43
            [setID] => 6
            [catParentID] => 0
            [catTitle] => Transit
            [catSlug] => transit
            [catPath] => markets/transit/
            [catDisplayPath] => Transit
            [catOrder] => 10
            [catTreePosition] => 006-010
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [32] => Array
        (
            [catID] => 51
            [setID] => 6
            [catParentID] => 0
            [catTitle] => Commercial Truck
            [catSlug] => commercial-truck
            [catPath] => markets/commercial-truck/
            [catDisplayPath] => Commercial Truck
            [catOrder] => 19
            [catTreePosition] => 006-019
            [catDynamicFields] => []
            [catDepth] => 1
        )

)

How can I get only the subcategories from the 'products/axles' category? Ultimately, all I'm trying to do is list all collection items in the 'products/axles' category, separated by their subcategories. If there's a better way to do that, I'm all ears!

Diagnostics:

Diagnostics report
HEALTH CHECK

Perch Runway is up to date
PHP 5.6.14-1+deb.sury.org~trusty+1 is up to date
MySQL 5.5.46-0ubuntu0.14.04.2 is up to date
Image processing available
SUMMARY INFORMATION

Perch Runway: 2.8.31, PHP: 5.6.14-1+deb.sury.org~trusty+1, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: 3c688b6bbc30d36af3ac34fdd4b7b5b787fe5555 $, with PDO
Server OS: Linux, apache2handler
Installed apps: content (2.8.31), assets (2.8.31), categories (2.8.31), perch_blog (5.0), perch_events (1.9.3), perch_forms (1.8.3)
App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_blog', 'perch_events', 'perch_forms', );
PERCH_LOGINPATH: /at-admin
PERCH_PATH: /var/www/public/axletech.dev/at-admin
PERCH_CORE: /var/www/public/axletech.dev/at-admin/core
PERCH_RESFILEPATH: /var/www/public/axletech.dev/at-admin/resources
Image manipulation: GD Imagick
PHP limits: Max upload 100M, Max POST 100M, Memory: 128M, Total max file upload: 100M
F1: 2edba60ed1f613d6dd804feb202456a2
Resource folder writeable: Yes
HTTP_HOST: axletech.dev
DOCUMENT_ROOT: /var/www/public/axletech.dev
REQUEST_URI: /at-admin/core/settings/diagnostics/
SCRIPT_NAME: /at-admin/core/settings/diagnostics/index.php
Shane Lenzen

Shane Lenzen 18 points

  • 5 years ago

Tried this, too:

          $prod_cats = perch_categories([
            'filter'=>'catPath',
            'match'=>'contains',
            'value'=>'products/axles/',
            'template'=>'_cat_slug.html',
            'skip-template'=>true,
          ]);
          print "<pre>";
          print_r($prod_cats);
          print "</pre>";

But that output is:

Array
(
)

Not sure what I'm doing wrong.

Shane, I think you need to filter catDepth if all you want is sub-cats

rk

Robert Ketter said:

Shane, I think you need to filter catDepth if all you want is sub-cats

rk

Oops, I think I gave wrong advice here after a quick search...

Thanks Robert, but that doesn't help in my particular case. I need to loop through the subcategories so I can display the products separated by subcategory, something like:

<div class="productList">
    <h2>Subcat 1</h2>
    <ul>
        <li>Product 1</li>
        <li>Product 2</li>
        <li>Product 3</li>
    </ul>
    <h2>Subcat 2</h2>
    <ul>
        <li>Product 1</li>
        <li>Product 2</li>
    </ul>
</div>

...etc. It seems I can't use perch:if different="" because it has to be used within <perch:categories>. So, I figure I need to loop through the subcategories. The problem is, if you look at the array output from my first post, I can't seem to get perch_categories() to only output the subcats from 'products/axles'. Instead, it's showing all sets, categories and subcats (like products/brakes, markets, markets/mining, etc).

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can't filter categories by category, as categories aren't categorised. Filter them by their path:

perch_categories([
    'filter' => 'catPath',
    'match' => 'contains',
    'value' => 'products/axles/?',
]);

That question mark did it. Thanks Drew! FYI the docs for perch_categories() list 'category' as an option https://docs.grabaperch.com/docs/categories/page-functions/

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's a general list of options that applies to any function based on the same infrastructure. I mean you technically could put categories in categories, but like wtf.

mind blown

Hi again. So this:

          $prod_cats = perch_categories([
            'filter'=>'catPath',
            'match'=>'contains',
            'value'=>'products/'.$currentURI[3].'/?',
            'skip-template'=>true,
          ]);
          print "<pre>";
          print_r($prod_cats);
          print "</pre>";

Gives me this:

Array
(
    [0] => Array
        (
            [desc] => 
            [catID] => 44
            [setID] => 5
            [catParentID] => 0
            [catTitle] => Axles
            [catSlug] => axles
            [catPath] => products/axles/
            [catDisplayPath] => Axles
            [catOrder] => 1
            [catTreePosition] => 005-001
            [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"","processed":""}}
            [perch_desc] => 
            [catDepth] => 1
        )

    [1] => Array
        (
            [catID] => 56
            [setID] => 5
            [catParentID] => 44
            [catTitle] => Non-Drive Steer Axles
            [catSlug] => non-drive-steer-axles
            [catPath] => products/axles/non-drive-steer-axles/
            [catDisplayPath] => Axles › Non-Drive Steer Axles
            [catOrder] => 1
            [catTreePosition] => 005-001-001
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [2] => Array
        (
            [catID] => 57
            [setID] => 5
            [catParentID] => 44
            [catTitle] => Planetary Rigid Axles
            [catSlug] => planetary-rigid-axles
            [catPath] => products/axles/planetary-rigid-axles/
            [catDisplayPath] => Axles › Planetary Rigid Axles
            [catOrder] => 2
            [catTreePosition] => 005-001-002
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [3] => Array
        (
            [catID] => 58
            [setID] => 5
            [catParentID] => 44
            [catTitle] => Planetary Rigid Tandem Axles
            [catSlug] => planetary-rigid-tandem-axles
            [catPath] => products/axles/planetary-rigid-tandem-axles/
            [catDisplayPath] => Axles › Planetary Rigid Tandem Axles
            [catOrder] => 3
            [catTreePosition] => 005-001-003
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [4] => Array
        (
            [catID] => 59
            [setID] => 5
            [catParentID] => 44
            [catTitle] => Planetary Steer Axles
            [catSlug] => planetary-steer-axles
            [catPath] => products/axles/planetary-steer-axles/
            [catDisplayPath] => Axles › Planetary Steer Axles
            [catOrder] => 4
            [catTreePosition] => 005-001-004
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [5] => Array
        (
            [catID] => 60
            [setID] => 5
            [catParentID] => 44
            [catTitle] => Portal Axles
            [catSlug] => portal-axles
            [catPath] => products/axles/portal-axles/
            [catDisplayPath] => Axles › Portal Axles
            [catOrder] => 5
            [catTreePosition] => 005-001-005
            [catDynamicFields] => []
            [catDepth] => 2
        )

    [6] => Array
        (
            [catID] => 61
            [setID] => 5
            [catParentID] => 44
            [catTitle] => T/A Series Axles
            [catSlug] => ta-series-axles
            [catPath] => products/axles/ta-series-axles/
            [catDisplayPath] => Axles › T/A Series Axles
            [catOrder] => 6
            [catTreePosition] => 005-001-006
            [catDynamicFields] => []
            [catDepth] => 2
        )

)

Which is great! But when I try:

          $prod_cats = perch_categories([
            'filter'=>[
              [
                'filter'=>'catPath',
                'match'=>'contains',
                'value'=>'products/'.$currentURI[3].'/?',
              ],
              [
                'filter'=>'catDepth',
                'match'=>'gt',
                'value'=>1,
              ],
            ],
            'skip-template'=>true,
          ]);

The array is empty. Any idea why?

Drew McLellan

Drew McLellan 2638 points
Perch Support

catDepth isn't a value in the database. You'd need to do this;

perch_categories([
    'filter'=>[
          [
            'filter'=>'catPath',
            'match'=>'contains',
            'value'=>'products/t-shirts/?',
          ],
          [
            'filter'=>'LENGTH(catTreePosition) - LENGTH(REPLACE(catTreePosition, "-", ""))',
            'match'=>'eq',
            'value'=>2,
          ],
        ],
]);

Well I definitely wouldn't have figured that out on my own. Thanks again!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Turns out there's lots of stuff you can do, just nobody ever does.