I want to select the best find result. I'm getting an error: here I am shown HTML form action and DB connection in the file below here please check it. and error msg also mention this section. i am using php 7.2 than some getting problem
Warning: count(): Parameter must be an array or an object that implements
Countable in D:\xammp\htdocs\search\index.php on line 64
Warning: Use of undefined constant city_id - assumed 'city_id' (this will throw an Error in a future version of PHP) in D:\xammp\htdocs\search\index.php on line 37
help me...error msg.
PHP:
<?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>';
}
?>
I'm using PHP like:
<?php
$searchdata = [];
$keyword = '';
if (isset($_POST['search'])) {
$city = $_POST['city'];
$keyword = $_POST['keyword'];
$searchdata = $model->getVisitinPlaceData($city, $keyword);
}
?>
db connection and tabel data fetching
<?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, $keyword){
$sWhere = '';
$where = array();
if($cityid > 0) {
$where[] = 'V.city_id = '.$cityid.' AND V.is_enabled = "1"';
}
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) || ($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;
}
}
}
?>
html form action
<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">
<input type="text" name="keyword" placeholder="Keword" value="<?php echo $keyword;?>" class="form-control">
</div>
<button name="search" type="submit" class="btn btn-primary">Search</button>
</form>
var_dump($searchdata);show?$model->getVisitinPlaceData($city, $keyword);return?foreachworks, the class implementsIterablebut notCountable.