预先搜索选择城市,访问地点,关键字而不是结果

时间:2019-04-04 12:59:21

标签: php

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

我有两个表“ 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;
                }
            }
        }
        ?>

这是索引页某些错误

通知:未定义的变量: 47
行中的 C:\ xampp \ htdocs \ search \ index.php 中的关键字 “ 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>

1 个答案:

答案 0 :(得分:0)

添加如下条件:

如果未提交表单,则$_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">