PHP-将变量从控制器传递到视图 [英] Php - passing a variable from controller to view

查看:101
本文介绍了PHP-将变量从控制器传递到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我运行此代码时,我都会得到未定义变量$ d的错误,因此我注意到我必须将变量从控制器传递给视图.我是MVC的新手,因此,如果有人在这个问题上为我提供帮助,我将不胜感激.

whenever I run this code i get the error of undefined variable $d, so i noticed i have to pass the variable from the controller to the view. I am new to MVC, hence why i would be grateful if someone helps me with this issue.

到目前为止,这是我的实现: **

This is my implementation so far: **

**

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>

推荐答案

尝试对文件进行此更新:

Try this update to your files:

控制器:

    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>

这篇关于PHP-将变量从控制器传递到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆