Bonjour,
Je fais un exercice sur le MVC multiple sur une version simplifiée d'un CRM je dois utiliser l'architecture MVC et la programmation objet. J'aimerais savoir comment je peux ajouter, modifier et supprimer une ligne dans l'une des tables
( tables : category, prospects, clients). Merci d'avance pour votre aide
Je vous montre mon code est plus précisément la table category :
Partie Controller category :
Partie View category :Code PHP:
code
<?php
include 'view/categoryView.php';
include 'model/categoryModel.php';
class CategoryController {
private $view;
private $model;
public function __construct(){
$this->view = new categoryView();
$this->model = new categoryModel();
}
public function listAction(){
$list = $this->model->getList();
$this->view->displayList($list);
}
public function addAction(){
$this->view->displayAdd();
}
public function updateAction($// Je ne sais pas quoi mettre ici){
$this->view->displayUpdate($// Je ne sais pas quoi mettre ici);
}
public function deleteAction($// Je ne sais pas quoi mettre ici){
$this->view->displayDelete($// Je ne sais pas quoi mettre ici);
}
}
?>
/code
Partie Model category :Code PHP:
code
<?php
class CategoryView {
private $page;
public function __construct(){
$this->page = $this->searchHTML('header');
$this->page .= $this->searchHTML('nav');
}
public function displayList($list){
$this->page .= "<h3>Vous êtes sur la page category</h3>";
$tableau = '<div class="container">'
. '<table class="table table-striped table-bordered" cellspacing="0">'
. '<thead>'
. '<th>Nom</th><th>Description</th><th>Modifier</th><th>Supprimer</th>'
. '</thead><tbody>';
foreach ($list as $ligne){
$tableau .= "<tr><td>$ligne[1]</td>"
."<td>$ligne[2]</td>"
."<td><a href='index.php?controller=category&action=update&parm0=$ligne[0]&parm1=$ligne[1]&parm2=$ligne[2]'><svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='currentColor' class='bi bi-pencil' viewBox='0 0 16 16'>
<path fill-rule='evenodd' d='M12.146.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1 0 .708l-10 10a.5.5 0 0 1-.168.11l-5 2a.5.5 0 0 1-.65-.65l2-5a.5.5 0 0 1 .11-.168l10-10zM11.207 2.5L13.5 4.793 14.793 3.5 12.5 1.207 11.207 2.5zm1.586 3L10.5 3.207 4 9.707V10h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.293l6.5-6.5zm-9.761 5.175l-.106.106-1.528 3.821 3.821-1.528.106-.106A.5.5 0 0 1 5 12.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.468-.325z'/>
</svg></a></td>"
."<td><a href='index.php?controller=category&action=delete&parm0=$ligne[0]&parm1=$ligne[1]&parm2=$ligne[2]'><svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='currentColor' class='bi bi-trash' viewBox='0 0 16 16'>
<path d='M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z'/>
<path fill-rule='evenodd' d='M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4L4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z'/>
</svg></a></td></tr>";
}
$tableau .= "</tbody></table><a class='btn btn-primary' href='index.php?controller=category&action=add' role='button'>Ajouter</a></div>";
$this->page .= $tableau;
$this->display();
}
public function displayAdd(){
$this->page .= "<h3>Vous êtes sur la page d'ajout category</h3>";
$paramatres = array(
"readonly"=>"",
"parm0"=>"",
"parm1"=>"",
"parm2"=>"",
"lib_action"=>"Ajouter");
$this->displayForm($paramatres);
}
public function displayUpdate($// Je ne sais pas quoi mettre ici){
$this->page .= "<h3>Vous êtes sur la page de modification category</h3>";
$paramatres = array(
"readonly"=>"",
"parm0"=>$['parm0'],
"parm1"=>$['parm1'],
"parm2"=>$['parm2'],
"lib_action"=>"Modifier");
$this->displayForm($paramatres);
}
public function displayDelete($// Je ne sais pas quoi mettre ici){
$this->page .= "<h3>Vous êtes sur la page de suppression category</h3>";
$paramatres = array(
"readonly"=>"readonly",
"parm0"=>$['parm0'],
"parm1"=>$['parm1'],
"parm2"=>$['parm2'],
"lib_action"=>"Supprimer");
$this->displayForm($paramatres);
}
private function displayForm($parametres){
$this->page .= "<h3>Formulaire</h3>";
$this->page .= $this->searchHTML('categoryForm');
$this->page = str_replace("{readonly}", $parametres["readonly"], $this->page);
$this->page = str_replace("{parm0}", $parametres["parm0"], $this->page);
$this->page = str_replace("{parm1}", $parametres["parm1"], $this->page);
$this->page = str_replace("{parm2}", $parametres["parm2"], $this->page);
$this->page = str_replace("{lib_action}", $parametres["lib_action"], $this->page);
$this->display();
}
public function display(){
$this->page .= $this->searchHTML('footer');
echo $this->page;
}
private function searchHTML($filename){
$content = file_get_contents('view/html/'.$filename.'.html');
return $content;
}
}
?>
/code
Code PHP:
code
<?php
class CategoryModel {
const SERVER = "sqlpr*************************";
const USER = "c*******";
const PASSWORD = "t********";
const BASE ="c*********";
private $connexion;
private $requete;
public function __construct(){
try
{
$this->connexion = new PDO("mysql:host=" . self::SERVER . ";dbname=" . self::BASE, self::USER, self::PASSWORD);
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
}
public function getList(){
$requete = "SELECT * FROM category ";
$resultat = $this->connexion->query($requete);
$list = array();
if($resultat){
$list = $resultat->fetchAll(PDO::FETCH_NUM);
}
return $list;
}
public function add($// Je ne sais pas quoi mettre ici){
$sql = 'INSERT INTO category VALUES ("", :parm1, :parm2)';
$this->requete = $this->connexion->prepare($sql);
$this->requete->bindParam(':parm1', $['parm1']);
$this->requete->bindParam(':parm2', $['parm2']);
$this->executeTryCatch();
}
public function update($// Je ne sais pas quoi mettre ici)){
$this->requete = 'UPDATE category SET nom=:parm1, description=:parm2 WHERE id=:parm0';
$this->requete = $this->connexion->prepare($this->requete);
$this->requete->bindParam(':parm0', $['parm0']);
$this->requete->bindParam(':parm1', $['parm1']);
$this->requete->bindParam(':parm2', $['parm2']);
$this->executeTryCatch();
}
public function delete($// Je ne sais pas quoi mettre ici)){
$this->requete = 'DELETE FROM category WHERE id=:parm0';
$this->requete = $this->connexion->prepare($this->requete);
$this->requete->bindParam(':parm0', $['parm0']);
$this->executeTryCatch();
}
private function executeTryCatch(){
try
{
$this->requete->execute();
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
$this->requete->closeCursor();
}
}
?>
/code
-----