PHP: Get data from array

2019-05-12 06:17发布

问题:

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(t1.total_time) as total

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

<?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.

<?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>

回答1:

Based on Codeignitor Generating Query Results Manual Guid

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:

So use foreach()

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

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



回答2:

Use foreach loop to get your data

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


回答3:

Since you retrieve an array as result with $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



回答4:

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(); to $result = $query->result();

As you getting single row there are more efficient way to avoid indexing like $user[0]['total']. Just use row_array() instead 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']; ?>