Welcome Guest, Not a member yet? Register   Sign In
Passing an Array from Model?
#1

[eluser]Solarpitch[/eluser]
Hey Guys,

I'm a little stuck with the following. I'm calculating a sum of 6 columns in a table, for a specific report. If the report has 2 POS terminals it will return something like...

Till 1: €2156.30
Till 2: €1881.20


The problem is its only printing the last value of the result which is Till 2: €1881.20. I must need to handle the array differently?

MODEL...

Code:
//see how many till sthe client has reports for on that date
function count_tills($date)
{  
$query = $this->client->query('SELECT till FROM golfpro_bar_control_zxread WHERE date = '.$date.' AND path = "real" GROUP BY `till`');
return $query->result();
}

//calculate the total taking for each till that we calculated
function total_till_takings($date, $till)
{  
$query = $this->client->query('SELECT SUM(bartotaltrue) AS bt, SUM(foodtotaltrue) AS ft, SUM(specialtotaltrue) AS st,
SUM(golftotaltrue) AS gt, SUM(barsalesaccounttotal) AS at, SUM(barsaleshotelroom) AS rt
FROM golfpro_bar_control_zxread WHERE date = '.$date.' and till = '.$till.' and path = "real"');
$row = $query->row();
      
$return = "Till ".$till.": ";
$return .= round($row->bt, 2) + round($row->ft, 2) + round($row->st, 2) + round($row->gt, 2) + round($row->at, 2) + round($row->rt, 2);
return $return;
;
}


CONTROLLER...

Code:
function dashboard(){
    
$this->load->helper('url');
$this->load->model('report_model');
        
$count_tills = $this->report_model->count_tills('20081209');
foreach($count_tills as $row)
{
$data['total_till_takings'] = $this->report_model->total_till_takings('20081209', $row->till);
}
                
        
$data['right_box'] = "right_box/dashboard_view";
$this->load->view('template/page_1', $data);
            
}

VIEW...

Code:
<?=$total_till_takings?> // WHICH WILL ONLY PRINT OUT "Till 2: €1881.20"
#2

[eluser]jalalski[/eluser]
Yup... that's correct.

the line:
Code:
$data['total_till_takings'] = $this->report_model->total_till_takings('20081209', $row->till);
is overriding the array with each iteration.
You possibly need this:
Code:
$data['total_till_takings'][] = $this->report_model->total_till_takings('20081209', $row->till);
to create an array of till takings.

And then in the view, iterate over it (not just print it out).
#3

[eluser]Solarpitch[/eluser]
Thanks for the reply

Ah I see what you mean. So create a multidimensional array (which are not my strong points) to hold the array being generated from the query. That makes sense.

Only, how could I iterate over it in the view. I presume I would need to declare a different array in the view to do this... then loop through it or something?
#4

[eluser]Michael Wales[/eluser]
Code:
<?php foreach ($total_till_takings as $till) {
  echo $till;
}




Theme © iAndrew 2016 - Forum software by © MyBB