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