Advertisement:

Author Topic: On unsuccessful pattern search, always show results without pattern  (Read 2207 times)

Kean

  • Newbie
  • *
  • Posts: 36
Hi,

There is a certain disappointment, when a user makes a search and there is only an empty page displayed.

Would it not be more interesting, if instead of showing an empty page, the user would get the same results as searching an empty field.

I would be grateful if there would be someone who could give pointers in how to achieve this.

Please, comment this idea. Pros / Cons ?

Kean
« Last Edit: August 12, 2015, 03:07:32 pm by Kean »

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Always show search results
« Reply #1 on: June 02, 2015, 12:52:36 am »
Hi,

Interesting. What's your theme? ???

Regards

Kean

  • Newbie
  • *
  • Posts: 36
Re: Always show search results
« Reply #2 on: June 02, 2015, 10:06:50 am »
Hello,

The site is using a modified Flatter design from drizzledesigns.in.

You can find the site itself at pryltorget.fi

Kean

  • Newbie
  • *
  • Posts: 36
Re: Always show search results
« Reply #3 on: June 02, 2015, 10:21:44 am »
Related to search also;

- If the user is looking for summer tires for a car,"SOMMARDÄCK" in one word, they would mostly write just DÄCK i.e. Tires. However, the user would not get any results.

Actually it would be fantastic to have a similar search feature like they have in http://try.discourse.org/ forum software (open source)

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Always show search results
« Reply #4 on: June 02, 2015, 01:28:31 pm »
***CORRECTED***

This procedure should be theme-independent:

Add this at the very bottom of your theme functions.php (take care not to leave blank lines after this):

Code: [Select]
<?php
function cust_withpattern_no_results() {

    if (
Params::getParam('sPattern') && !Search::newInstance()->count()) {
        
$currentParams Params::getParamsAsArray();
        unset(
$currentParams['sPattern']);
        
        
$message sprintf(__('There are no results matching "%s"'osc_theme()), osc_search_pattern());
        
osc_add_flash_info_message($message);
        
        
$url osc_search_url($currentParams);
        
header("Location: ".$url);
        exit; 
    }
}

osc_add_hook("after_search""cust_withpattern_no_results" );
?>


As you can see, I've used a flash message to inform the user that there were no results for his search. Results shown preserve all parameters (location, categories, various filters...) but search keyword.

Quote
- If the user is looking for summer tires for a car,"SOMMARDÄCK" in one word, they would mostly write just DÄCK i.e. Tires. However, the user would not get any results.

This would be a real performance killer. Though you can use this technique to account for prefixes (plurals, derived words):

http://forums.osclass.org/general-help/important-how-to-have-a-really-funcional-search/msg112640/#msg112640

Regards
« Last Edit: August 12, 2015, 05:50:52 pm by teseo »

Kean

  • Newbie
  • *
  • Posts: 36
Re: Always show search results
« Reply #5 on: June 02, 2015, 02:51:38 pm »
Thank you very much.

The solution worked perfectly.

Also, I feel that it improves usability quite a lot.

-----------------------------

One strange phenomenon, was that if I added a "code description" prior to the code in the function file,

like ;

/**
CODE DESCRIPTION
**/

Then nothing worked, but removing it made everything work.

There was no difference between the description I entered and the ones being there by default.

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Always show search results
« Reply #6 on: June 02, 2015, 03:06:11 pm »
You're welcome. :) Would you mind to change the title of this thread to something more descriptive such as "On unsuccessful pattern search, always show results without pattern [SOLVED]"? Thanks.

Regards

Kean

  • Newbie
  • *
  • Posts: 36
Hi,

Actually, I was too hasty to say that this solution works perfectly.

It seems to have a "BUG", not taking into account empty CATEGORIES, or sub-categories that do not have any content. In those case it will show "The page is redirected incorrectly" ...

Regards

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: On unsuccessful pattern search, always show results without pattern
« Reply #8 on: August 12, 2015, 05:53:26 pm »
Hi,

I see. Try my corrected code above.

Quote
// if (!Search::newInstance()->count()) {
if (Params::getParam('sPattern') && !Search::newInstance()->count()) {

Regards

omid_firoznia

  • Newbie
  • *
  • Posts: 37
Re: On unsuccessful pattern search, always show results without pattern
« Reply #9 on: January 31, 2019, 06:00:33 am »
Hi dear

I tried 2 below code that you said on ver 3.7.4 but not worked  :

Code: [Select]
<?php
function cust_withpattern_no_results() {

    if (
Params::getParam('sPattern') && !Search::newInstance()->count()) {
        
$currentParams Params::getParamsAsArray();
        unset(
$currentParams['sPattern']);
        
        
$message sprintf(__('There are no results matching "%s"'osc_theme()), osc_search_pattern());
        
osc_add_flash_info_message($message);
        
        
$url osc_search_url($currentParams);
        
header("Location: ".$url);
        exit; 
    }
}

osc_add_hook("after_search""cust_withpattern_no_results" );
?>

and

Code: [Select]
<?php
function cust_withpattern_no_results() {

    
// if (!Search::newInstance()->count()) {
    
if (Params::getParam('sPattern') && !Search::newInstance()->count()) {
        
$currentParams Params::getParamsAsArray();
        unset(
$currentParams['sPattern']);
        
        
$message sprintf(__('There are no results matching "%s"'osc_theme()), osc_search_pattern());
        
osc_add_flash_info_message($message);
        
        
$url osc_search_url($currentParams);
        
header("Location: ".$url);
        exit; 
    }
}

osc_add_hook("after_search""cust_withpattern_no_results" );
?>