Question

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 ?>
Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top