phparrayscodeigniteractiverecordresultset

How to access row values of an array populated by CodeIgniter's result_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(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>

Solution

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