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
https://forums.phpfreaks.com/topic/102161-help-optimizing-my-search-script-w-ajax/
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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.