0

チュートリアルを使用して検索エンジンを作成しています。ただし、ユーザーが検索を入力し続けることを許可し、一定量のテキストが入力された後は検索を無視するようにしたいと考えています。

ここが見える

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文字目以降は無視します。何か助けはありますか?

4

2 に答える 2

1
<?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
    }
?>
于 2013-08-07T03:42:25.017 に答える
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.
}
于 2013-08-07T03:35:24.473 に答える