Domanda

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 ?>
È stato utile?

Soluzione

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

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top