3

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>
7
  • 2
    Did you do any debugging? What did var_dump($searchdata); show? Commented Apr 10, 2019 at 8:37
  • try to check with if(!empty($searchdata)) { Commented Apr 10, 2019 at 8:39
  • 3
    what type data does $model->getVisitinPlaceData($city, $keyword); return? Commented Apr 10, 2019 at 8:44
  • 1
    If foreach works, the class implements Iterable but not Countable. Commented Apr 10, 2019 at 8:49
  • 1
    You are returning a mysqli_result out of your method, and that implements the Traversable interface only. Simply call fetch_all and return the result of that instead, so that you have a normal array at the point where you want to count the elements. php.net/manual/en/mysqli-result.fetch-all.php Commented Apr 10, 2019 at 9:33

3 Answers 3

12

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

Sign up to request clarification or add additional context in comments.

2 Comments

than geting errorr .Fatal error: Uncaught Error: Call to undefined function is_countable() in D:\xammp\htdocs\search\index.php:64 Stack trace: #0 {main} thrown in D:\xammp\htdocs\search\index.php on line 64
@AmitabhKr Apologies, is_countable() is a 7.3 thing, I misread the question. You should be able to use is_iterable() to the same effect, I have updated the answer.
0

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!

2 Comments

This is better served as a comment, not an answer.
Can you explain that further? The given code does not contain $elements
0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.