Welcome Guest, Not a member yet? Register   Sign In
Need help with data arrays
#1

Hi,

Got two tables, Staff and Events. For a view, I need to get all the Staff records and, for each record, get a SUM of all the related (by Staffid) amount fields in Events.

What I'd like is to have all the data from each record in Staff and the SUM total in the same array ...I just can't figure out a way to do that.

Here's what I've got so far ... get all the Staff data and do a foreach loop to call a method to get the SUM total... but how to marry the two together???

Controller:
PHP Code:
public function list_all_staff()
 {
 
$this->data['data'] = $this->M_staff->get_all();
 foreach (
$this->data['data'] as $item)
 {
 
$sum_amount=$this->M_events->count_events($item['staffid']);
 }
 
$this->data['title'] = 'Work with Staff';
 
$this->data['main'] = 'v_staff_list';
 
$this->load->view('v_template'$this->data'refresh'); 
 } 


Model method to get sum:
PHP Code:
function count_events($par_id)
 { 
 
$this->db->select('SUM(events.amount) as total');
 
$this->db->where('staffid'$par_id); 
 
$this->db->where('date >='$this->session->userdata('start'));
 
$this->db->where('date <='$this->session->userdata('end')); 
 
$this->db->where('typeid'1);
 
$this->db->group_by('typeid'); 
 
$query $this->db->get('events');
 
$ret $query->row();
 } 
Reply


Messages In This Thread
Need help with data arrays - by blackbulldog - 03-14-2015, 08:28 AM
RE: Need help with data arrays - by RobertSF - 03-14-2015, 04:01 PM
RE: Need help with data arrays - by blackbulldog - 03-15-2015, 05:04 AM
RE: Need help with data arrays - by RobertSF - 03-15-2015, 05:42 AM
RE: Need help with data arrays - by blackbulldog - 03-16-2015, 04:46 AM
RE: Need help with data arrays - by RobertSF - 03-16-2015, 12:34 PM
RE: Need help with data arrays - by blackbulldog - 03-16-2015, 01:27 PM
RE: Need help with data arrays - by RobertSF - 03-16-2015, 05:36 PM
RE: Need help with data arrays - by RobertSF - 03-16-2015, 03:24 PM
RE: Need help with data arrays - by blackbulldog - 03-17-2015, 01:57 AM
RE: Need help with data arrays - by RobertSF - 03-17-2015, 03:21 AM



Theme © iAndrew 2016 - Forum software by © MyBB