简体   繁体   English

PHP:从数组中获取数据

[英]PHP: Get data from array

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

I want to post this SUM value 我想发布这个SUM值

sum(t1.total_time) as total sum(t1.total_time)为总数

but I kept getting error, saying that "Trying to get property of non-object" 但我一直收到错误,说“试图获得非对象的财产”

here's how I called it on my view (vw_report_excel): 这是我在视图中调用它的方式(vw_report_excel):

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

how is the correct way to call or declare it? 如何调用或声明它的正确方法? Regards 问候

here's my view, I want my SUM exclude from the table/loop. 这是我的观点,我希望从表/循环中排除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>

Use foreach loop to get your data 使用foreach loop来获取数据

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

Try this as you returned array not object 在返回数组而不是对象时尝试此操作

<?php echo ($user[0]['total']); ?>

If you want exactly like $user[0]->total then change $result = $query->result_array(); 如果你想要完全像$user[0]->total那么改变$result = $query->result_array(); to $result = $query->result(); to $result = $query->result();

As you getting single row there are more efficient way to avoid indexing like $user[0]['total'] . 当您获得单行时,有更有效的方法来避免索引,如$user[0]['total'] Just use row_array() instead result_array() 只需使用row_array()而不是result_array()

$result = $query->row_array();

If you use that then no looping required and you can now use direct like 如果你使用它,那么不需要循环,你现在可以直接使用

<?php echo $user['login']; ?>

Based on Codeignitor Generating Query Results Manual Guid 基于Codeignitor生成查询结果手册Guid

result_array() result_array()

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循环中使用它,如下所示:

So use foreach() 所以使用foreach()

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

Or use:- <?php echo $user[0]['total']; ?> 或者使用: - <?php echo $user[0]['total']; ?> <?php echo $user[0]['total']; ?> (Based on your code modification in your question) <?php echo $user[0]['total']; ?> (根据你问题中的代码修改)

Since you retrieve an array as result with $result = $query->result_array(); 因为你使用$result = $query->result_array();检索一个数组$result = $query->result_array(); , you need to access it with numerical index and associative arrays. ,您需要使用数字索引和关联数组访问它。 Hence if you want to access the first record; 因此,如果您想访问第一条记录;

<?php echo $user[0]['total']; ?>

Or if you want to traverse through all the results 或者,如果您想遍历所有结果

<?php foreach( $user as $rec) echo $rec['total'];
//or
for($i=0,$count = count($user);$i<$count;$i++) echo $user[$i]['total']; ?>

PS the for loop is faster if you want to traverse 如果你想遍历, PS for循环更快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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