首页 > 解决方案 > 从函数中的查询中获取结果并计算 Codeigniter 中的 num_rows

问题描述

我在模型中的查询:

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。

请帮助解决这个问题..非常感谢

标签: javascriptphpmysqlajaxcodeigniter-3

解决方案


试试下面的代码

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

推荐阅读