ignore entered text after a certain length PHP JQuery Search

懵懂的女人 提交于 2019-12-25 05:37:11

问题


I'm using the Tutorial to create a search engine. However I want the user to be allowed to keep typing in the search but make the search ignore anything after a certain amount of text has been entered.

I see right here

include_once ('database_connection.php');//Including our DB Connection file
if(isset($_GET['keyword'])){//IF the url contains the parameter "keyword"
 $keyword =     trim($_GET['keyword']) ;//Remove any extra  space
$keyword = mysqli_real_escape_string($dbc, $keyword);//Some validation

$query = "select topictitle,topicdescription from topics where topictitle like '%$keyword%' or topicdescription like '%$keyword%'";
//The SQL Query that will search for the word typed by the user .

$result = mysqli_query($dbc,$query);//Run the Query
if($result){//If query successfull
 if(mysqli_affected_rows($dbc)!=0){//and if atleast one record is found
 while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ //Display the record
 echo '<p> <b>'.$row['topictitle'].'</b> '.$row['topicdescription'].'</p>'   ;
 }
 }else {
 echo 'No Results for :"'.$_GET['keyword'].'"';//No Match found in the Database
 }

}
}else {
 echo 'Parameter Missing in the URL';//If URL is invalid
}

But I don't know how I can make it so after the user has typed 7 charactors let the user keep typing but ignore anything after the 7th charactor. Any help?


回答1:


Try this

$keyword   =     trim($_GET['keyword']) ;//Remove any extra  space
$keyword   =     mysqli_real_escape_string($dbc, $keyword);//Some validation
if(strlen($keyword) > 7){
  $keyword =    substr($keyword,0,7);//This will give you first 7 characters if the user input is greater than seven characters in length.
}



回答2:


<?php 
    include_once ('database_connection.php');//Including our DB Connection file
    if(isset($_GET['keyword'])){//IF the url contains the parameter "keyword"

            $keyword=$_GET['keyword'];
        if(strlen($_GET['keyword'])>7)
        {
            $keyword= substr($_GET['keyword'],0,7);
        }
        $keyword =     trim($keyword) ;//Remove any extra  space
        $keyword = mysqli_real_escape_string($dbc, $keyword);//Some validation
        $query = "select topictitle,topicdescription from topics where topictitle like '%$keyword%' or topicdescription like '%$keyword%'";
        //The SQL Query that will search for the word typed by the user .
        $result = mysqli_query($dbc,$query);//Run the Query
        if($result){//If query successfull
             if(mysqli_affected_rows($dbc)!=0){//and if atleast one record is found
                 while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ //Display the record
                     echo '<p> <b>'.$row['topictitle'].'</b> '.$row['topicdescription'].'</p>'   ;
                 }
             }
            else {
                 echo 'No Results for :"'.$_GET['keyword'].'"';//No Match found in the Database
             }
        }

    }
    else {
         echo 'Parameter Missing in the URL';//If URL is invalid
    }
?>


来源:https://stackoverflow.com/questions/18094180/ignore-entered-text-after-a-certain-length-php-jquery-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!