Skip to main content
Web Publishing at URI Banner

Web Publishing at URI

Web Publishing at URI » Auto-Complete with jquery, PHP, and mySQL


Auto-Complete with jquery, PHP, and mySQL

June 8th, 2009 by pcho

Note: Based on a jquery plugin by Dylan Verheul, Dan G. Switzer, Anjesh Tuladhar, and Jörn Zaefferer.  Click here to view the original plug in.

Description

To service a quick method to present similar search data results without requesting additional resources from the server, the use of an autocomplete jquery can be used.  This modified version of the plug-in may be used to search through an array of database queries to present such data on-the-fly.

Download

jquery.autocomplete.mysql

Instructions

For quick configurations, open “include/mysql.php“  You should see the following:

$q = strtolower( $_GET["q"] );
if (!$q) return;
 
$dbhost = "localhost";        // Database Host
$dbuser = "";            // User
$dbpass = "";            // Password
$dbname = "";            // Name of Database
 
mysql_connect( $dbhost, $dbuser, $dbpass ) or die( mysql_error() );
mysql_select_db( $dbname ) or die( mysql_error() );
 
// Replace "TABLE_NAME" below with the table you'd like to extract data from
$data = mysql_query( "SELECT * FROM TABLE_NAME" )
or die( mysql_error() );
 
// Replace "COLUMN_ONE" below with the column you'd like to search through
// In between the if/then statement, you may present a string of text
// you'd like to appear in the textbox.
while( $row = mysql_fetch_array( $data )){
if ( strpos( strtolower( $row['COLUMN_ONE'] ), $q ) !== false ) {
echo $row['COLUMN_ONE'] . " - " . $row['COLUMN_TWO'] . "\n";
}
}

Modify the attributes $dbhost, $dbuser, $dbpass, and $dbname to connect to the database you’d like to use for this script.  Next, find “SELECT * FROM TABLE_NAME“  You may modify this statement to which ever mySQL statement you need.  The one used in this example will take every cell from each row of the database and integrate them into the scrript.

Finally, where you fine COLUMN_ONE, you may replace it with an appropriate column from the selected table.  The echo statement below will then allow you to input whatever text and fields you would like to populate into the autocomplete console.

Your may test your script at this time.  Further customization may be done at your discretion.


Leave a Reply

You must be logged in to post a comment.