首页 > 解决方案 > 提前搜索选择城市,访问地点,关键字而不是结果

问题描述

我想在关键字之后搜索选择城市最佳访问地点,而不是在结果下方搜索单击。所有类别,如城市->访问地点->关键字。我有一些错误请分享这个想法。

我有两张桌子 'tourist_city' 和 'visiting_places'
1>tourist_city

        city_id | city    | is_enabled
        ==============================
              1 | Delhi   |        1
        ==============================
              2 | Mumbai  |        1
        ==============================
              3 | Goa     |        1
        ==============================
              4 | kolkata |        1
        ===============================

        2> visiting_places

        vid | city_id | visiting_place  | history                         |is_enabled
        ==============================================================================
          1 |       1 |Red Fort         |The Red Fort was the residence.. |         1
        ==============================================================================
          2 |       1 |Lotus Temple     | The Lotus Temple, new Delhi     |         1
        ==============================================================================
          3 |       2 |Gateway of India |The Gateway of India.....        |         1
        ==============================================================================
          4 |       2 |Elephanta Caves  |Elephanta Caves are a network..  |         1
        ==============================================================================
          5 |       3 |Marine Drive     |Marine Drive is a 3.5-kilometre..|         1
        ==============================================================================
          6 |       3 |Fort Aguada      |The fort was constructed in..    |         1
        ==============================================================================
          7 |       4 |Victoria Memorial|The Victoria Memorial is a large |         1
        ==============================================================================
          8|        4 |Dakhshineswar    |The Victoria Memorial is a large |         1
        ==============================================================================

数据库在此处的代码下方工作正常...

        db.php
        <?php 
        class Db {
            private $hostname = 'localhost';
            private $username = 'root';
            private $password = '';
            private $database = 'test';
            private $conn;
            public function __construct() { 
                $this->conn = mysqli_connect($this->hostname, $this->username, $this->password, $this->database); 
                if(!$this->conn) {
                    echo 'Database not connected';
                }
            }
            public function getTouristCity(){
                $query = "SELECT * FROM tourist_city WHERE is_enabled = '1'";
                $result = mysqli_query($this->conn, $query);
                return $result;
            }
            public function getVisitingPlaces(){
                $query = "SELECT * FROM visiting_places WHERE is_enabled = '1'";
                $result = mysqli_query($this->conn, $query);
                return $result;
            }
            public function getVisitinPlaceData($cityid, $placeid , $keyword){
                $sWhere = '';
                $where = array();
                if($cityid > 0) {
                    $where[] = 'V.city_id = '.$cityid.' AND V.is_enabled = "1"';
                }
                if($placeid > 0) {
                    $where[] = 'V.vid = '.$placeid;
                }
                if($keyword != '') {
                    $keyword = trim($keyword);
                    $where[] = "( V.visiting_place LIKE '%$keyword%' OR  V.history LIKE '%$keyword%'  OR  C.city LIKE '%$keyword%' )";
                }
                $sWhere     = implode(' AND ', $where);
                if($sWhere) {
                    $sWhere = 'WHERE '.$sWhere;
                } 
                if(($cityid > 0) || ($placeid > 0) || ($keyword != '')) {
                    $query = "SELECT * FROM visiting_places AS V JOIN tourist_city AS C ON C.city_id = V.city_id $sWhere ";
                    $result = mysqli_query($this->conn, $query);
                    return $result;
                }
            }
        }
        ?>

这是索引页有些错误

注意:未定义变量:C:\xampp\htdocs\search\index.php47
行 “class="form-control">中的关键字”

        index.php 
        <?php
         include 'db.php';
         $model = new Db();


         $turistCity = $model->getTouristCity();
         $visitingPlace = $model->getVisitingPlaces();
          $searchdata = [];
           if (isset($_POST['search'])) {
            $searchdata = $model->getVisitinPlaceData($_POST['city'], 
          $_POST['place'], $_POST['keyword']);
           }
        ?>
        <!DOCTYPE html>
        <html>
            <head>
                <title></title>
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
            </head>
            <body>
                <div style="width: 50%; margin: 0 auto;">
                <div class="hidden-sm bg-info" style="padding: 10px;"> 
                <form action="" method="post" > 
                    <div class="col-sm-3"> 
                        <select name="city" class="form-control">
                        <option value="0">Select City</option>
                        <?php foreach($turistCity as $city) {
                            $checked = ($_POST['city'] == $city[city_id])? 'selected' : '';
                            echo '<option value="'.$city[city_id].'" '.$checked.'>'.$city[city].'</option>';
                        }
                        ?>
                        </select>
                    </div>
                    <div class="col-sm-3"> 
                        <select name="place" class="form-control">
                            <option value="0">Select Visiting Place</option>
                            <?php foreach($visitingPlace as $place) { 
                                $checked1 = ($_POST['place'] == $place[vid])? 'selected' : '';
                                echo '<option value="'.$place[vid].'"  '.$checked1.'>'.$place[visiting_place].'</option>';
                            }
                            ?>
                        </select>
                    </div>
                    <div class="col-sm-3">
                        <input type="text" name="keyword" placeholder="Keword" value="<?php echo $_POST['keyword']; ?>"  class="form-control" /> 
                    </div>
                    <button name="search" class="btn btn-primary">Search</button>
                </form>
                </div>
                <div class="hidden-md bg-warning" style="padding: 10px;">
                    <table cellpadding="10" cellspacing="10" class="table table-striped">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>City</th>
                            <th>Place</th>
                            <th>History</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $i = 1;
                        if(count($searchdata) > 0 ){
                        foreach($searchdata as $places) {
                            echo '<tr>';
                                echo '<th>'.$i.'</th>';
                                echo '<td>'.$places[city].'</td>';
                                echo '<td>'.$places[visiting_place].'</td>';
                                echo '<td>'.$places[history].'</td>';
                            echo '</tr>';
                            $i++;
                        }
                        }
                        else {
                            echo '<td colspan="4">No Search Result Found.</td>';
                        }
                        ?>
                    </table>
                </div>
                </div>
            </body>
        </html>

标签: php

解决方案


像这样添加条件:

如果未提交表单, As$_POST最初将不可用。

$searchdata = [];
if (isset($_POST['city'])) {
 $searchdata = $model->getVisitinPlaceData($_POST['city'], $_POST['place'], $_POST['keyword']);
}

如何检查是否设置了值并修复错误:

<?
$keyword = '';
if (isset($_POST['keyword'])) {
 $keyword = $_POST['keyword'];
}
?>
<input type="text" name="keyword" placeholder="Keword" value="<?php echo $keyword;?>" class="form-control">

推荐阅读