Forum

Thread tagged as: Question, Forum, Runway

Get category path & name from a collection entry on landing page (no catSlug in...

Hey guys, I'm trying to pull in a category path and category name from a collection entry on a landing where there is no category in the URL. I have 4 entries on this page filtered by a checkbox called 'featured_business'. Each of the entries has one or two categories assigned from a set named 'business'. I only need the parent category path to pass in an 'href' - www.sitename.co.nz/business/categoryName/. I've had a bit of help to get this far but I'm not quite there yet. I can't seem to get anything via the 'business_category'.

Thanks in advance.

In the perch:showall, I can see the 'business_category' array pulling in the associated catIDs:

ID  Value
page_title  Business
perch_page_path /business/index.php
page_cat    false
_id 1
listing_name    ASURE Kaimai View Motel
_title  ASURE Kaimai View Motel
slug    asure-kaimai-view-motel
business_category   
Array
(
    [0] => 1
    [1] => 29
)
featured_business   yes

My /business/index.php page holds this code block:

<?php
    $businesses = perch_collection('Businesses', array(
     'template' => '/business/featured_layout.html',
     'filter' => 'featured_business',
     'match' => 'eq',
     'value' => 'yes',
    'sort' => 'listing_name',
    'sort-order' => 'ASC',
     'skip-template' => true,
    'return-html' => true,
    ));

    if(isset($businesses[0])) {
         $cat = perch_categories([
         'set' => 'business',
        'filter' => 'catID',
        'match' => 'in',
        'value' => $businesses['business_category'], 
        'skip-template' => true
         ]);

        if($cat) {
            $catPath = $cat['catPath'];
            $catSlug = $cat['catSlug'];
         }
    }
    echo($businesses[html]);
    ?>

My 'featured_layout.html' page:

<perch:before>
    <div class="row">
</perch:before>             

    <!--* start of categories *-->
    <div class="col-lg-4 col-xl-3">
        <div class="card">
            <img class="card-img-top" src="<perch:content id="listing_image">" alt="<perch:content id="listing_name">" class="img-fluid center-block">
            <div class="card-body">
                <h5 class="card-title"><perch:content id="listing_name"></h5>
                <a href="/<perch:content id="catPath" /><perch:content id="slug" type="slug"/>" class="btn btn-round"><i class="fas fa-long-arrow-alt-right"></i></a>
            </div>
        </div>
    </div>
    <!--* end of categories *-->

<perch:after>               
    </div>
</perch:after>

My business data page:

<perch:content id="listing_name" type="text" label="Business Name" required title>
<perch:content id="slug" type="slug" for="listing_name" suppress="true">

<perch:categories id="business_category" label="Business Category" set="business" required>
<perch:content id="featured_business" type="checkbox" label="Featured Business" value="yes" help="Is this business featured on landing page?">
<perch:content id="listing_info" type="textarea" label="Listing Information" editor="ckeditor" html="true" divider-before="collapse-start|Listing Information">
<!--* Images *-->
<perch:content type="image" id="listing_logo" label="Logo Image" width="576" bucket="Business" divider-before="collapse-start|Listing Images">
<perch:content type="image" id="listing_image" label="Listing Image" width="576" height="230" crop="true" bucket="Business" divider-after="collapse-end|Listing Images">
<!-- start of repeating images -->
<perch:repeater id="slider_images" label="Slider Images" max="6">
    <perch:content type="image" id="slide_image" label="Slide Image" width="768" height="500" bucket="Business" />
</perch:repeater>
<!-- end of repeating images -->
<!--* Contact details *-->
<perch:content id="listing_address" type="textarea" label="Address" editor="ckeditor" html="true" divider-before="collapse-start|Contact Information">
<perch:content id="listing_phone_number" type="text" label="Phone">
<perch:content id="listing_mobile_number" type="text" label="Mobile">
<perch:content id="listing_fax_number" type="text" label="Fax">
<!--* social media *-->
<perch:content id="listing_website" type="text" label="Website">
<perch:content id="listing_facebook" type="text" label="Facebook">
<!--* category *-->

Diagnostics as follows:

Perch Runway: 3.1.1, PHP: 7.2.5, MySQL: mysqlnd 5.0.12-dev - 20150407 - $Id: 38fea24f2847fa7519001be390c98ae0acafe387 $, with PDO
Server OS: Linux, fpm-fcgi
Installed apps: content (3.1.1), assets (3.1.1), categories (3.1.1), perch_events (1.9.5), perch_forms (1.11), perch_gallery (2.8.9), perch_shop_orders (1.2.6), perch_shop_products (1.2.6), perch_shop (1.2.6), perch_members (1.6.4)
App runtimes: <?php $apps_list = [ 'perch_gallery', 'perch_forms', 'perch_members', 'perch_shop', 'perch_events', ];
PERCH_LOGINPATH: /perch
PERCH_PATH: /container/application/public/perch
PERCH_CORE: /container/application/public/perch/core
PERCH_RESFILEPATH: /container/application/public/perch/resources
Image manipulation: GD Imagick
PHP limits: Max upload 100M, Max POST 100M, Memory: 128M, Total max file upload: 100M
F1: 3b606135b33e6a102526838f4152a807
Resource folder writeable: Yes
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
REQUEST_URI: /perch/core/settings/diagnostics/
DOCUMENT_ROOT: /var/www/html/public
HTTP_HOST: katchkatikati.xeno.co.nz
Steve Hopper

Steve Hopper 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Steve,

You need to use $businesses[0]['business_category'].

And to use match='in', value should be a string like value1,value2,value3.

$business_catIDs = $businesses[0]['business_category'];

$cats = perch_categories([
'set' => 'business',
'filter' => 'catID',
'match' => 'in',
'value' => implode(',',  $business_catIDs), 
'skip-template' => true
]);

$cats should return an array of categories so you can loop through them with foreach to grab what you need.

if($cats) {
foreach($cats as $cat) {
// use $cat['catPath'] or $cat['catSlug'] as you need
}
}

Hey Hussein, Thanks for extending on what you already provided me with. I started reading up on implode & foreach loops last night before posting on the forum so excuse my nooby questions. Your original post on slack did work as I had 4 featured items using the same category(s). This was never realistic though. If I use:

$business_catIDs = $businesses[0]['business_category'];

$cats = perch_categories([
'set' => 'business',
'filter' => 'catID',
'match' => 'in',
'value' => implode(',', $business_catIDs), 
'skip-template' => true
]);

It only ever returns the 1st item's categories as expected using:

$businesses[0]['business_category']

Array ( [0] => 1 [1] => 29 ) 

and the catPaths:

business/accommodation/
business/accommodation/motels/

I now have 4 featured items using different categories from the 'business' set which is more realistic for the client. How do I get access to the other 3 categories?

Again, I appreciate your time on this.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

excuse my nooby questions

You are in the right place to ask questions!

The reason I'm using implode() is to format the value of $businesses[0]['business_category'] from an array to a comma separated list (string), which is the format match='in' requires the value option to have (e.g. value1,value2,value3).

So for perch_categories() to return all the selected categories, we use 'value' => 'catID_1,catID_2,catID_3' (but we're generating the value dynamically).

Then we loop through the returned categories $cats (always an array) with foreach.

What do you get if you do the following? Does it output all selected categories?

foreach($cats as $cat) {
  echo $cat['catPath'] . '<br>';
}

Thanks bro. That code block only outputs the categories from the first item on the page (https://katchkatikati.xeno.co.nz/business/) :-

business/accommodation/
business/accommodation/motels/

I haven't been able to output the other item categories no matter what I've tried.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Oh you have multiple Collection items! So you'd loop through these too:

$businesses = perch_collection('Businesses', array(
'template' => '/business/featured_layout.html',
'filter' => 'featured_business',
'match' => 'eq',
'value' => 'yes',
'sort' => 'listing_name',
'sort-order' => 'ASC',
'skip-template' => true,
'return-html' => true,
));

$cats = array();

foreach($businesses as $business) {
$catIDs = $business['business_category'];

$cats[] = perch_categories([
'set' => 'business',
'filter' => 'catID',
'match' => 'in',
'value' => implode(',', $catIDs), 
'skip-template' => true
]);
}

foreach($cats as $cat) {
echo $cat[0]['catPath'] . '<br>';
}

Choice. That's now outputting the correct category paths for each item. How do I use them in my html template? Do I need to assign them again to a system var? I was using

<perch:content id="catPath">
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

I think we got our wires crossed here. Where do you want to output the path? In the Collection item card featured_layout.html? In that case you wouldn't need to retrieve the categories in PHP:

<perch:categories id="business_category" set="business">
<perch:category id="catPath" />
</perch:categories>

I probably need to continue along the first line of coding as the above outputs the category path but includes all levels of categories - I only need the parent cat path - and looking through the docs there's no filter to use inside a template, so I get business/accommodation/business/accomodation/motels/entry-title when there is a sub category.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

If you don't want to display sub-categories, you can test against catParentID:

<perch:categories id="business_category" set="business">
  <perch:if id="catParentID" value="0">
    <perch:category id="catPath" />
  </perch:if>
</perch:categories>

Thanks so much for your help mate - got there in the end and learnt a lot about arrays in between. It would be good for the docs to have a few more examples like this.