model view controller - foreach loop inside another foreach loop using Custom PHP MVC -


i have table 1 products , table 2 suppliers

the tables structure way:

products

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 ; 

suppliers

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 ; 

model: (suppliers_model.php)

class suppliers_model extends model{      public function fetchsuppliers(){         stmt = $this->db->prepare("select * suppliers");         $stmt->setfetchmode(pdo::fetch_obj);         $stmt->execute();         return $stmt->fetchall();            } } 

controller (suppliers.php)

class suppliers extends controller{     function __construct(){         parent::__construct();     }      public function index(){         $this->view->suppliers= $this->model->fetchsuppliers();     } } 

view :

<table>     <tr>         <th>id</th>         <th>name of supplier</th>         <th>list of products</th>     </tr>     <?php foreach($this->suppliers $key=>$value):?>     <tr>         <td><?php echo $value->id_supplier; ?></td>         <td><?php echo $value->name_supplier; ?></td>         <td>                 <?php foreach():?>                     **here wante display liste of products**                     1. product                     2. product b                     etc....                 <?php endforeach;?>         </td>     </tr>     <?php endforeach;?> 

how created model , controller fetch list of products , pass value

$value->id_supplier

as condition display list of products of each supplier?

i've tried code doesn't work

    public function listproducts($id_supplier){         stmt = $this->db->prepare("select * products id_supplier=$id_supplier");         $stmt->setfetchmode(pdo::fetch_obj);         $stmt->execute();         return $stmt->fetchall();      } 

and added code controller

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

and tried use foreach loop loop through products table retrieve list of products doesn't work

nb: im working custom php mvc

since setup model method products, use in controller.

don't try make invocations inside views, inside controller.

controller:

public function index() {      $suppliers = $this->model->fetchsuppliers();     foreach($suppliers &$s) {         $s->products = $this->model->listproducts($s->id_supplier);     }      $this->view->suppliers = $suppliers;  } 

then in view, continue on have:

<table>     <tr>         <th>id</th>         <th>name of supplier</th>         <th>list of products</th>     </tr>     <?php foreach($this->suppliers $key=>$value):?>     <tr>         <td><?php echo $value->id_supplier; ?></td>         <td><?php echo $value->name_supplier; ?></td>         <td>             <?php foreach($value->products $k => $p): ?>                <p><?php echo ($k + 1) '. ' . $p->product_name; ?></p>             <?php endforeach;?>         </td>     </tr>     <?php endforeach;?> </table> 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -