nealios Posted March 18, 2008 Share Posted March 18, 2008 Hello, I got this nice bit of code courtesy of http://nodstrum.com/2007/09/19/autocompleter/ Its an Ajax suggest function that im using on a text field to fill a search page. However at the moment it only looks at one field in the table currently surname. Ive played around with it but cant manage to get another field to work. What i want it to do is search a few fields from my table. Here is the php code with it, i dont think the javascript needs to be edited. Can anyone help? thanks <?php // PHP5 Implementation - uses MySQLi. // mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase'); $db = new mysqli('localhost', 'root' ,'root', 'Ocklynge'); if(!$db) { // Show error if we cannot connect. echo 'ERROR: Could not connect to the database.'; } else { // Is there a posted query string? if(isset($_POST['queryString'])) { $queryString = $db->real_escape_string($_POST['queryString']); // Is the string length greater than 0? if(strlen($queryString) >0) { // Run the query: We use LIKE '$queryString%' // The percentage sign is a wild-card, in my example of countries it works like this... // $queryString = 'Uni'; // Returned data = 'United States, United Kindom'; // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE. // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10 $query = $db->query("SELECT surname FROM customer WHERE surname LIKE '$queryString%' LIMIT 10"); if($query) { // While there are results loop through them - fetching an Object (i like PHP5 btw!). while ($result = $query ->fetch_object()) { // Format the results, im using <li> for the list, you can change it. // The onClick function fills the textbox with the result. // YOU MUST CHANGE: $result->value to $result->your_colum echo '<li onClick="fill(\''.$result->surname.'\');">'.$result->surname.'</li>'; } } else { echo 'ERROR: There was a problem with the query.'; } } else { // Dont do anything. } // There is a queryString. } else { echo 'There should be no direct access to this script!'; } } ?> Quote Link to comment Share on other sites More sharing options...
nealios Posted March 19, 2008 Author Share Posted March 19, 2008 Anyone out there that can help? It would really help me out! Quote Link to comment Share on other sites More sharing options...
XoSilenceoX Posted March 26, 2008 Share Posted March 26, 2008 You just need to add the ||(or) indicator into your PHP QUERY. For example surname LIKE ***** || email LIKE || also make sure to define an order for example ORDER BY userid ASC. Doing all this will order them so it can be easily browsed. With the OR command it will check to make sure that it the result you are looking for isn't in both fields. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.