3

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>
5
  • use foreach loop to get your data
    – Danish Ali
    Commented Mar 29, 2018 at 6:46
  • I suppose result_array() gives you result as array.
    – u_mulder
    Commented Mar 29, 2018 at 6:47
  • this is why i hate query builders.... Commented Mar 29, 2018 at 6:50
  • I added my view, I want the SUM to be outside of the loop/table. thanks
    – Syns
    Commented Mar 29, 2018 at 6:52
  • use (object) $result... check my answer for details.
    – nitrex
    Commented Mar 29, 2018 at 6:53

4 Answers 4

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)

0
1

Use foreach loop to get your data

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

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

0
1

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']; ?>
2
  • I added more details, I need to extract the array but I want the total to be separated. is that possible? thanks
    – Syns
    Commented Mar 29, 2018 at 6:54
  • NO if you use SUM then only you can get single row. Answer edited, check now @Syns Commented Mar 29, 2018 at 7:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.