site ecommerce php mvc & pdo الجزء الثالت
فهاد الجزء الثالت من site ecommerce ب php و pdo غادي نكملوا الدورة ديالنا ونزيدو ل model ول controller الخاصين بل produits غادي نشوفوا كيفاش نزيدو نعدلوا نمسحوا وكذلك نسترجعوا produit من قاعدة البيانات.
نظرة سريعة بالفيديو
1- إضافة ل Model Product
ف dossier models زيد fichier جديد سميه Product.php هاد الملف غادي يكون فيه الكود لي كيمكن من استرجاع ل produits كاملين من قاعدة البيانات.
أيضا كاين إسترجاع ل produits الخاصين بفئة معينة (catégorie) عندي أيضا إسترجاع produit بل id ديالو وإضافة تعديل وحذف produit فكل fonction عندها دور خاص بها.
هاد المعلومات فاش كنسترجعها كنرسلها ل controller لي غادي نزيدو من بعد.
الكود ديال الملف هو :
<?php
class Product{
static public function getAll(){
$stmt = DB::connect()->prepare('SELECT * FROM products');
$stmt->execute();
return $stmt->fetchAll();
$stmt->close();
$stmt =null;
}
static public function getProductByCat($data){
$id = $data['id'];
try{
$stmt = DB::connect()->prepare('SELECT * FROM products WHERE product_category_id = :id');
$stmt->execute(array(":id" => $id));
return $stmt->fetchAll();
$stmt->close();
$stmt =null;
}catch(PDOException $ex){
echo "erreur " .$ex->getMessage();
}
}
static public function getProductById($data){
$id = $data['id'];
try{
$stmt = DB::connect()->prepare('SELECT * FROM products WHERE product_id = :id');
$stmt->execute(array(":id" => $id));
$product = $stmt->fetch(PDO::FETCH_OBJ);
return $product;
$stmt->close();
$stmt =null;
}catch(PDOException $ex){
echo "erreur " .$ex->getMessage();
}
}
static public function addProduct($data){
$stmt = DB::connect()->prepare('INSERT INTO products (product_title
,product_description,product_quantity,product_image,
product_price,old_price,short_desc,product_category_id)
VALUES (:product_title,:product_description,:product_quantity,:product_image,
:product_price,:old_price,:short_desc,:product_category_id)');
$stmt->bindParam(':product_title',$data['product_title']);
$stmt->bindParam(':product_description',$data['product_description']);
$stmt->bindParam(':product_quantity',$data['product_quantity']);
$stmt->bindParam(':product_image',$data['product_image']);
$stmt->bindParam(':product_price',$data['product_price']);
$stmt->bindParam(':old_price',$data['old_price']);
$stmt->bindParam(':short_desc',$data['short_desc']);
$stmt->bindParam(':product_category_id',$data['product_category_id']);
if($stmt->execute()){
return 'ok';
}else{
return 'error';
}
$stmt->close();
$stmt = null;
}
static public function editProduct($data){
$stmt = DB::connect()->prepare('UPDATE products SET
product_title = :product_title,
product_description=:product_description,
product_quantity=:product_quantity,
product_image=:product_image,
product_price=:product_price,
old_price=:old_price,
short_desc=:short_desc,
product_category_id=:product_category_id
WHERE product_id=:product_id
');
$stmt->bindParam(':product_id',$data['product_id']);
$stmt->bindParam(':product_title',$data['product_title']);
$stmt->bindParam(':product_description',$data['product_description']);
$stmt->bindParam(':product_quantity',$data['product_quantity']);
$stmt->bindParam(':product_image',$data['product_image']);
$stmt->bindParam(':product_price',$data['product_price']);
$stmt->bindParam(':old_price',$data['old_price']);
$stmt->bindParam(':short_desc',$data['short_desc']);
$stmt->bindParam(':product_category_id',$data['product_category_id']);
if($stmt->execute()){
return 'ok';
}else{
return 'error';
}
$stmt->close();
$stmt = null;
}
static public function deleteProduct($data){
$id = $data['id'];
try{
$stmt = DB::connect()->prepare('DELETE FROM products WHERE product_id = :id');
$stmt->execute(array(":id" => $id));
$product = $stmt->fetch(PDO::FETCH_OBJ);
if($stmt->execute()){
return 'ok';
}else{
return 'error';
}
$stmt->close();
$stmt =null;
}catch(PDOException $ex){
echo "erreur " .$ex->getMessage();
}
}
}
2- إضافة ل Controller ProductsController
ف dossier controllers زيد fichier جديد سميه ProductsController.php هاد الملف غادي يكون فيه الكود لي كيمكن من تنفيذ ل fonctions لي زدنا فل model Product وغادي يكون هو صلة الوصل بين ل view و قاعدة البيانات.
فعندي تقريبا نفس ل fonctions لي فل model Product بإستثناء emptyCart لي كتنقص produit من ل panier و uploadPhoto لي كنزيد بها الصور الخاصة بكل produit زدتو ف dossier غادي يكون ف public وسميتو uploads.
الكود ديال الملف هو :
<?php
class ProductsController{
public function getAllProducts(){
$products = Product::getAll();
return $products;
}
public function getProductsByCategory($id){
if(isset($id)){
$data = array(
'id' => $id
);
$products = Product::getProductByCat($data);
return $products;
}
}
public function getProduct(){
if(isset($_POST["product_id"])){
$data = array(
'id' => $_POST["product_id"]
);
$product = Product::getProductById($data);
return $product;
}
}
public function emptyCart($id,$price){
unset($_SESSION["products_".$id]);
$_SESSION["count"] -= 1;
$_SESSION["totaux"] -= $price;
Redirect::to("cart");
}
public function newProduct(){
if(isset($_POST["submit"])){
$data = array(
"product_title" => $_POST["product_title"],
"product_description" => $_POST["product_description"],
"product_quantity" => $_POST["product_quantity"],
"short_desc" => $_POST["short_desc"],
"product_image" => $this->uploadPhoto(),
"old_price" => $_POST["old_price"],
"product_price" => $_POST["product_price"],
"product_category_id" => $_POST["product_category_id"],
);
$result = Product::addProduct($data);
if($result === "ok"){
Session::set("success","Produit ajouté");
Redirect::to("products");
}else{
echo $result;
}
}
}
public function updateProduct(){
if(isset($_POST["submit"])){
$oldImage = $_POST["product_current_image"];
$data = array(
"product_id" => $_POST["product_id"],
"product_title" => $_POST["product_title"],
"product_description" => $_POST["product_description"],
"product_quantity" => $_POST["product_quantity"],
"short_desc" => $_POST["short_desc"],
"product_image" => $this->uploadPhoto($oldImage),
"old_price" => $_POST["old_price"],
"product_price" => $_POST["product_price"],
"product_category_id" => $_POST["product_category_id"],
);
$result = Product::editProduct($data);
if($result === "ok"){
Session::set("success","Produit modifié");
Redirect::to("products");
}else{
echo $result;
}
}
}
public function uploadPhoto($oldImage = null){
$dir = "public/uploads";
$time = time();
$name = str_replace(' ','-',strtolower($_FILES["image"]["name"]));
$type = $_FILES["image"]["type"];
$ext = substr($name,strpos($name,'.'));
$ext = str_replace('.','',$ext);
$name = preg_replace("/\.[^.\s]{3,4}$/", "",$name);
$imageName = $name.'-'.$time.'.'.$ext;
if(move_uploaded_file($_FILES["image"]["tmp_name"],$dir."/".$imageName)){
return $imageName;
}
return $oldImage;
}
public function removeProduct(){
if(isset($_POST["delete_product_id"])){
$data = array(
"id" => $_POST["delete_product_id"]
);
$result = Product::deleteProduct($data);
if($result === "ok"){
Session::set("success","Produit supprimé");
Redirect::to("products");
}else{
echo $result;
}
}
}
}
3- إضافة ل HomeController
دائما ف dossier controllers زيد fichier جديد سميه HomeController هذا هو ل controller لي غادي يمكن من عرض les pages ديالنا عندي فيه fonction index لي كتاخذ إسم la page ومنبعد كنسترجعها من dossier views لي غادي نزيدو من بعد وكنعرضها.
الكود ديال الملف هو :
<?php
class HomeController{
public function index($page){
include('views/'.$page.'.php');
}
}