سؤال

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