Forum

Thread tagged as: Question, Runway

Runway: Looping Collections by Category?

What would be the right way to loop through the collections placing them under a new category? I'm trying to output multiple collection entries under separate categories on the same page.

Currently the following outputs the categories but the same category is repeated with just one collection entry each time.

archive.php

<?php
    perch_collection('Knowledge Articles', [
    'category' => 'knowledge-base/'.perch_get('cat'),
    'template' => 'cpi/listings/knowledge_archive.html',
    ]); 
?>

knowledge_archive.html

<div class="card card-5">

    <h3>
          <perch:categories id="categories" set="departments" label="Categories" display-as="dataselect">
              <a href="/knowledge-base/category/<perch:category id="catSlug" type="slug" />" class="p-category">
                <perch:category id="catTitle" type="text" />
              </a>
            </perch:categories>
        </h3>


        <div class="card__menu">
            <ul class="side-nav">
                <li>
                    <a href="">
                    <span class="cat__icon"><i class="ss-icon">&#x1F4D3;</i></span> 
                    <perch:content id="desc" type="text" label="Title" order="1" required="true" title="true" />
                    </a>
                </li>

                <li>
                    <perch:categories id="categories" set="departments" label="Categories" display-as="dataselect">
                        <a href="/knowledge-base/category/<perch:category id="catSlug" type="slug" />" class="p-category">
                            <span class="cat__icon"><i class="ss-icon">&#x27A1;</i></span> 
                            View All Articles
                        </a>
                    </perch:categories>
                </li>                       
            </ul>
        </div>

</div>
Dan Lee

Dan Lee 1 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you order by category, you can then use <perch:if different=""> to only output then heading when it changes.

Dan Lee

Dan Lee 1 points

So are you saying place 'sort' =>'categories', in the function to order by category? Not sure what you mean by "Order by".

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's what I mean. Order by as in sort or arrange in that order.

Dan Lee

Dan Lee 1 points

Ok, I added the sort by categories but it shows none of the collection content. I have included my degbug code below "just in case".

<?php
  perch_collection('Knowledge Articles', [
     'template' => 'cpi/listings/knowledge_archive.html',
     'sort' =>'categories',
  ]); 
?>
Time    Δ  Debug Message
0.1757  0   SELECT * FROM perch2_pages WHERE pagePath='/login/core/runway/start.php' LIMIT 1
0.1954  0.0197  SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate FROM perch2_pages p LEFT JOIN perch2_page_routes pr ON p.pageID=pr.pageID ORDER BY pr.routeOrder ASC
0.1961  0.0007  Using master page: /templates/pages/knowledge_base.php
0.1962  0   Page arguments:
0.2063  0.0101  SELECT * FROM perch2_pages WHERE pagePath='/knowledge-base' LIMIT 1
0.2645  0.0583  SELECT setID FROM perch2_category_sets WHERE setSlug='departments' LIMIT 1
0.2649  0.0003  SELECT main.* FROM perch2_categories main WHERE 1=1 AND setID='3' ORDER BY catTreePosition ASC
0.2654  0.0005  Using template: /templates/categories/menu_department.html
0.2913  0.0259  SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='System Links' AND (regionPage='/knowledge-base' OR regionPage='*')
0.2918  0.0005  SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID=5 AND idx.itemRev=8)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID ORDER BY sortval ASC
0.4022  0.1103  Using template: /templates/content/cpi/systemlinks.html
0.4259  0.0238  SELECT setID FROM perch2_category_sets WHERE setSlug='blog' LIMIT 1
0.4262  0.0003  SELECT main.* , COALESCE(cc.countValue,0) AS `count.blog.post` FROM perch2_categories main LEFT OUTER JOIN perch2_category_counts cc ON cc.catID=main.catID WHERE 1=1 AND (cc.countType='blog.post' OR cc.countType IS NULL) AND cc.countValue > 0 ORDER BY catTreePosition ASC
0.427   0.0008  SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
0.4292  0.0022  Using template: /templates/blog/category_link.html
0.457   0.0277  SELECT setID FROM perch2_category_sets WHERE setSlug='departments' LIMIT 1
0.4573  0.0004  SELECT main.* FROM perch2_categories main WHERE 1=1 AND setID='3' ORDER BY catTreePosition ASC
0.4578  0.0005  Using template: /templates/categories/department.html
0.6112  0.1534  SELECT setID FROM perch2_category_sets WHERE setSlug='knowledge-base' LIMIT 1
0.6115  0.0003  SELECT main.* FROM perch2_categories main WHERE 1=1 AND setID='4' ORDER BY catTreePosition ASC
0.612   0.0004  Using template: /templates/categories/knowledge_articles.html
0.7108  0.0989  SELECT collectionID, collectionTemplate FROM perch2_collections WHERE collectionKey='Knowledge Articles'
0.7112  0.0004  SELECT * FROM ( SELECT idx.itemID, ci.collectionID, ci.itemJSON, idx2.indexValue as sortval FROM perch2_collection_index idx JOIN perch2_collection_items ci ON idx.itemID=ci.itemID AND idx.itemRev=ci.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_revisions cr ON idx.itemID=cr.itemID AND idx.itemRev=cr.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='categories' WHERE (idx.collectionID=2) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID ORDER BY sortval ASC
0.8248  0.1135  Using template: /templates/content/cpi/listings/knowledge_archive.html
0.8254  0.0007  Time: 0.8254
0.8255  0   Memory: 5.0147
Drew McLellan

Drew McLellan 2638 points
Perch Support

Hmm. Try this:

'sort' =>'_category',
Dan Lee

Dan Lee 1 points

Thanks Drew - That seems to work - though I'm not sure why.

Dan Lee

Dan Lee 1 points

Hi Drew

I have been using <perch:if different=""> to output the headings above each category list as you advised. How would I wrap/encase each category list in a <div> detecting where the category changes?

So in basic form i'm wanting it to look like the following...

<div>
  <h2>Category One</h2>
  <ul>
    <li>Collection Item One</li>
    <li>Collection Item Two</li>
  </ul>
</div>

<div>
  <h2>Category Two</h2>
  <ul>
    <li>Collection Item One</li>
    <li>Collection Item Two</li>
  </ul>
</div>

Currently it looks like the following...

<div>
  <h2>Category One</h2>
  <ul>
    <li>Collection Item One</li>
    <li>Collection Item Two</li>
  </ul>

  <h2>Category Two</h2>
  <ul>
    <li>Collection Item One</li>
    <li>Collection Item Two</li>
  </ul>
</div>
Drew McLellan

Drew McLellan 2638 points
Perch Support

If it's a different category, and it's not the first item, output a </div>.

<perch:if different="category">
    <perch:if exists="perch_item_first"><perch:else /></div></perch:if>
    <div>
        <h2> .... </h2>
</perch:if>
Dan Lee

Dan Lee 1 points

Thanks Drew!

Dan Lee

Dan Lee 1 points

How would I count 5 items in a category

Dan Lee

Dan Lee 1 points

How would I show only 5 items per category? - I have tried using <perch:every> to do this but noticed that it skips an item out of the list.

<perch:if different="categories">
    <perch:if exists="perch_item_first">

    <perch:else />
                </ul>
            </div>
        </div>
        </div>
    </perch:if>

    <div class="large-6 small-12 columns">
    <div class="card card-5">

            <h3>
                <perch:categories id="categories" set="departments">
                  <a href="/knowledge-base/<perch:category id="catSlug" type="slug" />" class="p-category">
                    <perch:category id="catTitle" type="text" />
                  </a>
                </perch:categories>
            </h3>

            <div class="card__menu">
                <ul class="side-nav">

                <li>
                    <perch:categories id="categories" set="departments">
                        <a href="/knowledge-base/<perch:category id="catSlug" type="slug" />">
                        <span class="cat__icon"><i class="ss-icon">&#x27A1;</i></span> 
                        View All Articles
                        </a>
                    </perch:categories>
                </li>


</perch:if>

<perch:every count="5">
<perch:else />

                    <li>
                        <a href="/knowledge-base/<perch:categories id="categories" set="departments"><perch:category id="catSlug" type="slug" /></perch:categories>/<perch:content id="titleSlug" type="slug" for="title" label="Slug" editable="true" order="2" />">
                        <span class="cat__icon"><i class="ss-icon">&#x1F4D3;</i></span> 
                        <perch:content id="title" type="text" label="Title" order="1" required="true" title="true" />
                        </a>
                    </li>

</perch:every>
Drew McLellan

Drew McLellan 2638 points
Perch Support

<perch:every count="5"> will output every 5 items, not the first 5.

You should be able to use nth-child for this. Try -n+5