CodeIgniter Forums
Is this an efficeient way ti use models? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Is this an efficeient way ti use models? (/showthread.php?tid=14502)



Is this an efficeient way ti use models? - El Forum - 01-05-2009

[eluser]Solarpitch[/eluser]
I'm just wondering if this is best practice or if there is a better way of achieving this. I have a report model that calculates some reports from a sales table. For example, it will calculate the total amount of cash, mastercard, visa, lazer sales etc.

This is what I have...

Code:
function cash_sales()
{  
$this->client->select_sum('price', 'cash_total');
$query = $this->client->get_where('sales', array('type' => 'cash');
$row = $query->row();
return round($row->cash_total, 2);
}

function visa_sales()
{  
$this->client->select_sum('visa', 'visa_total');
$query = $this->client->get_where('sales', array('type' => 'cash');
$row = $query->row();
return round($row->visa_total, 2);
}

function mcard_sales()
{  
$this->client->select_sum('mcard', 'mcard_total');
$query = $this->client->get_where('sales', array('type' => 'cash');
$row = $query->row();
return round($row->mcard_total, 2);
}

.... and so on for the rest

Is there a better way to achieve this or is it pretty much the same. I was worried about page loading due to all the calculations. I would have around 25 functions like this in the report model to be displayed on one page.