Hello, I want to make a search function on my website so that people can search for other usernames, similar to facebook.
For example say there is Chicohuman on the database, and someone types in Chic, then he would see, if there are, 5 results with usernames starting with Chic in a drop down, if he then types 2 more letters, Chicoh, he would see the usernames that start with these 6 letters in the drop down, that appears automatically.
I am not sure how it is possible without too much queries in the DB, and making every username stored in a cache is probably not the way to go too.
What technologies could I use, any source example?
Thanks for help
'automatic' search function - like on FaceBook
Started by Chicohuman, May 20 2010 01:36 PM
4 replies to this topic
#1
Posted 20 May 2010 - 01:36 PM
|
|
|
#2
Posted 20 May 2010 - 03:45 PM
Unfortunately Chic, you would have to resort to AJAX for this. For with AJAX you can do an "onChange" even on the text box, then request a search of the database.
That's the way, many requests on the database. Of course, you could always make an array using PHP, then itterate it into a javascript array, then that would be in the scripts.
Otherwise, you could start the session variables up and load the array into a session variable, then search the session variable as an array. This would be less load on your database, but mean more load on your server still. It all depends how many members are in the database as well. I don't find searching 10,000 people to be that much of a burden on a quieter system, but if you have 5,000 people tryign to search at the same time, then it may become a little slower, but not by much.
I will get the code for you to see how it works. I've done it before, and it does work. I'll post it up shortly once I have all the code in one place and formatted so it can be displayed here. (unless someone posts it up first, but I have to take out sensitive information from my code.)
That's the way, many requests on the database. Of course, you could always make an array using PHP, then itterate it into a javascript array, then that would be in the scripts.
Otherwise, you could start the session variables up and load the array into a session variable, then search the session variable as an array. This would be less load on your database, but mean more load on your server still. It all depends how many members are in the database as well. I don't find searching 10,000 people to be that much of a burden on a quieter system, but if you have 5,000 people tryign to search at the same time, then it may become a little slower, but not by much.
I will get the code for you to see how it works. I've done it before, and it does work. I'll post it up shortly once I have all the code in one place and formatted so it can be displayed here. (unless someone posts it up first, but I have to take out sensitive information from my code.)
#3
Posted 20 May 2010 - 04:07 PM
\/ =================== In your PHP file. ==================== \/
<script type="text/javascript">
function RequestFields()
{
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("userList").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","request.php",true);
xmlhttp.send("SENT_FIELDNAME="+document.getElementById("SearchFieldName").value);
}
</script>
<form>
<input type="text" name="SearchFieldName" onChange="javascript:RequestFields()" />
<select size="10" name="userList" id="userList">
<option></option>
</select>
</form>
\/ =================== REQUEST.PHP ==================== \/
<?php
if (isset($_POST['SENT_FIELDNAME']))
{
//=============================================================== BEGIN
$DB_Connection = mysql_connect("localhost", "USERNAME","PASSWORD");
mysql_select_db("DATABASE");
$result = mysql_query("select USERNAME from TABLE where FIELD = ".$_POST['SENT_FIELDNAME']);
$counter = count($result);
$lcv = 0;
foreach($result)
{
echo '<option>'.$result['USERNAME'].'</option>';
}
mysql_close($DB_Connection);
//=============================================================== END
}
?>
#4
Posted 20 May 2010 - 08:54 PM
Nice! Thanks a lot.
I will play with it and will write back once it's all working :)
Thanks again.
I will play with it and will write back once it's all working :)
Thanks again.
#5
Posted 20 May 2010 - 09:41 PM
Not a problem my friend, glad to be of assistance to youI look forward to hearing how it works out for you.
If you have troubles, let me know. But should work, since it worked for me.
Take care!
If you have troubles, let me know. But should work, since it worked for me.
Take care!


Sign In
Create Account


Back to top









