【问题标题】:ignore entered text after a certain length PHP JQuery Search在一定长度后忽略输入的文本 PHP JQuery 搜索
【发布时间】:2013-08-08 06:52:06
【问题描述】:

我正在使用Tutorial 创建一个搜索引擎。但是,我希望允许用户继续在搜索中输入,但在输入一定数量的文本后让搜索忽略任何内容。

我看到这里

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
}

但我不知道如何做到这一点,所以在用户输入 7 个字符后让用户继续输入,但忽略第 7 个字符之后的任何内容。有什么帮助吗?

【问题讨论】:

    标签: php jquery mysql mysqli


    【解决方案1】:

    试试这个

    $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
          }
      ?>
      

      【讨论】:

        猜你喜欢
        • 2016-04-06
        • 2020-07-09
        • 1970-01-01
        • 2016-05-09
        • 1970-01-01
        • 1970-01-01
        • 2020-06-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多