there was a problem with the withdrawal of goods from the categories (in the drop-down menu), the categories are displayed, but I don’t know how to properly make the sql query substitute an id when selecting the desired category and displaying the corresponding products (trying to do it using the MVC pattern).
Request for CategoryModel.php categories
class CategoryModel extends Model { public static function Get_Category (){ $conn=DB::connToDB(); $stmt=$conn->prepare("SELECT id, name FROM categories"); $result = array(); $stmt ->execute(); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $result[$row['id']]=$row['name']; } return $result; } } Request for products ProductModel.php
class ProductModel extends Model { public function products($idcat) { $stmt = $this->db->prepare("SELECT id, name,price,description FROM products WHERE category_id = ?"); $stmt->execute([$idcat]); return $stmt->fetchAll(PDO::FETCH_ASSOC); } } ProductController.php
require_once (MODEL_PATH ."/CategoryModel.php"); class ProductController extends Controller { private $pageTpl = "/Views/templates/product.tpl.php"; public function __construct() { $this->model=new ProductModel(); $this->view= new View(); } public function index(){ $this->pageData['title']="Украшения"; $category=CategoryModel::Get_Category(); $this->pageData['category'] = $category; $idcat=$category['id']; $this->pageData['products']=$this->model->products($idcat); $this->view->render($this->pageTpl,$this->pageData); } } And for every piece of processing categories:
<div class="dropdown-menu " aria-labelledby="navbarDropdownMenuLink"> <?php foreach ($pageData['category'] as $key =>$value): ?> <a class="dropdown-item" href = "/product?<?= $key?>" > <?=$pageData['category'][$key]?></a > <?php endforeach; ?> </div >
indexmethod do you get the value of the category that is passed? - Yaroslav Molchan