Jump to content

help optimizing my search script w/ AJAX


mikefrederick

Recommended Posts

i have a search script located at http://sites.loado.com/notestock/friendsearch.php

 

You will see that if you slowly type in names of colleges that each letter will change colors, but if you don't do it slowly a lot of times if only does it every couple of letters and you will have typed in letters that don't change. This is my php code

that the AJAX reaches for:

<?php


if(isset($_GET['getCountriesByLetters']) && isset($_GET['letters'])){
$bar = $_GET['letters'];
$replace[0]="-";
$replace[1]="the ";
$replaces[1]="";
$replaces[0]="";

$bar=str_replace($replace,$replaces,$bar);
$replacee[0]=" of ";
$replacee[1]=" in ";
$replacess[1]=" ";
$replacess[0]=" ";
$bar=str_replace($replacee,$replacess,$bar);

$x=explode(" ",$bar);
$search = "WHERE name LIKE '%" . implode("%' and name LIKE '%", $x) . "%' ";
$res=mysql_query("select * from schools " . $search . " order by name limit 7");
while($inf = mysql_fetch_array($res)){
$t=$inf["name"];
foreach(explode(" ",$bar) as $x)
$t= str_replace($x,"<span class='searched'>" . $x . "</span>",$t);
echo $inf["id"]."###".$t."|"; 
 }	
}
?>

 

 

 

any ideas on better optimization strategies? maybe it is dumb to add a span class to each letter

Link to comment
Share on other sites

The more you can cache in the users browser, the better... If the entire XML-dump of your colleges is 25-50kb, then just load all results into javascript, and let the client do the matching. The nice part about real-time results is you don't have to be so loose in your searching. If a typo is made the user knows it instantly.

 

If the xml dump is rather big, i recommend caching it to static xml pages x times a day using cron jobs.. Depending on the size, you can dump colleges starting with a-f in one file, g-m on another, or if that's too big a file for each starting letter.

 

This way you save tons of unnecessary queries :) Let the client machine (JS) do the work :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.