Forum

Thread tagged as: Problem, Runway

Detail one Collection item but list other collection items on same page

Hello, all! I've got a Perch Runway Collection called "Properties" and am displaying a detail view of same on a properties.php page like so:

<?php 
  $properties = perch_collection('Properties', [ 
    'template' => 'properties.html', 
    'filter' => 'property_slug',
    'match' => 'eq', 
    'value' => perch_get('s'),
    'count' => 1,
    'skip-template' => true,
    'return-html' => true,
  ]);
?>
<?php echo $properties['html']; ?>

This works well, but at the bottom of the page I want to display links to other items in the collection; the ones that do not match on the property_slug.

I tried this:

<?php 
  $properties = perch_collection('Properties', [ 
    'template' => 'properties.html', 
    'filter' => 'property_slug',
    'match' => 'eq', 
    'value' => perch_get('s'),
    'count' => 1,
    'skip-template' => true,
    'return-html' => true,
  ]);
   $properties_grid = perch_collection('Properties', [
    'template'  => 'properties-grid.html',
    'filter'    => 'property_slug',
    'match'     => 'neq',
    'value'     => perch_get('s'),
    'skip-template' => true,
    'return-html' => true,
  ]);
?>
<?php echo $properties['html']; ?>
<?php echo $properties_grid['html']; ?>

...but where $properties_grid should echo, there's nothing, and there's no obvious error in the debugger.

Joel Davies

Joel Davies 0 points

  • 4 years ago

You don't need skip-template or return-html, or to assign the function calls to variables. Just call perch_collection and pass in the parameters

I think that was one of the first things I tried:

<?php 
  $properties = perch_collection('Properties', [ 
    'template' => 'properties.html', 
    'filter' => 'property_slug',
    'match' => 'eq', 
    'value' => perch_get('s'),
    'count' => 1,
    'skip-template' => true,
    'return-html' => true,
  ]);
?>
<?php echo $properties['html']; ?>

<p>Argle</p>
<?php
perch_collection('Properties', [
    'template'  => 'properties-grid.html',
    'filter'    => 'property_slug',
    'match'     => 'neq',
    'value'     => perch_get('s'),
  ]);
?>
<p>Bargle</p>

The properties.html echos, but the properties-grid.html does not. I get nothing between "Argle" and "Bargle".

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does debug tell you?

I'm not seeing any missing files or variables in Debug, but I'm not particularly adept yet at reading it yet:

Debug Message
[5] 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, p.pagePath ASC
Matched route: property/[slug:s]
Using master page: /templates/pages/property.php
Page arguments:

Array
(
    [0] => /property/cowabunga-bluffs
    [s] => cowabunga-bluffs
    [1] => cowabunga-bluffs
)

[1] SELECT collectionID, collectionTemplate FROM perch2_collections WHERE collectionKey='Properties'
[1] 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='_order' WHERE (idx.collectionID=3) AND ((idx.indexKey='property_slug' AND idx.indexValue='cowabunga-bluffs')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, itemJSON, sortval ORDER BY sortval ASC LIMIT 0, 1
[2] Using template: /templates/content/properties.html
[1] SELECT * FROM perch2_collections WHERE collectionKey='States' LIMIT 1
[1] SELECT * FROM perch2_collection_items ci, perch2_collection_revisions r WHERE r.itemID=ci.itemID AND ci.collectionID=2 AND ci.itemRev=r.itemRev AND ci.itemID=53
[1] SELECT * FROM perch2_collection_items ci, perch2_collection_revisions r WHERE r.itemID=ci.itemID AND ci.collectionID=2 AND ci.itemRev=r.itemRev AND ci.itemID=53
[1] SELECT * FROM perch2_collection_items ci, perch2_collection_revisions r WHERE r.itemID=ci.itemID AND ci.collectionID=2 AND ci.itemRev=r.itemRev AND ci.itemID=53
[1] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/property' OR regionPage='*' ORDER BY regionPage DESC
[1] SELECT * FROM perch2_pages WHERE pagePath='/property' LIMIT 1
Using template: /templates/pages/attributes/default.html
Using sub-template: /templates/pages/attributes/seo.html
Using sub-template: /templates/pages/attributes/facebook-image.html
[1] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Contact Form' AND (regionPage='/property' OR regionPage='*')
[nil] 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=19 AND idx.itemRev=0)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval ORDER BY sortval ASC
Using template: /templates/content/contact-form.html
[3] 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='_order' WHERE (idx.collectionID=3) AND ((idx.indexKey='property_slug' AND idx.indexValue != 'cowabunga-bluffs')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, itemJSON, sortval ORDER BY sortval ASC
[3] Using template: /templates/content/properties-grid.html
[12] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
Request time: 0.1949
Process time: 0.1831
Memory: 2.562
Drew McLellan

Drew McLellan 2638 points
Perch Support

It looks like 3 items are being templated with properties-grid.html. Does that sound about right?

Actually, the main template for properties is properties.html. It's both the template for Perch Admin input and the main output template for properties.php.

I wrote properties-grid.html just to handle layout of a "Other Regional Properties" section at the bottom of the properties.php page. It should display all the other Properties except the one that is being detailed.

  <perch:related id="properties_other" collection="Properties">
     <perch:before>
     <section id="other-properties">
       <div class="row">
         <div class="small-12 columns">
           <h3>Other Regional Properties</h3>
           <div class="properties-grid">
             <div class="row small-up-1 medium-up-4">               
     </perch:before>
                <div class="column text-center">
                  <a href="/property/<perch:content id="property_slug" />/">
                    <div class="properties-grid-wrapper">
                      <img src="<perch:content id="property_thumbnail" type="image" />" alt="<perch:content id="property_name" />">
                    </div>
                    <h2><perch:content id="property_name" /></h2>
                    <perch:content id="property_city" />, <perch:content id="state_full" />
                  </a>
                </div>
    <perch:after>
            </div>
           </div><!-- END .properties-grid -->
         </div>
       </div>
     </section>
    </perch:after>
  </perch:related>
Drew McLellan

Drew McLellan 2638 points
Perch Support

You've got the whole thing wrapped in a perch:related tag - what's the purpose of that?

That code block, in the page, without <perch:related> does produce something, but only shows the item featured on the page already.

I want to display the other items in the collection, or at least links to them.

I guess what I am trying to stumble toward could be called a "self-join" but I don't know how to achieve that in Runway.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you moved on from the original question? I thought you wanted to display the matching property, and then after that other properties from the same collection. None of that requires a relationship or a self-join.

Yes, my goal is to simply to display other properties from the collection on the same page as detail from one item in that same collection.

The fact that I don't seem to be able to get it to work made me wonder if conceptually what I was trying to do was a self-join, which perhaps is not doable in Runway? I'm not a db expert but I know from working in FileMaker you often have to have intermediate tables to get self-joins to work.

I'm happy to not mess with self-joins if I don't have to!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Just sticking to the default template for illustrative purposes, does this do what you want?

perch_collection('Properties', [ 
    'filter' => 'property_slug', 
    'match' => 'eq',  
    'value' => perch_get('s'), 
    'count' => 1,
]);

perch_collection('Properties', [ 
    'filter' => 'property_slug', 
    'match' => 'neq',  
    'value' => perch_get('s'), 
]);

Thanks, Drew, that was a helpful suggestion to debug this.

I modified my default template to add the two perch_collection blocks as you suggested. That successfully pulled all the Properties to the page, with the target property at the top. By modifying it further:

`<?php if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php'); ?>
<?php   perch_layout('global/header', ['body-class' => 'home',]); ?>

<?php perch_layout('global/property-top-nav'); ?>

<?php perch_content('Main heading'); perch_pages_navigation(['levels'=>1]); perch_content('Intro'); ?>

<?php
perch_collection('Properties', [ 
    'filter' => 'property_slug', 
    'match' => 'eq',  
    'value' => perch_get('s'), 
    'count' => 1,
]);
?>

<h1>OTHER REGIONAL PROPERTIES</h1>

<?php
perch_collection('Properties', [
    'template' => 'properties-grid-xrelated.html',
    'filter' => 'property_slug', 
    'match' => 'neq',  
    'value' => perch_get('s'), 
]);
?>

<?php perch_layout('global/footer');`

...I got what I need, which is the detail layout, then the properties-grid below.

The file properties-grid-xrelated.html is the same version of my previous file, with the <perch:related> tag removed as you suggested above. Oddly, yesterday when I simply removed <perch:related> from the previous file and refreshed, that didn't work. Saving a new file with a new name seemed to do the trick. Are the templates cached?

Anyway, I next copied the same perch_collection call in my property.php file and that also worked (mostly).

Now I have a niggling issue relating to a Collection item ("States") inside my Collection items ("Properties") returning an array number instead of a value in the above properties-grid. I think perhaps I will mark this topic as solved and start a new one.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are the templates cached?

PHP caches all files to some extent. If you're in development mode we attempt to override that.

Documentation of PERCH_PRODUCTION_MODE is a bit sparse. On the Troubleshooting Tips page I read:

If you set PERCH_PRODUCTION_MODE to PERCH_PRODUCTION in your config then they will not cache and you should see changes immediately.

Shouldn't it be PERCH_DEVELOPMENT mode in which caching is overridden? I would think PERCH_PRODUCTION would be the default live mode in which caching is fully leveraged.

Also, how do you set that? I presume in /config/config.php like this...

    define('PERCH_PRODUCTION_MODE', PERCH_DEVELOPMENT);

Or should it be...

    define('PERCH_PRODUCTION_MODE', 'PERCH_DEVELOPMENT');
Drew McLellan

Drew McLellan 2638 points
Perch Support

No, it should be the constant:

define('PERCH_PRODUCTION_MODE', PERCH_DEVELOPMENT);