Jump to content

Suggest field


nealios

Recommended Posts

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!';
	}
}
?>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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