Reputation: 49
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>
Upvotes: 3
Views: 37942
Reputation: 506
Sometimes you cannot wrap all the clause where you use count, or you are using several counts, you only need to create empty arrays of the uncountable, just add before your actual code.
if(!is_countable($your_array))$your_array = Array();
Upvotes: 0
Reputation: 11
PHP 7.3 gave error with: // If the requested page doesn't exist. if ( $elements['page'] > count( $elements['pages'] ) ) {
Changed it to: if(is_countable($elements['page'])) {
That fixed it!
Upvotes: 0
Reputation: 3045
Simply wrap it in a conditional that checks if it is countable, so..
// PHP >= 7.3
if(is_countable($searchdata)) {
// Do something
}
// PHP >= 7.1
if(is_iterable($searchdata)) {
// Do something
}
Note: You should never, ever assume anything is true unless you explicitly define it as such. count($something)
implies an expectation that it is indeed an array or something you can count. If this is not always the case, and so you much check it before proceeding.
EDIT: Use is_iterable()
for PHP >= 7.1 or is_countable()
for >= 7.3 in this scenario. Snippet above updated
Upvotes: 12