dongyoufo5672 2015-10-08 14:18
浏览 64

在PHP脚本和类中搜索功能

This is the code of search.php - this part display the search textbox and search button. will be get data form database.

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

This is the code of 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;
}
}

I get this error code:


Notice: Undefined variable: search_term in C:\xampp\htdocs\search\search.php on line 28
Notice: Undefined variable: search_results in C:\xampp\htdocs\search\search.php on line 34

When I run search.php i get 2 errors. I try click search,after search no more error appear. any ideas for this solution? Thank you

  • 写回答

1条回答 默认 最新

  • drus40229 2015-10-08 14:37
    关注

    Juste declare your variables :

    <?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);
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录