-1

i want to search select city best visiting place after keyword than search click below the result. all category releted like city->visiting place->Keyword. i have some error please share this idea.

I have two table 'tourist_city' and '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
        ==============================================================================

database is working fine below code here...

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

This is index page some error

Notice: Undefined variable: keyword in C:\xampp\htdocs\search\index.php on line 47
" 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>
Amitabh
  • 21
  • 6
  • 1
    Add condition if (isset($_POST['YOUR-SUBMIT-BUTTON']`, `$_POST` is not initialised by default. – Pupil Apr 04 '19 at 13:02
  • than error show Notice: Undefined variable: searchdata in C:\xampp\htdocs\search\index.php on line 61 – Amitabh Apr 04 '19 at 13:06

1 Answers1

0

Add condition like this:

As $_POST will not be available initially if form is not submitted.

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

How to check if value is set and fix the error:

<?
$keyword = '';
if (isset($_POST['keyword'])) {
 $keyword = $_POST['keyword'];
}
?>
<input type="text" name="keyword" placeholder="Keword" value="<?php echo $keyword;?>" class="form-control">
Pupil
  • 23,834
  • 6
  • 44
  • 66