PHP:从数组获取数据 [英] PHP: Get data from array

查看:87
本文介绍了PHP:从数组获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器上有以下查询:

I have this query on my 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值


总和(t1.total_time)总计

sum(t1.total_time) as total

但我不断收到错误消息,说获取非对象的属性

but I kept getting error, saying that "Trying to get property of non-object"

这是我在视图中调用它的方式(vw_report_excel):

here's how I called it on my view (vw_report_excel):

<?php echo ($user->total); ?>

如何正确调用或声明它?
问候

how is the correct way to call or declare it? Regards

这是我的观点,我希望将SUM从表/循环中排除。

here's my view, I want my SUM exclude from the table/loop.

<?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生成查询结果手册指南

result_array()

result_array()


此方法以纯数组或空$返回查询结果没有结果的b $ b数组。通常,您将在
foreach循环中使用它,如下所示:

This method returns the query result as a pure array, or an empty array when no result is produced. Typically you’ll use this in a foreach loop, like this:

因此请使用 foreach ()

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

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

Or use:-<?php echo $user[0]['total']; ?> (Based on your code modification in your question)

这篇关于PHP:从数组获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆