簡體   English   中英

php腳本和類中的搜索功能

[英]search function in php script and class

這是search.php的代碼-此部分顯示搜索文本框和搜索按鈕。 將從數據庫獲取數據。

<?php
//Just declaration here for null
$search_term =  '';
search_results = false;

//Check if search data was submitted
if (isset($_GET['s'])) {
// Include the search class
require_once( dirname(__FILE__) . '/class-search.php' );

// Instantiate a new instance of the search class
$search = new search();

// Store search term into a variable
$search_term = $_GET['s'];

// Send the search term to our search class and store the result
$search_results = $search->search($search_term);
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Search</title>
</head>
<body>
    <h1>Search</h1>
    <div class="search-form">
        <form action="" method="get">
            <div class="form-field">
                <label for="search-field">Search</label>


              <b>  <input type="search" name="s" placeholder="Enter your search term..." results="5" value="<?php echo $search_term; ?>"> </b>
                <input type="submit" value="Search">
            </div>
        </form>
    </div>
    <?php
    if ($search_results) :
        ?>
        <div class="results-count">
            <p>
                <?php
                echo $search_results['count'];
                ?> results found
            </p>
        </div>
        <div class="results-table">
            <?php
            foreach ($search_results['results'] as $search_result) :
                ?>
                <div class="result">
                    <p>
                        <?php
                        echo $search_result->cust_ic;
                        ?>
                    </p>
                </div>
                <?php
            endforeach;
            ?>
        </div>
        <?php
    endif;
    ?>
</body>
</html>

這是class-search.php的代碼

 class search {

private $mysqli;

public function __construct() {

$this->connect();
}

private function connect() {
$this->mysqli = new mysqli( 'localhost', 'root', 'root', 'abc' );
}


public function search($search_term) {

$sanitized = $this->mysqli->real_escape_string($search_term);

// Run the query
$query = $this->mysqli->query("
  SELECT *
  FROM customer_info
  WHERE cust_name LIKE '%{$sanitized}%'
  OR cust_ic LIKE '%{$sanitized}%'
");

// Check results
if ( ! $query->num_rows ) {
  return false;
}

// Loop and fetch objects
while( $row = $query->fetch_object() ) {
  $rows[] = $row;

}

// Build our return result
$search_results = array(
  'count' => $query->num_rows,
  'results' => $rows,
);

return $search_results;
}
}

我收到此錯誤代碼:


注意 :第28行的C:\\ xampp \\ htdocs \\ search \\ search.php中的未定義變量:search_term
注意:未定義的變量:第34行的C:\\ xampp \\ htdocs \\ search \\ search.php中的search_results

當我運行search.php時,出現2個錯誤。 我嘗試單擊搜索,搜索后不再出現錯誤。 這個解決方案有什么想法嗎? 謝謝

Juste聲明您的變量:

<?php
//Declaration
$search_term =  '';
search_results = false;

//Check if search data was submitted
if (isset($_GET['s'])) {
// Include the search class
require_once( dirname(__FILE__) . '/class-search.php' );

// Instantiate a new instance of the search class
$search = new search();

// Store search term into a variable
$search_term = $_GET['s'];

// Send the search term to our search class and store the result
$search_results = $search->search($search_term);
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM