OpenCart中的致命错误:调用未定义的函数错误

时间:2014-08-28 15:59:19

标签: php mysql opencart

我不想问一个已经回答的问题,但是我已经做了大量的研究而且我被困住了。任何帮助,将不胜感激。

我正在尝试查询并将数据库结果添加到OpenCart的addproduct.tpl

MODEL 文件中,我有:

public function units() { //function that gets database info and returns it

$unit_variables = $this->db->query("SELECT unit FROM  ". DB_PREFIX ."
weight_class_description");

if (!$unit_variables) {

die(mysql_error());

}

else {

foreach ($unit_variables->rows as $temp_var) {

print_r($temp_var['unit'], true);

}   
}   
}

CONTROLLER 文件中,我有:

$this->load->model('catalog/product'); //where my function is located
$this->model_catalog_product->units();
$weight_variables = units(); 

if (isset($this->request->post['weight_variables'])){

$this->data['weight_variables'] = $this->request->post['weight_variables'];

}

视图中,我有:

<?php echo $weight_variables ?>

我收到以下错误:

Call to undefined function units() in /path/to/controller/file on line etc.

注意:当我print_r($temp_var);而不是返回print_R($temp_var, true)并删除控制器文件中的这些代码行$weight_variables = units(); if (isset($this->request->post['weight_variables'])){ $this->data['weight_variables'] = $this->request->post['weight_variables'] }时,我的模型文件将在addproduct.tpl上显示查询结果

1 个答案:

答案 0 :(得分:2)

units()是您对象的一种方法,但您将其称为独立的常规函数​​:

$weight_variables = units();
                    ^^^^^^^

不应该是:

$weight_variables = $this->model_catalog_product->units();

代替?请注意,按照书面形式,您的方法实际上不会返回任何内容,因此$weight_variables只会被分配null ..