Jump to content

Dynamic Search - Facebook


nealios

Recommended Posts

Hello,On facebook the search box on the side enables you to search for friends and new people.When you type in the name of a person that is already your friend it dynamically creates a drop down list with all the people in your friends that has that name.Ive looked everywhere to find some examples of similar coding to no avail. I have no real idea how to even start, ive looked at W3 schools but there live search works from an XML file rather than connecting to a database.What i want to do is have a customer search within my system that when i type in a first name/surname all the people that meet that criteria will automatically appear.Anyone have any idea of how i can do this?Many thanksnealios

Link to comment
Share on other sites

Ive looked everywhere to find some examples of similar coding to no avail. I have no real idea how to even start, ive looked at W3 schools but there live search works from an XML file rather than connecting to a database.
The W3Schools example is a good place to start, and is very similar to what you want. All you need to do is modify the PHP part so that the information is called from the database instead. Have a good look at that code, you should find what you need. Do you know PHP?
Link to comment
Share on other sites

Thanks for your reply.Ive just started learning php, so my understanding is still very basic. Im not sure where the xml will come in. In W3 example you input the hyperlinks into the xml file. But i need to search my database for the information rather than the xml file? Otherwise each time a new customer is added to the database i would have to manually type it in the xml file.

The W3Schools example is a good place to start, and is very similar to what you want. All you need to do is modify the PHP part so that the information is called from the database instead. Have a good look at that code, you should find what you need. Do you know PHP?
Link to comment
Share on other sites

In terms of performance it would probably be ideal to preload an XML file generated from the database. That will make the search suggestions a lot quicker, but it's only an option if the data set they are searching through is small. So in the facebook example you gave if the person has maybe 50 friends or so, it would be quicker to have Javascript import an XML file containing those 50 people when the page loads. The XML file can be generated using PHP from the information in the database. If, on the other hand, the data set they are searching through is considerably larger, then it wouldn't make sense to have them load a giant XML file if they might not need it. In that case each keypress on the search field would need to trigger a function to look up the string in the database and return partial matches. In either case you're going to have a function get triggered by a keypress to look up the string, the question is if it gets looked up from a preloaded XML data set or if it gets looked up from the database using an AJAX call.In terms of the actual PHP script to look up information, it can be as simple as this:

<?phpif (!$con = @mysql_connect("database_server", "database_user", "database_pass"))  exit("database connection error");if (!@mysql_select_db("database_name", $con))  exit("database selection error");if (!$result = @mysql_query("SELECT username FROM users WHERE username LIKE '" . mysql_real_escape_string($_GET['search']) . "%'", $con))  exit("database query error");while ($row = mysql_fetch_assoc($result))  echo $row['username'] . "\n";?>

That script would get user names from the database that start with whatever was sent through $_GET['search']. Each username is separated by a newline character, so Javascript would get the response, break it apart on newlines, and suggest each name. If there is an error the suggestion would say "database connection error" or whatever. Javascript can use the split method to break apart the string into an array of usernames, and then loop through the array to show each one.This type of thing works for simple data (a single username), if you want to send more complex data to the page (username, age, location, favorite communicative disease, etc) it would be a better solution to use JSON then XML. XML can get pretty complex and the complexity doesn't really pay off all that much, if you use JSON then you can basically create a big object data structure in PHP with all your info in it (an array of "user objects", or "user arrays" or whatever) then use the JSON library to encode the PHP object into a native Javascript object, and then Javascript just reads it and starts to use it, no XML processing necessary. If you want to learn about JSON, the homepage is at json.org. If you want to use JSON in your PHP project, there are some links there to PHP implementations, but the one that I typically use is the one that comes with PEAR, called Services_JSON. You can download the source file for the Services_JSON class here:http://mike.teczno.com/JSON/JSON.phpsAnd there's some documentation for it here:http://mike.teczno.com/JSON/doc/

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...