tags:

views:

21

answers:

3

Hi everyone

I am writing a livesearch using Ajax+PHP+MySQL. I have some question wish you guys help me out. How can I display just the top 10 of the result when I type in the search box? below is the js code. thanks for helps in advance.

function showUser(eleID,str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
+1  A: 

You should limit your search to 10 results in the PHP file when querying the database. Check this out.

thelost
A: 

you should try thinking at the first step. I would require only the first 10 results from the begining, that means in MySQL. Use LIMIT 10 in your mysql query and voila, you got only 10 results. And your scripts will be faster as well.

Ervin
+1  A: 

You probably want to do paging also, use a combination of LIMIT and OFFSET sql statements to help you.

http://www.petefreitag.com/item/451.cfm

Stephen Lee