为什么我总是得到总数。当我输入“”时,行而不是值0 - 空?
控制器
$totalSchools = $this->Classroom->find('count', array('conditions' => array('Classroom.name LIKE' => '%'. $searchQuery .'%')));
$this->set('totalSchools', $totalSchools);
查看
<?php echo $totalSchools ?>
答案 0 :(得分:1)
因为空字符串的计算结果为'%%'=&gt;这匹配一切。您必须使用if / else。
检查此特殊情况答案 1 :(得分:1)
如果$searchQuery
为空,则应首先过滤,如下所示:
if($searchQuery != ""){
$totalSchools = $this->Classroom->find('count', array('conditions' => array('Classroom.name LIKE' => '%'. $searchQuery .'%')));
$this->set('totalSchools', $totalSchools);
}
else
$this->set('totalSchools', 0);