Reputation: 275
New to OpenCart and am having issues. I'm Trying to get the sum of a column from a table and display it on a page. I've tried a few iterations of the following but always get an undefined variable error. I've been stuck on this for a while and am not sure what Im doing wrong.
my model:
public function getSum() {
$amount_total = $this->db->query("SELECT FROM oct_donate SUM(amount) as amount_sum");
$sums = $amount_total->row["amount_sum"];
return $sums;
}
my controller:
public function sum() {
$data['total_sum']=$this->load->model('revenue/order')->getSum();
}
view: <?php echo $total_sum; ?>
Upvotes: 0
Views: 163
Reputation: 2044
The problem is in your controller code. You have to load the model first and then call its method. Update the code.
public function sum() {
$this->load->model('revenue/order')
$data['total_sum']=$this->model_revenue_order->getSum();
}
Also your query is not correct. I think you missed to select columns. It should be
$this->db->query("SELECT SUM(amount) as amount_sum FROM oct_donate");
Upvotes: 1