繁体   English   中英

从函数中的查询中获取结果并在Codeigniter中计算num_rows

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM