我在我的 Controller 上有这个查询:

$this->db->select('t1.act_id, t2.login, t1.cust_name, t1.act_type, t1.act_detail, t1.date_added, t1.date_modified, t1.act_notes, '
             . 't3.category, t1.total_time, sum(t1.total_time) as total')
            ->from('activity as t1')
            ->join('user as t2', 't1.user_id = t2.user_id', 'LEFT')
            ->join('customer as t3', 't1.cust_name = t3.cust_name', 'LEFT')
            ->where('t2.login', $user)
            ->where('MONTH(t1.date_added)', $month)
            ->order_by('t1.date_added', "desc");

    $query = $this->db->get();
    $result = $query->result_array();

    $data = array('title' =>'Sales Report'." - ".$user,
        'user' => $result);
    $this->load->view('admin/report/vw_report_excel', $data);

我想发布这个 SUM 值



但我一直收到错误,说“试图获取非对象的属性”

这是我在我的 View 中调用它的方式 (vw_report_excel):
<?php echo ($user->total); ?>

调用或声明它的正确方法是什么?
问候

这是我的观点,我希望我的 SUM 从表/循环中排除。
<?php $i=1; foreach($user as $xuser) { ?>
                 <tr>
                      <td><?php echo $xuser['act_id']; ?></td>
                      <td><?php echo $xuser['login']; ?></td>
                      <td><?php echo $xuser['cust_name']; ?></td>
                      <td><?php echo $xuser['act_type']; ?></td>
                      <td><?php echo $xuser['act_detail']; ?></td>
                      <td><?php echo $xuser['date_added']; ?></td>
                      <td><?php echo $xuser['date_modified']; ?></td>
                      <td><?php echo $xuser['act_notes']; ?></td>
                      <td><?php echo $xuser['category']; ?></td>
                      <td><?php echo $xuser['total_time']; ?></td>
                 </tr>
                 <?php $i++; } ?>
<td><?php echo count($user); ?></td>
        <td><?php echo ($user->total); ?></td>

最佳答案

基于 Codeignitor Generating Query Results Manual Guid

result_array()



所以使用 foreach()

foreach($user as $usr){
 echo $usr['total'];
}

或使用:- <?php echo $user[0]['total']; ?>(基于您问题中的代码修改)

关于PHP:从数组中获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49549627/

10-14 13:03
查看更多