Comment créer un panier en PHP, code source Créer un panier en PHP


Comment créer un panier en PHP

Structure des fichiers pour le panier

index.php: Ce fichier contiendra toute la partie de codage
config.php: ce fichier est utilisé pour la connexion à la base de données
style.css: il s’agit d’un fichier CSS utilisé à des fins de conception
dossier product-images: utilisé pour stocker les images du produit

Étape 1:

Créez une base de données, puis créez une table dans cette base de données tblproducts et insérez des données dans cette table.

structure de la table tblproducts

--
-- Table structure for table `tblproduct`
--
CREATE TABLE `tblproduct` (
`id` int(8) NOT NULL,
`name` varchar(255) NOT NULL,
`code` varchar(255) NOT NULL,
`image` text NOT NULL,
`price` double(10,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `tblproduct`
--
INSERT INTO `tblproduct` (`id`, `name`, `code`, `image`, `price`) VALUES
(1, 'MSI GF63 Thin Core i7 9th Gen', 'MSI4353', 'product-images/msi-laptop.jpeg', 1500.00),
(2, 'WD 1.5 TB Wired External Hard Disk Drive (Black)', 'WD091', 'product-images/external-hardidisk.jpeg', 50.00),
(3, 'VERTIGO Running Shoes For Men (Black)', 'LOTTO215', 'product-images/lotto-shoes.jpeg', 10.00);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tblproduct`
--
ALTER TABLE `tblproduct`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `product_code` (`code`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `tblproduct`
--
ALTER TABLE `tblproduct`
MODIFY `id` int(8) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Étape 2:

Récupérer les produits insérés

Products
">
">

Étape 3 :

Ajout de produits dans le panier d’achat.

 //code for adding product in cart
case "add":
if(!empty($_POST["quantity"])) {
$pid=$_GET["pid"];
$result=mysqli_query($con,"SELECT * FROM tblproduct WHERE id='$pid'");
while($productByCode=mysqli_fetch_array($result)){
$itemArray = array($productByCode["code"]=>array('name'=>$productByCode["name"], 'code'=>$productByCode["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["price"], 'image'=>$productByCode["image"]));
if(!empty($_SESSION["cart_item"])) {
// searches for specific value code
if(in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["code"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
//The array_merge() function merges one or more arrays into one array.
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
}
break;

Le code ci-dessus utilisé pour ajouter des produits au panier. Dans ce cas, «ajouter» gère l’action d’ajout de panier. Toutes les valeurs du panier seront stockées dans la session du panier.

Étape 4 :

Récupérer les produits du panier de la session du panier

Panier
Panier vide
Nom Code Quantité Prix ​​unitaire Prix Retirer
"class =" image-article-panier" /> "class =" btnRemoveAction ">Retirer l'objet
Total:
Votre panier est vide

Étape 5:

Retirer les produits du panier

// code for removing product from cart
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["code"] == $k)
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;

Nous utilisons unset () pour supprimer le produit du panier.

Étape 6:

Dans cette étape, videz le panier en un clic.

 // code for if cart is empty
case "empty":
unset($_SESSION["cart_item"]);
break;

Afficher la démo ————————

Télécharger le code source complet (script de panier d’achat)

Taille: 36,4 Kio

Version: V 1.0



Telecharger ici

lire plus  Fonctionnement PHP CRUD à l'aide d'une procédure stockée
Laisser un commentaire

Aller au contenu principal