문제

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