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

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?


//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;


function dashboard(){
$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);


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

Yup... that's correct.

the line:
$data['total_till_takings'] = $this->report_model->total_till_takings('20081209', $row->till);
is overriding the array with each iteration.
You possibly need this:
$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).

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?

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

Theme © iAndrew 2016 - Forum software by © MyBB