[PHP-NOTES] note 130589 added to mysqli-result.fetch-assoc

[email protected] ("[email protected]") Fri, 19 Dec 2025 07:32:49 +0000
Newsgroups php.notes
Message-ID <[email protected]>
here's an example of a controller that is fonctional :

<?php
namespace app\controller;
use app\controller\Home;
class Product
{
    private \app\model\repository\ProductRepositoryInterface $repository;
    public function __construct()
    {
        $this->repository = new \app\model\repository\DbProductRepository();
    }
    function add()
    {
        require "view" . DIRECTORY_SEPARATOR . "addProduct.php";
    }
    function addProduct()
    {
        if (isset($_POST["name"]) && isset($_POST["price"]) && isset($_POST["quantity"])) {
            $name = $_POST["name"];
            $price = $_POST["price"];
            $quantity = $_POST["quantity"];
            $id = random_int(1, 100000);
            while ($this->repository->findById($id) !== null) {
                $id = random_int(1, 100000);
            }
            $products = $this->repository->findAll();
            $nomsproduits = array_map(function ($prod) {
                return $prod['name'];
            }, $products);
            if (in_array($name, $nomsproduits, true)) {
                throw new \Exception("Le nom du produit existe déjà.");
            }
            $nproduit = new \app\model\entity\ProductEntity();
            $nproduit->setId($id);
            $nproduit->setName($name);
            $nproduit->setPrice((float)$price);
            $nproduit->setQuantity((int)$quantity);
            $this->repository->add($nproduit);
        }
        $this->display([$id]);
    }
   
    function update(array $params)
    {
        $produitcherche = null;
        if (isset($params[0])) {
            $produitcherche = $this->repository->findById(intval($params[0]));
        }
        if ($produitcherche === null) {
            throw new \Exception("Produit non trouvé pour la mise à jour.");
        }
        require "view" . DIRECTORY_SEPARATOR . "updateProduct.php";
    }
    function updateProduct()
    {
        if (isset($_POST["name"]) && isset($_POST["price"]) && isset($_POST["quantity"]) && isset($_POST["id"])) {
            $id = $_POST["id"];
            $name = $_POST["name"];
            $price = $_POST["price"];
            $quantity = $_POST["quantity"];
            $products = $this->repository->findAll();
            $nomsproduits = array_map(function ($prod) use ($id) {
                if ($id != $prod['id']) {
                    return $prod['name'];
                }
            }, $products);
            if (in_array($name, $nomsproduits, true)) {
                throw new \Exception("Le nom du produit existe déjà.");
            }
            $nproduit = new \app\model\entity\ProductEntity();
            $nproduit->setId($id);
            $nproduit->setName($name);
            $nproduit->setPrice((float)$price);
            $nproduit->setQuantity((int)$quantity);
            $this->repository->update($id, $nproduit);
        }
        $this->display([$id]);
    }
    function delete(array $params)
    {
        if (isset($params[0])) {
            $id = intval($params[0]);
            $this->repository->delete($id);
        }
        header("Location: /workspace/code_demo/04-MVC/app/Home");
    }
}
----
Server IP: 206.189.2.9
Probable Submitter: 2001:660:7220:385:193:52:103:33
----
Manual Page -- https://php.net/manual/en/mysqli-result.fetch-assoc.php
Edit        -- https://main.php.net/note/edit/130589
Del: integrated  -- https://main.php.net/note/delete/130589/integrated
Del: useless     -- https://main.php.net/note/delete/130589/useless
Del: bad code    -- https://main.php.net/note/delete/130589/bad+code
Del: spam        -- https://main.php.net/note/delete/130589/spam
Del: non-english -- https://main.php.net/note/delete/130589/non-english
Del: in docs     -- https://main.php.net/note/delete/130589/in+docs
Del: other reasons-- https://main.php.net/note/delete/130589
Reject      -- https://main.php.net/note/reject/130589
Search      -- https://main.php.net/manage/user-notes.php