We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question, Problem

Filtering by _id

Hi,

How do I go about filtering by _id?

I have a one page detail and list view set up, and the detail view uses a different layout. What I'm trying to do is to filter the detail view to match the item that's been selected, but the code below is still outputting all the repeating regions:

        <?php perch_content_custom('Properties', array(
            'template'=>'property/_header_card.html',
            'filter' => '_id',
            'match' => 'eq',
            'value' => $_GET['id'],
        )); ?>

Should I be filtering the URL instead of the _id? And if so, how would I go about filtering the URL in it's current setup below?

<a href="?s=<perch:content id="slug" type="slug" />-<perch:content id="_id" type="hidden" />">

Any help would be much appreciated.

Thanks!

Philip Gwynne

Philip Gwynne 1 points

  • 5 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

It looks as if your querystring contains the slug and the id concatenated together.

I would suggest removing the id and just filtering on the slug.

Thanks Rachel,

The only problem I have with that is there are lots of duplicate slugs due to the client's naming scheme for the property types. Is there a way to filter by id or a different approach I could take that would achieve the same result?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use multiple fields to make up the slug.

If you did just want to use the ID, just filter like this:

'_id' => '1234',

Is there a way to do that dynamically?

I want to show the property detail page based on the slug and ID, and I can't specify a specific ID when the client is adding properties - so I was hoping it was achievable via querying the URL with the ID as part of it.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, absolutely, but you'll need to split the ID out of your slug if you're going to do that.

Brilliant, thanks Drew.

I just had a search on the docs and forums as to how to split the slug but I can't seem to find any results - do you by chance have a link to a relevant document/article that explains it?

I just found some documentation on the site, and I've now implemented the code to split the URL. But the custom_content region is not displaying anything at all - do you know where I'm going wrong?

This is my page:

<?php include('../perch/runtime.php'); ?>
<?php perch_layout('global/header'); ?>

<?php
    perch_content_create('Development', array(
    'template'   => 'development/_detail.html',
    ));
?>

<?php
    perch_content_create('Properties', array(
    'template'   => 'property/_detail.html',
    'multiple'    => true,
    'columns' => 'property_details_title, property_details_plot_number, property_details_type',
    'edit-mode' => 'listdetail',
    ));
?>

<?php
    $parts = explode('-', perch_get('s'));
    $id = array_pop($parts);
    $slug = implode('-', $parts); 
?>

<?php if (perch_get('s')) {
    perch_layout('property');
} else {
    perch_layout('development');
} ?>

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

And this is my custom region on a template within the detail layout:

        <?php perch_content_custom('Properties', array(
            'template'=>'property/_header_card.html',
            'filter'=>array(
            array(
            'filter'=>'slug',
            'match'=>'eq',
            'value'=>$slug
            ),
            array(
            'filter'=>'_id',
            'match'=>'eq',
            'value'=>$id
            ),
            )
        )); ?>

I'm wondering if I've placed the code to split the URL in the wrong place?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You shouldn't need to filter on the slug as this point - the ID makes it unique.

What value does your $id have?

If you turn on debug for the page, what output do you get?

Are you running the latest version of Perch and Blog?

I've now removed the filter on the slug and I wasn't on the latest version, however I've just updated to 2.6.6 and I'm getting the same result (i.e. nothing displaying).

As for the debug mode, I'm getting this where I the filter is called for the relevant template:

Using template: /templates/content/property/_header_card.html
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='72' AND idx.itemRev='30')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev
) as tbl GROUP BY itemID ORDER BY sortval ASC 

And this is the full debug for the page, incase you need it:

DIAGNOSTICS:
SELECT * FROM perch2_pages WHERE pagePath='/developments/poole-court.php' LIMIT 1
Using template: /templates/pages/attributes/default.html
Using sub-template: /templates/pages/attributes/seo.html
SELECT * FROM perch2_pages WHERE pagePath='/developments/poole-court.php' LIMIT 1
SELECT groupID FROM perch2_navigation WHERE groupSlug='about-section' LIMIT 1
SELECT np.pageID, np.pageParentID, p.pagePath, p.pageTitle, p.pageNavText, p.pageNew, p.pageOrder, np.pageDepth, p.pageSortPath, np.pageTreePosition, p.pageAccessTags, p.pageAttributes
FROM perch2_navigation_pages np, perch2_pages p WHERE p.pageID=np.pageID AND np.groupID='10' AND p.pageNew=0 AND np.pageDepth >=0 AND np.pageDepth<=1 ORDER BY np.pageTreePosition ASC
SELECT np.pageTreePosition FROM perch2_pages p, perch2_navigation_pages np WHERE np.pageID=p.pageID AND np.groupID='10' AND p.pagePath='/developments/poole-court.php' LIMIT 1
Using template: /templates/navigation/group_navigation.html
SELECT regionKey, regionHTML FROM perch2_content_regions 
WHERE regionPage='/developments/poole-court.php' OR regionPage='*' ORDER BY regionPage DESC
SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Contact Details' AND (regionPage='/developments/poole-court.php' OR regionPage='*')
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='29' AND idx.itemRev='11')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev
) as tbl GROUP BY itemID ORDER BY sortval ASC 
Using template: /templates/content/global/contact_details_telephone.html
SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch2_pages WHERE pagePath='/developments' OR pageSortPath='/developments' LIMIT 1
SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-002%' AND pageDepth >=1 AND pageDepth<=2 ORDER BY pageTreePosition ASC
SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/developments/poole-court.php' LIMIT 1
SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-002-001', '000-002', '000')
Using template: /templates/navigation/navigation.html
SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch2_pages WHERE pagePath='/buying-from-inspired' OR pageSortPath='/buying-from-inspired' LIMIT 1
SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-003%' AND pageDepth >=1 AND pageDepth<=2 ORDER BY pageTreePosition ASC
SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/developments/poole-court.php' LIMIT 1
SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-002-001', '000-002', '000')
Using template: /templates/navigation/navigation.html
Using template: /templates/navigation/navigation_subitems.html
SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch2_pages WHERE pagePath='/help-to-buy' OR pageSortPath='/help-to-buy' LIMIT 1
SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-004%' AND pageDepth >=1 AND pageDepth<=2 ORDER BY pageTreePosition ASC
SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/developments/poole-court.php' LIMIT 1
SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-002-001', '000-002', '000')
Using template: /templates/navigation/navigation.html
Using template: /templates/navigation/navigation_subitems.html
SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Development' AND (regionPage='/developments/poole-court.php' OR regionPage='*')
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='71' AND idx.itemRev='48')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev
) as tbl GROUP BY itemID ORDER BY sortval ASC 
Using template: /templates/content/property/_header_card_heading.html
SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Properties' AND (regionPage='/developments/poole-court.php' OR regionPage='*')
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='72' AND idx.itemRev='30')) AND ((idx.indexKey=NULL AND idx.indexValue=NULL) OR (idx.indexKey='e' AND idx.indexValue='e') OR (idx.indexKey=NULL AND idx.indexValue=NULL)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev
) as tbl GROUP BY itemID HAVING count(*)=3 ORDER BY sortval ASC 
Using template: /templates/content/property/_header_card.html
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='72' AND idx.itemRev='30')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev
) as tbl GROUP BY itemID ORDER BY sortval ASC 
Using template: /templates/content/global/carousel_image_single_fallback.html
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='71' AND idx.itemRev='48')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev
) as tbl GROUP BY itemID ORDER BY sortval ASC 
Drew McLellan

Drew McLellan 2638 points
Perch Support

What does your perch_content_custom() call look like now?

This is what I'm currently using:

        <?php perch_content_custom('Properties', array(
            'template'=>'property/_header_card.html',
            'filter'=>array(
            'filter'=>$id,
            'match'=>'eq',
            'value'=>perch_get('_id')
            ),
        )); ?>
Rachel Andrew

Rachel Andrew 394 points
Perch Support

And what does your URL look like, including the QueryString?

My URL is setup as the following:

<a href="?s=<perch:content id="slug" type="slug" />-<perch:content id="_id" type="hidden" />">

And outputs as:

http://localhost:8888/developments/poole-court.php?s=the-roker-59

Rachel Andrew

Rachel Andrew 394 points
Perch Support

I don't understand then why you are filtering on

'value'=>perch_get('_id')

You would need a URL parameter of ?_id=

Are you still splitting off the ID at this point, if so the value needs to be your $id variable.

Ah my mistake! I've updated it to the following:

        <?php perch_content_custom('Properties', array(
            'template'=>'property/_header_card.html',
            'filter'=>$id,
            'match'=>'eq',
            'value'=>$id,
        )); ?>

However, it's now showing all properties again rather than showing the property that matches the $id?

I'm wondering if I've set it up correctly in the page...

Drew McLellan

Drew McLellan 2638 points
Perch Support

filter is the name of the field to filter by. value is the value that field should have. The two likely shouldn't be the same.

I understand now. So I've changed the code to filter to "_id", but it still displays all properties rather than filters from the $id taken from the URL:

        <?php perch_content_custom('Properties', array(
            'template'=>'property/_header_card.html',
            'filter'=>'_id',
            'match'=>'eq',
            'value'=>$id,
        )); ?>

I tried changing $id to the _id number of one of the properties, and that works fine, so to me that suggests a problem with the code used to split the URL?

Rachel Andrew

Rachel Andrew 394 points
Perch Support

if you echo $id to the page, so:

<?php echo $id; ?>

What do you see?

It's not actually outputting anything on the page when I call that in the detail layout.

This is the code I have for the page itself:

<?php include('../perch/runtime.php'); ?>
<?php perch_layout('global/header'); ?>

<?php
    perch_content_create('Development', array(
    'template'   => 'development/_detail.html',
    ));
?>

<?php
    perch_content_create('Properties', array(
    'template'   => 'property/_detail.html',
    'multiple'    => true,
    'columns' => 'property_details_title, property_details_plot_number, property_details_type',
    'edit-mode' => 'listdetail',
    ));
?>

<?php
    $parts = explode('-', perch_get('s'));
    $id = array_pop($parts);
    $slug = implode('-', $parts); 
?>

<?php if (perch_get('s')) {
    perch_layout('property');
} else {
    perch_layout('development');
} ?>

<?php perch_layout('global/footer'); ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

If it's not outputting anything, the filter certainly won't work. You need to figure out why your ID isn't populating first.