使用自定义PHP MVC

时间:2015-05-15 11:15:27

标签: php model-view-controller

我有一个表1 产品和表2 供应商

表结构是这样的:

产品

CREATE TABLE IF NOT EXISTS `products` (
`id_product` int(11) NOT NULL AUTO_INCREMENT,   
`id_supplier` int(11) NOT NULL,   
`name_product` varchar(20) NOT NULL,     
PRIMARY KEY (`id_product`) )
ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `suppliers` (
`id_supplier` int(11) NOT NULL AUTO_INCREMENT,   
`name_supplier` varchar(11) NOT NULL,    
PRIMARY KEY (`id_supplier`) ) 
ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

型号:( suppliers_model.php

class Suppliers_Model extends Model{

    public function fetchSuppliers(){
        stmt = $this->db->prepare("SELECT * FROM suppliers");
        $stmt->setFetchMode(PDO::FETCH_OBJ);
        $stmt->execute();
        return $stmt->fetchAll();       
    }
}

控制器( suppliers.php

class Suppliers extends Controller{
    function __construct(){
        parent::__construct();
    }

    public function index(){
        $this->view->Suppliers= $this->model->fetchSuppliers();
    }
}

查看:

<table>
    <tr>
        <th>ID</th>
        <th>Name of supplier</th>
        <th>List of products</th>
    </tr>
    <?php foreach($this->Suppliers AS $key=>$value):?>
    <tr>
        <td><?php echo $value->id_supplier; ?></td>
        <td><?php echo $value->name_supplier; ?></td>
        <td>
                <?php foreach():?>
                    **HERE I Wante To display a liste of products**
                    1. Product A
                    2. Product B
                    etc....
                <?php endforeach;?>
        </td>
    </tr>
    <?php endforeach;?>

如何创建另一个模型和控制器来获取产品列表并传递值

  

$值 - &GT; id_supplier

作为显示每个供应商的产品清单的条件?

我已尝试过此代码,但它无法正常工作

    public function listProducts($id_supplier){
        stmt = $this->db->prepare("SELECT * FROM products WHERE id_supplier=$id_supplier");
        $stmt->setFetchMode(PDO::FETCH_OBJ);
        $stmt->execute();
        return $stmt->fetchAll();

    }

并将此代码添加到控制器

$this->view->listProducts= $this->model->listProducts('id_supplier');

并尝试使用foreach循环遍历产品表以检索产品列表但它不起作用

  

注意:我正在使用自定义PHP MVC

1 个答案:

答案 0 :(得分:2)

由于您已经设置了模型方法来获取产品,因此只需在控制器中使用它即可。

不要尝试在视图中进行调用,在控制器内部进行调用。

控制器:

public function index()
{

    $suppliers = $this->model->fetchSuppliers();
    foreach($suppliers as &$s) {
        $s->products = $this->model->listProducts($s->id_supplier);
    }

    $this->view->Suppliers = $suppliers; 
}

然后在你看来,继续你所拥有的:

<table>
    <tr>
        <th>ID</th>
        <th>Name of supplier</th>
        <th>List of products</th>
    </tr>
    <?php foreach($this->Suppliers AS $key=>$value):?>
    <tr>
        <td><?php echo $value->id_supplier; ?></td>
        <td><?php echo $value->name_supplier; ?></td>
        <td>
            <?php foreach($value->products as $k => $p): ?>
               <p><?php echo ($k + 1) '. ' . $p->product_name; ?></p>
            <?php endforeach;?>
        </td>
    </tr>
    <?php endforeach;?>
</table>