我使用Codigniter。
我使用以下行返回数据库中的结果:
return $query->result_array();
在我的控制器中,我编写以下代码来获得这些结果:
$data["results"] = $this->model_abc->get_them();
我想知道我应该在控制器中写哪一行来获得结果数。
你能帮帮我吗。
答案 0 :(得分:1)
您可以从此
获取行号$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_rows();
修改
如果要计算$ data [“results”],可以使用php count()
函数
答案 1 :(得分:1)
在你的控制器中试试这个:
//得到结果 $ data [“results”] = $ this-> model_abc-> get_them();
//得到结果的数量 $ count = count($ data [“results”]);
答案 2 :(得分:1)
你可以用一般的方式做到这一点
型号:
// general purpose counting rows
function get_them($table ,$where=NULL)
{
if(!empty($where))
{
$this->db->where($where);
}
$query=$this->db->get($table);
if($query)
{
return $query->num_rows();
}
else
{
return array();
}
}
// general purpose fetching rows
function get_records($table ,$where=NULL)
{
if(!empty($where))
{
$this->db->where($where);
}
$query=$this->db->get($table);
if($query)
{
return $query->result();
}
else
{
return array();
}
}
控制器:
$data['counter']=$this->model_abc->get_them('tablename');
$data['results']=$this->model_abc->get_records('tablename');
$this->load->view('view',$data);
查看:
if(isset($counter))
{
echo $counter;
}
if(isset($result))
{
foreach($result as $row)
{
//YOUR RECORDS
}
}
我希望这对你有用,我希望它能清除你的概念