每当我运行此代码时,我都会得到未定义变量$ d的错误,所以我注意到我必须将变量从控制器传递给视图。我是MVC的新手,所以如果有人帮我解决这个问题,我会感激不尽。
这是我到目前为止的实施: **
**
class ControllerModuleGetstoreproducts extends Controller{
public function index() {
$object = new ModelGetstoreproducts();
$d = $object->fetch();
require_once "getstoreproducts.php";
}
function returndata(){
$this->db->select("count(*)");
$this->db->from("oc_product");
$query = $this->db->get();
return $query->result();
}
}
<?php
class ModelGetstoreproducts{
function fetch(){
$sql = 'SELECT count(*) FROM oc_product';
$req = @mysql_query($sql);
return @mysql_fetch_object($req);
//return $req;
}
}
?>
<p>The total is <?php var_dump($d) ?></p>
答案 0 :(得分:1)
尝试对您的文件进行此更新:
<强>控制器:强>
class ControllerModuleGetstoreproducts extends Controller{
public $data;
public function index() {
$object = new ModelGetstoreproducts();
$this->data['products'] = $object->fetch();
require_once "getstoreproducts.php";
}
function returndata(){
$this->db->select("count(*)");
$this->db->from("oc_product");
$query = $this->db->get();
return $query->result();
}
}
查看:强>
<p>The total is <?php var_dump($this->data['products']) ?></p>
答案 1 :(得分:0)
将您的ID从View传递给Controller并将其导入Model.Apply此规则您不会收到任何错误。