On the fly searching results as you type.

I have found this little bit of code vary useful and I thought I would give it to you all.

The basic idea here is that as an individual types something in an text field the java script runs another php file with every keystroke and yields a result in a

somewhere on the page.

Its not as complicated as one might think.

This example will show you how to search an array of fruit but you can use this to display anything you want and run any php code you want. I often times use this to do LDAP and MySQL queries on the fly.

This is the javascript for the keypress feature. This needs to be on the same page as the your typing into, I generally add it in the section of my pages as a
[/php]

This is the actual code for the field and display area. field you want to use. Notice there is no submit button, that is because every thing happens when you type not when you hit submit so it is not needed.
[php]<form method=“get” action="<?php $_SERVER["PHP_SELF"] ?>">

Search:
[/php] The issue with this code is if someone hits return to submit the form you end up right back at a blank page. So.. a little e if then statement and problem resolved.

[php]<?php

if (!$_REQUEST){ // if there is nothing in the parameter fields on the URL do this.

?>

"> Search:
<?php } else { // if there is something in the parameter fields do this. ?> "> Search:
" />
<?php } ?>[/php]

Now on our second file we actually preform the search of our array.

[php]<?php
$fruit = array(“apple”,“orange”,“grape”,“tomato”,“cherry”,“watermelon”); // this is our array

foreach ($fruit as $key => $value) { // this does just what it says it does… for every value in the array take the key and out put the value as a variable $value
$pos = strpos($value, $_REQUEST[‘search’]); // This searches every value in the array for a series of letters that matches what you typed
if ($pos === false) { // if no results exists do nothing
} else {
echo $value."
";
}
}

?>[/php]

and there you have it an array searched on the fly by the letters in the individual words in the array.

Here are the two files completed.

index.php:
[php]

<?php if (!$_REQUEST){ // if there is nothing in the parameter fields on the URL do this. ?> "> Search:
<?php } else { // if there is something in the parameter fields do this. ?> "> Search:
" />
<?php } ?>[/php]

search.php:
[php]<?php
$fruit = array(“apple”,“orange”,“grape”,“tomato”,“cherry”,“watermelon”);

foreach ($fruit as $key => $value) {
$pos = strpos($value, $_REQUEST[‘search’]);
if ($pos === false) {
} else {
echo $value."
";
}
}

?>[/php]

I saw you link to this when somene asked for search. These are neat User features but I would like to warn that this in a production enviorment would cause massive uneccary searchs. Fill the mysql Cache with worthless partial word searches and slow down the overall site as well as the search function.

Some cleanups would be to run a seperate Sql call and make sure you tag it Sql NOCACHE.
Also don’t start your search looping until you reach 3 letters typed. Theres no indexing below that anyway.
Also place in your page somewhere NOT to bring up this feature becuase many users are disabling javascript these days because of virus injection from webads via Flash.
Finally I would shorten and seperate the list to Previously searched for keywords that you generate and HARD FILE that list daily. Let the buffers of an array under 4 mb bring back your speed. I see your example of the fruit but realisically many coders try to dump a Mysql query into that array.

I use this method on many live sites and it works very well. With SQL queries as well as LDAP queries. It has not caused site slowdowns and I have not had cacheing issues. You are more than welcome to look at the live versions if you would like

degrees.utah.edu
gradschool.utah.edu/centers

Your not looking through 2gig + of data from what I can see. Small Db’s can use this feature with ease was not trying to say NEVER use it just don’t hang fuzzy dice on a tank mirror:)

Sponsor our Newsletter | Privacy Policy | Terms of Service