使用 Custom PHP MVC 在另一个 foreach 循环中进行 foreach 循环
foreach loop inside another foreach loop using Custom PHP MVC
我有 table 1 产品 和 Table 2 供应商
table的结构是这样的:
产品
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;?>
如何创建另一个模型和控制器来获取产品列表并传递值
$value->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 循环遍历产品 table 以检索产品列表,但它不起作用
NB: Im working with a custom PHP MVC
由于您已经设置了模型方法来获取产品,因此只需在您的控制器中使用它即可。
不要尝试在视图中调用,在控制器中调用。
控制器:
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>
我有 table 1 产品 和 Table 2 供应商
table的结构是这样的:
产品
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;?>
如何创建另一个模型和控制器来获取产品列表并传递值
$value->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 循环遍历产品 table 以检索产品列表,但它不起作用
NB: Im working with a custom PHP MVC
由于您已经设置了模型方法来获取产品,因此只需在您的控制器中使用它即可。
不要尝试在视图中调用,在控制器中调用。
控制器:
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>