We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Live Search in EE 2.0

Development and Programming

Simon Balz's avatar
Simon Balz
34 posts
15 years ago
Simon Balz's avatar Simon Balz

Hi all,

currently I’m working on some live search concepts for EE 2.0. My first idea was just to call the already built-in search module with jquery - which seems to work basically.

But I have some questions to solve before I’m going to work further on a solution like this: - How does the configuration option to restrict the search time interval work? Is there any inheritance from the “Guests”-group to other member groups? Or do I have to change this option for every possible group on my website? - What do I have do expect when setting the search time interval to zero? Does the built-in search module generate huge SQL queries so it’s maybe dangerous using a live search? Or is there any search index generated from home by expressionengine? - What are your recommendations for a proper solution?

Of course if I have the best solution I will publish this to the community.

Thanks in advance Simon

[Mod Edit: Moved to the ExpressionEngine Third Party Development:Modules Discussion forum]

       
giusi's avatar
giusi
94 posts
15 years ago
giusi's avatar giusi

I am facing the same issue. I thought to try to port one of the existing livesearch plugins to EE 2.0.

I am not a real php programmer, so it is not sure i will be successful. I will update my progress here. I hope this article from devot-ee will be helpful.

       
giusi's avatar
giusi
94 posts
15 years ago
giusi's avatar giusi

Still no luck. These are the results of my efforts. The plug-in get recognized from the system that also displays the help and I have no errors during the installation. But i can’t get live results to show. I have the same setup (plugin + template groups and template) on a EE 1.6.9 installation and it works, so the problem is in the plugin itself. I wasn’t able to port the SQL query to Active Record, but I have seen other EE2 plugins that simply use the old queries updating the tables and fields names, so I am not sure the problems is the query. Below the code of the ported plugin, in case someone want to give a look. To try the plugin you need to download the old plugin to get the .js and .css files.

UPDATED; removed the wrong code. New code attached in a reply below.

       
giusi's avatar
giusi
94 posts
15 years ago
giusi's avatar giusi

Just noticed an error in the SQL query, where it reads “b.blog_url” it must be “b.channel_url”.

Anyway the plugin still doesn’t work 😊

       
giusi's avatar
giusi
94 posts
15 years ago
giusi's avatar giusi

Found another error. Used “->result as $row)” instead of the new “>result_array() as $row)”. But still no luck. UPDATE: i found other legacy code and updated the code. UPDATE 2: corrected a syntax error UPDATE 3: in the post below you can find the WORKING version of the plugin for EE 2

       
giusi's avatar
giusi
94 posts
15 years ago
giusi's avatar giusi

It Works!

I was using $this->EE->uri->page_query_string instead of $this->EE->uri->query_string . Lame distraction error 😊

This is the working code, if someone want to try to see if it works on other setups too I would be happy : )

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
=====================================================
 Copyright (c) 2008 Pascal Kriete
-----------------------------------------------------
 http://pascalkriete.com/
=====================================================
 File: pi.live_search.php
-----------------------------------------------------
 Purpose: Generate live-search json results
=====================================================
Changelog:
- Version 2.0
    Updated to EE 2.0
- Version 1.1.0
    Removed trailing comma from js array
    Updated javascript to accept optional parameters
- Version 1.0.2
    Fixed PHP Notice bug
- Version 1.0.1
    Added 'No Results' text
- Version 1.0.0
    Plugin Created
*/

ini_set('display_errors', 1);

$plugin_info = array(
                        'pi_name'            => 'Live Search',
                        'pi_version'        => '2.0',
                        'pi_author'            => 'Pascal Kriete, ported to EE 2.0 by Giuliano Velli',
                        'pi_author_url'        => 'http://pascalkriete.com/',
                        'pi_description'    => 'Generate search results for the accompanying jQuery plugin.',
                        'pi_usage'            => Live_search::usage()
                    );


class Live_search {

    /**
     * Display Search Results
     *
     * @access    public
     * @return    string
     */
    function results()
    {
        $this->EE =& get_instance();

        if ( ! $this->EE->uri->query_string) 
        { 
            return '';
        }
                
        $search_phrase =& $this->EE->uri->query_string;
        $search_pharse = $this->EE->db->escape_str( urldecode($search_phrase) );
        
        /** ---------------------------------------
        /**  Build and run the query
        /** ---------------------------------------*/
        
        $get_results = "SELECT distinct(a.entry_id), a.url_title, a.title, b.channel_url, b.comment_url 
                FROM exp_channel_titles a, exp_channels b
                WHERE a.channel_id = b.channel_id
                AND a.status != 'closed'
                AND (a.expiration_date > '".$this->EE->localize->now."' OR a.expiration_date = '0')
                AND a.title LIKE '%{$search_phrase}%'
                ORDER BY a.title ASC LIMIT 0,10";
            
        $query = $this->EE->db->query($get_results);

        /** ---------------------------------------
        /**  Empty, no point in continuing
        /** ---------------------------------------*/
        
        if ($query->num_rows() == 0) 
        { 
           echo '[{ "title" : "No results", "path" : "#" }]';
            exit;
        }
        
        /** ---------------------------------------
        /**  Process parameters
        /** ---------------------------------------*/
                
        if( $this->EE->TMPL->fetch_param('link') == 'comment')
        {
            $link_to = 'comment_url';
        }
        else
        {
            $link_to = 'channel_url';
        }
        
        /** ---------------------------------------
        /**  Create javascript json array
        /** ---------------------------------------*/

        $data = '[';
        if ($query->num_rows() > 0)
        {
            foreach($query->result_array() as $row) 
            {
                $data .= '{ "title" : "'.$row['title'].'", "path" : "'.$row[$link_to].$row['url_title'].'" }, ';
            }
        }
        // Take off the last comma
        $data = substr($data, 0, -2);
        $data .= ']';
        
        echo $data;
        exit;
    }
    
// ----------------------------------------
//  Plugin Usage
// ----------------------------------------

// This function describes how the plugin is used.
// Make sure and use output buffering

function usage()
{
ob_start(); 
?>
EE Template:

{exp:live_search:results}

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

EE Parameters:

link="comment"
- The channel path to link to.  Options are "channel" and "comment".  Default is "channel".

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

[removed]

Make sure you have an up-to-date version of jQuery included before the plugin javascript.

Parameters:
width            : width of the displayed results
center_results    : boolean to center the result box

Examples:
$('#keywords').livesearch('/search/livesearch/');
$('#keywords').livesearch('/search/livesearch/', { width: '200' });
$('#keywords').livesearch('/search/livesearch/', { center_results: true });
$('#keywords').livesearch('/search/livesearch/', { width: '200', center_results: true });

<?php
$buffer = ob_get_contents();
    
ob_end_clean(); 

return $buffer;
}
// END


}
// END CLASS
?>
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.