I will tell you about implementation autocomplete for your sites. Data
can be located in different sources – directly in the JS code, in the
database, and even in the XML file.
Step 1. HTML
Here are HTML layout for our autocomplete example page:
index.html
04 | <meta charset= "utf-8" /> |
05 | <title>Autocomplete with PHP, jQuery, MySQL and XML | Script Tutorials</title> |
07 | <link href= "css/jquery.autocomplete.css" rel= "stylesheet" type= "text/css" /> |
08 | <link href= "css/main.css" rel= "stylesheet" type= "text/css" /> |
09 | <script type= "text/javascript" src= "js/jquery-1.5.2.min.js" ></script> |
10 | <script type= "text/javascript" src= "js/jquery.autocomplete.pack.js" ></script> |
11 | <script type= "text/javascript" src= "js/script.js" ></script> |
14 | <div class = "container" > |
16 | <p><label>Your month:</label> <input id= "month" type= "text" autocomplete= "off" ></p> |
17 | <p><label>Your year:</label> <input id= "year" type= "text" autocomplete= "off" ></p> |
18 | <p><label>Your country:</label> <input id= "country" type= "text" autocomplete= "off" ></p> |
22 | <h2>Autocomplete with PHP, jQuery, MySQL and XML</h2> |
Step 2. CSS
Now, lets define all used styles:
css/main.css
007 | background-repeat : no-repeat ; |
008 | background-color : #bababa ; |
009 | background-image : -webkit-radial-gradient( 600px 200px , circle , #eee , #bababa 40% ); |
010 | background-image : -moz-radial-gradient( 600px 200px , circle , #eee , #bababa 40% ); |
011 | background-image : -o-radial-gradient( 600px 200px , circle , #eee , #bababa 40% ); |
012 | background-image : radial-gradient( 600px 200px , circle , #eee , #bababa 40% ); |
014 | font : 14px / 1.3 Arial , sans-serif ; |
019 | background-color : #212121 ; |
021 | box-shadow: 0 -1px 2px #111111 ; |
040 | footer a.stuts,a.stuts:visited{ |
042 | text-decoration : none ; |
047 | margin : 23px 0 0 110px ; |
059 | border : 3px #111 solid ; |
068 | -moz-border-radius: 15px ; |
069 | -webkit-border-radius: 15px ; |
073 | border : solid 1px #E5E5E5 ; |
076 | border-radius: 0 0 5px 5px ; |
077 | -moz-border-radius: 0 0 5px 5px ; |
078 | -webkit-border-radius: 0 0 5px 5px ; |
081 | background-color : #444 ; |
090 | background-color : #FFFFFF ; |
091 | background : -moz-linear-gradient( top , #FFFFFF , #EEEEEE 1px , #FFFFFF 25px ); |
092 | background : -webkit-gradient(linear, left top , left 25 , from( #FFFFFF ), color-stop( 4% , #EEEEEE ), to( #FFFFFF )); |
093 | border : solid 1px #E5E5E5 ; |
100 | -moz-border-radius: 5px ; |
101 | -webkit-border-radius: 5px ; |
103 | box-shadow: rgba( 0 , 0 , 0 , 0.1 ) 0px 0px 8px ; |
104 | -moz-box-shadow: rgba( 0 , 0 , 0 , 0.1 ) 0px 0px 8px ; |
105 | -webkit-box-shadow: rgba( 0 , 0 , 0 , 0.1 ) 0px 0px 8px ; |
108 | form input:hover, form input:focus { |
109 | border-color : #C9C9C9 ; |
111 | box-shadow: rgba( 0 , 0 , 0 , 0.5 ) 0px 0px 8px ; |
112 | -moz-box-shadow: rgba( 0 , 0 , 0 , 0.5 ) 0px 0px 8px ; |
113 | -webkit-box-shadow: rgba( 0 , 0 , 0 , 0.5 ) 0px 0px 8px ; |
In our package you can find few more files:
css/jquery.autocomplete.css + css/indicator.gif
Both files I got from autocomplete jquery package (this is default files – don`t need to re-publish it in our article)
Step 3. Javascript
Its time to prepare JS:
js/script.js
03 | $( '#month' ).autocomplete([ 'January' , 'February' , 'March' , 'April' , 'May' , 'June' , 'July' , 'August' , 'September' , 'October' , 'November' , 'December' ], { |
08 | $( '#year' ).autocomplete( 'data.php?mode=xml' , { |
13 | $( '#country' ).autocomplete( 'data.php?mode=sql' , { |
As you can see – very easy syntax of using Autocomplete. In first
case I hardcoded possible values directly in JS code. Second and third
cases – through PHP file (using different way of obtaining data – XML
and SQL). In package you can find two another JS files:
js/jquery-1.5.2.min.js + js/jquery.autocomplete.pack.js
This is jQuery library itself plus Autocomplete plugin
Step 4. SQL
Now, lets prepare our database – lets add 1 new table:
01 | CREATE TABLE `s85_countries` ( |
02 | `country_code` varchar (2) NOT NULL , |
03 | `country_name` varchar (255) NOT NULL , |
04 | PRIMARY KEY (`country_code`) |
05 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8; |
07 | INSERT INTO `s85_countries` (`country_code`, `country_name`) VALUES |
17 | ( 'US' , 'United States' ); |
This small table contain several records – list of countries. I took that SQL code from one of our old tutorials.
Step 5. PHP
This step most important – now you will see how we returning data for Autocomplete:
data.php
03 | if (version_compare(phpversion(), "5.3.0" , ">=" ) == 1) |
04 | error_reporting (E_ALL & ~E_NOTICE & ~E_DEPRECATED); |
06 | error_reporting (E_ALL & ~E_NOTICE); |
08 | require_once ( 'classes/CMySQL.php' ); |
10 | $sParam = $GLOBALS [ 'MySQL' ]->escape( $_GET [ 'q' ]); |
13 | switch ( $_GET [ 'mode' ]) { |
15 | $aValues = $aIndexes = array (); |
16 | $sFileData = file_get_contents ( 'data.xml' ); |
17 | $oXmlParser = xml_parser_create( 'UTF-8' ); |
18 | xml_parse_into_struct( $oXmlParser , $sFileData , $aValues , $aIndexes ); |
19 | xml_parser_free( $oXmlParser ); |
21 | $aTagIndexes = $aIndexes [ 'ITEM' ]; |
22 | if ( count ( $aTagIndexes ) <= 0) exit ; |
23 | foreach ( $aTagIndexes as $iTagIndex ) { |
24 | $sValue = $aValues [ $iTagIndex ][ 'value' ]; |
25 | if ( strpos ( $sValue , $sParam ) !== false) { |
31 | $sRequest = "SELECT `country_name` FROM `s85_countries` WHERE `country_name` LIKE '%{$sParam}%' ORDER BY `country_code`" ; |
32 | $aItemInfo = $GLOBALS [ 'MySQL' ]->getAll( $sRequest ); |
33 | foreach ( $aItemInfo as $aValues ) { |
34 | echo $aValues [ 'country_name' ] . "\n" ; |
We filter the resulting data by incoming parameter $_GET['q'] from
the active text field (where we started typing something). The result –
the script gives all matching records. Another one file which we using
(as always):
classes/CMySQL.php
This is our usual class file to work with database (pretty
comfortable). In its constructor you will able to set your own database
configuration:
1 | $this ->sDbName = '_DATABASE_NAME_' ; |
2 | $this ->sDbUser = '_DATABASE_USERNAME_' ; |
3 | $this ->sDbPass = '_DATABASE_USERPASS_' ; |
Step 6. XML
Here are content of our XML data file:
data.xml
0 comments:
Post a Comment