php - Improving Ajax auto-complete to return better result -


i using ajax & php fetch elements matches keys(letters) entered user elements resembling database.

for e.g database contain manufacturer names as:

hydfloo, rexflex, easton, vickters, ever gush, thomas hydraulics, avt pumps 

and example user has entered letter "h" input box. receive hydfloo return , not thomas hydraulics along it. if "p" typed wd expect see "avt pumps". changes need make php in order able have values returned matches either first or second word of manufacturer name.

php code

 <?php     require('../config/connection.php');      if(!$dbc) {          echo 'could not connect database.';     } else {  if(isset($_post['querystring'])) {     $querystring = mysqli_real_escape_string($dbc, $_post['querystring']);      if(strlen($querystring) >0) {    $query = mysqli_query($dbc, "select distinct name prdct_subcat name '$querystring%' limit 10");   echo '<ul>';    while ($result = mysqli_fetch_assoc($query)) {    echo '<li onclick="fill(\''.addslashes($result['name']).'\');">'.$result['name'].'</li>';             }         echo '</ul>';               }          }      } ?> 

use regular expression instead of like, can match word boundary anywhere in value.

change

where name '$querystring%' 

to:

where name regexp '[[:<:]]$querystring' 

in sql.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -