[英]Getting result from a query in a function and count the num_rows in Codeigniter
我在模型中的查詢:
public function infoReprob(){
$tid = $this->input->get('tid');
$query = $this->db->select('histprob.tid,histprob.ketprob,histprob.updated_at,otslm.lokasi')->from('histprob')->join('otslm', 'otslm.tid = histprob.tid')->where('histprob.tid', $tid)->get();
return $query->result();
}
上面的查詢用於在Ajax的視圖中生成結果並完美地工作。 但是我也需要計算結果,並且我試圖在Controller中使用folowong:
function index(){
$data['reprobs'] = count($this->m->infoReprob()); // USING COUNT
$this->load->view('front/reprob_view', $data);
}
然后像這樣調用我的Javascript中的變量:
<?php echo $reprobs; ?>
但結果是:0,即使實際的num_rows也> 0。
請幫助解決此問題。非常感謝
嘗試以下代碼
function index(){
$data['result'] = $this->m->infoReprob(); // get RESULT
$data['count'] = $this->m->infoReprob(1); // get COUNT
$this->load->view('front/reprob_view', $data);
}
public function infoReprob($count=0){
$tid = $this->input->get('tid');
$query = $this->db->select('histprob.tid,histprob.ketprob,histprob.updated_at,otslm.lokasi')->from('histprob')->join('otslm', 'otslm.tid = histprob.tid')->where('histprob.tid', $tid)->get();
if ($count == 1)
return $query->num_rows();
else
return $query->result_array();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.