Вопрос

Why do I keep getting the total no. of rows instead of the value 0 when i've entered "" - empty ?

Controller

$totalSchools = $this->Classroom->find('count', array('conditions' => array('Classroom.name LIKE' => '%'. $searchQuery .'%')));
$this->set('totalSchools', $totalSchools);

View

<?php echo $totalSchools ?>
Это было полезно?

Решение

You should filter first your $searchQuery if it is empty like here:

if($searchQuery != ""){

      $totalSchools = $this->Classroom->find('count', array('conditions' => array('Classroom.name LIKE' => '%'. $searchQuery .'%')));
       $this->set('totalSchools', $totalSchools);

}
    else
        $this->set('totalSchools', 0);

Другие советы

Because an empty string evaluates to '%%' => this matches everything. You have to check for this special case with an if/else.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top