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

LiveSearch

Development and Programming

Lisa Wess's avatar
Lisa Wess
20,502 posts
18 years ago
Lisa Wess's avatar Lisa Wess

ponders

Anyone up to making something that works like this?

       
Paul Burdick's avatar
Paul Burdick
480 posts
18 years ago
Paul Burdick's avatar Paul Burdick

Hee hee…took me about forty minutes, but I have it working on my local dev site.

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
18 years ago
Lisa Wess's avatar Lisa Wess

paul……………….. begs o great developer, let me play? =)

       
Paul Burdick's avatar
Paul Burdick
480 posts
18 years ago
Paul Burdick's avatar Paul Burdick

Here, more or less, is a step by step process:

  1. Grab his livesearch.js file and create your own file by the same name with its content. In that file, you will have to change the URL for grabbing the search data to one of your templates. I named my template ‘livesearch’ of all things. So find this:
liveSearchReq.open("GET", "./livesearch.php?s=" + document.forms.searchform.s.value);</code></pre>
And change it to this:

<pre><code>liveSearchReq.open("GET", "./index.php/livesearch/" + document.forms.searchform.s.value+"/");

Now, put that file at the root of your EE site.

  1. Create a new template called ‘livesearch’ and have it set to parse PHP on input. Inside this file you will put the PHP code for performing the search and outputting the results. The search term will be sent along in the URL. Here is my quick example:
<?php

global $IN, $DB, $LOC;

if (!$IN->QSTR)
{
    exit;
}

$search_phrase =& urldecode($IN->QSTR);

$query = $DB->query("SELECT distinct(a.entry_id), a.url_title, a.title, b.blog_url 
                                  FROM exp_weblog_titles a, exp_weblogs b 
                                  WHERE a.weblog_id = b.weblog_id 
                                  AND a.status != 'closed' 
                                  AND (a.expiration_date > '".$LOC->now."' OR a.expiration_date = '0') 
                                  AND a.title LIKE '%{$search_phrase}%' 
                                  ORDER BY rand() LIMIT 0,10");

if ($query->num_rows == 0)
{
    exit('No Results');
}

foreach($query->result as $row)
{
    echo '<a href="'.$row['blog_url'].$row['url_title'].'">'.$row['title'].'</a><br />';
}

?>
  1. Determine where you want the search to go on your site. I am using Template 7, so I just substituted the new search for the EE simple search.
<h4>Blog LiveSearch:</h4>
<form onsubmit="return liveSearchSubmit()" name="searchform" method="get" action="/index.php"
>

<input type="text" id="livesearch" name="s" size="15" autocomplete="off" onkeypress="liveSearchStart()" />

</form>

<div id="LSResult" style="display: none;"><div id="LSShadow"></div></div>

Oh, and at the top of the page, you will have to include the javascript file:

<script type="text/javascript" src="livesearch.js"></script>
       
Paul Burdick's avatar
Paul Burdick
480 posts
18 years ago
Paul Burdick's avatar Paul Burdick

Did it work? Was it magic?

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
18 years ago
Lisa Wess's avatar Lisa Wess

I fell asleep, I’m going to implement this in the next hour or so and I’ll let you know if it works. =)

Thank you Paul!

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
18 years ago
Lisa Wess's avatar Lisa Wess

Paul,

I have this live at the moment even though it’s not working. I’m getting very strange results. Would you mind having a look? my site.

I’m sure it’ll quickly be evident what it’s doing wrong. I’ve followed the steps fastidiuously, although it’s possible I made a mistake - I think I’ve got it the way you wrote.

Thanks Paul! =)

       
Paul Burdick's avatar
Paul Burdick
480 posts
18 years ago
Paul Burdick's avatar Paul Burdick

Sleep, yes, I should get more of that myself.

It is odd. In the javascript, you are using: index.php/livesearch/?s=search_term_here, whereas I made it so it went to a template: index.php/weblog/livesearch/search_term_here/. So you should modify it to be like that if you want my example to work. However, I just tried to look at the livesearch template on your site and I get your frontpage. Are you sure you created this page and that it is accessible to all users (even guests)?

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
18 years ago
Lisa Wess's avatar Lisa Wess

ooh ok, I only replaced the filename, stupid, I’ll fix it. =) my livesearch template in in the template group includex - is that a problem? does it need to be in weblog?

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
18 years ago
Lisa Wess's avatar Lisa Wess

http://www.lisa-jill.com/index.php/includes/livesearch/ is the template - it’s definitely there.

I modified that line in the javascript but still getting the same odd result of it repeating the page and overlaid.

All permissions are set to yes for the livesearch template. =)

       
Paul Burdick's avatar
Paul Burdick
480 posts
18 years ago
Paul Burdick's avatar Paul Burdick

It does not need to be in weblog, but you have to make sure you have the correct link to the template (and template group) in the javascript.

This:

liveSearchReq.open(“GET”, “./index.php/includes/livesearch/” + document.forms.searchform.s.value+”/”);

Not this:

liveSearchReq.open(“GET”, “./index.php/livesearch/” + document.forms.searchform.s.value+”/”);

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
18 years ago
Lisa Wess's avatar Lisa Wess

whee, it works. Not I just need to get it formatted properly so that it looks good. Thank you so much, I’m sorry I’m so dumb. =)

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
18 years ago
Lisa Wess's avatar Lisa Wess

One last question, Paul. Can I somehow make this NOT parse a certain weblog? In my old search form I had not=”friends” or some such - all private entries. Possible with this or am I becoming a real thorn? 😊

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
18 years ago
Lisa Wess's avatar Lisa Wess

(must have edit function!)

Also - autocomplete=”off” is not valid and doesn’t appear to affect anything when removed. The rest is valid xhtml transition. Yay. 😊

       
Paul Burdick's avatar
Paul Burdick
480 posts
18 years ago
Paul Burdick's avatar Paul Burdick

Yes, well, if someone was brilliant enough, they would write this up as a plugin so the results could be created using a tag. If you know the weblog’s id, then you can easily just add a.weblog_id != ‘7’ or something like that in the query.

autocomplete is not valid (yet), but Camino, IE, Firefox, and Safari all accept it just fine. Up to you to determine if validity is important enough to do away with it.

       
1 2 3 Last

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.