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

标签 javascript php mysql ajax codeigniter-3

我在模型中的查询:

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 View 中生成结果并完美运行。但我还需要计算结果,并且我尝试在 Controller 中使用以下内容:

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

关于javascript - 从函数中的查询获取结果并计算 Codeigniter 中的 num_rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52379361/

相关文章:

php - 在 INSERT ON DUPLICATE KEY UPDATE 中,检测指定插入值时是否使用默认值

Mysql With Rollup 没有运行我预期的

javascript - 沿着矩形边界的进度条

JavaScript 正则表达式和子匹配

php - 使用 COUNT(*) 和 SUM() 时如何打印或回显行

php - 在私有(private)函数的数组上插入元素

mysql - 将相交从 Oracle 转换为 MySQL

mysql - 如何选择和直接在相关行之前求和

javascript - Jquery在数组中创建数组

javascript - 为什么 html2canvas 为 MS Edge 上的表格捕获比 Chrome 更小的字节数组 Canvas