Forum

Thread tagged as: Problem

Search Filtering

Hi,

I'm just wondering whether I am missing a trick here, or have got things completely wrong but....

My aim is to have a search facility on the site. This is all working well, based of the supplied pages and modified templates, i.e. search.php, main-search-form.html and main-search-result.html.

The search bar is in the header of the site (within a layout file) and returns results currently just to the search.php page within the route and this works fine.

I have exposed the 'result-source' so the user can see where results are being pulled from, but what I would like to do is add a 'filter' on the search.php page, which would then return results based on this secondary request.

So, workflow would be. User submits initial search term (search=search-term), which is then displayed on the search.php page using:

<?php
$query = perch_get('search');
perch_content_search($query, array(
'template'=>'main-search-result.html'
 ));
?>

However, at the top of the search.php page, I would like to have a series of links, where I could then filter the delvierd results by the result-source, or rather app.

Just wondering if I can do this and how I could, as I cannot quite think through how I would still keep the 'search-term' and submit it once more into another perch query such as:

<?php
$query = perch_get('search');
perch_content_search($query, array(
'apps' => array('PerchBlog'),
'template'=>'main-search-result.html'
));
?>

I've tried a few things, but not even sure if this is possible.

Any ideas ?

Maybe I have to pass it to another page ? Bit lost.

Any help, greatly appreciated.

Ulitmately, I suppose trying to get to a button or link that would filter the current displayed results.

If I passed the query value into a new page then I could use a new perch call as above to 'filter' on apps, but wondering if I can keep it on the same page, maybe with an if/else statement to pick-up if a filter has been clicked ?????

Thanks.

Andrew Kennedy

Andrew Kennedy 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

The search term is in perch_get('search') so you need to append that to your link. For example:

/search?search=foo&source=PerchBlog

Before searching, check if source has a value.

$query = perch_get('search'); 
$opts = ['template'=>'main-search-result.html'];
if (perch_get('source') {
    $opts['apps'] = [perch_get('source')],
}
perch_content_search($query, $opts));

Remember if you're outputting anything to the page other than through a template you need to make sure you escape any user input.

Thanks Drew, I will have a go now and see if I can get it resolved.

Sorry Drew, that seems to slightly off and I cannot quite get why.

First error is:

$opts['apps'] = [perch_get('source')],

The comma I have removed.

It seems to get stuck with:

unexpected 'perch_content_search' (T_STRING) 

Tried a whole bunch of things, but no joy.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Should be a ;

$opts['apps'] = [perch_get('source')];

I had tried that. When added, I get:

Parse error: syntax error, unexpected ';'
Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't think I can debug this for you line by line without seeing it.

I've tried having a good go at this and still no joy. It all seems to stem from the IF statement.

In my search page, all I have done initially is to replace the my code with yours above. Even if I do not generate the '<a href' source link, the code should just return all results, i.e. unfiltered.

My page in it's most basic guise is:

<?php include($_SERVER['DOCUMENT_ROOT'].'/admin/runtime.php'); ?>
<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if IE 9 ]><html class="ie ie9" lang="en"> <![endif]-->
<!--[if (gte IE 10)|!(IE)]><!-->


<!-- HEADER SETTINGS - JS, CSS ETC -->
<?php perch_layout('global.tuk.header-settings'); ?>
<!-- //HEADER SETTINGS -->  

<?php
$query = perch_get('search'); 
$opts = ['template'=>'main-search-result.html'];
if (perch_get('source') {
    $opts['apps'] = [perch_get('source')];
}
perch_content_search($query, $opts);
?>

Having tried lots of different ways, it seems that the issue lies with:

if (perch_get('source') {
    $opts['apps'] = [perch_get('source')];
}

being within that statement. It just throws an error.

Even just having the if statement without any options does not parse.

Duncan Revell

Duncan Revell 78 points
Registered Developer

Try this:

if (perch_get('source')) {
 $opts['apps'] = [perch_get('source')]; 
}

(missing a ) on line 1)

Thanks Duncan. Cannot believe I missed that ! Oh well, once again, thank you for replying.

All the best.