PHP MySQL Shopping Cart
learn more about PHP SESSION
just follow the below codes to create a php shopping Cart..
=>Database Configuration:
Database Name: codenair
table name: product
Table Structure for table product
CREATE TABLE IF NOT EXISTS `product` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `price` int(10) NOT NULL, `code` varchar(20) NOT NULL, PRIMARY KEY(id) ) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
Dumping Data for Table Products..
INSERT INTO `product` (`id`, `name`, `price`, `code`) VALUES (NULL, 'ASUS VivoBook', 500, 'erwer'), (NULL, 'HP Power Gamming', 1230, '4gdfg'), (NULL, 'Nokia 1020', 460, 'drg546'), (NULL, 'Sony XA Ultra', 340, '5e46dg'), (NULL, 'Intel Sky Lake 4.3GHz', 340, 'dge5t'), (NULL, 'ASUS BM-86 6 Generation', 543, 'e54tgdf');
First We will display product info from database using below codes..
<?php $conn=new mysqli('localhost','root','','codenair'); $result=mysqli_query($conn,"SELECT*FROM product"); ?> <title>PHP MySQL Shopping Cart</title> <link rel="stylesheet" type="text/css" href="style.css"/> <table border="1" cellspacing="0"> <tr> <th>Name</th> <th>Price</th> <th>Quantity</th> <th>Code</th> <th>Action</th> </tr> <?php while($row=$result->fetch_array()){?> <form method="POST" action=""> <tr> <td><?php echo $row['name'];?></td> <td>$<?php echo $row['price'];?></td> <td><input type="text" name="quantity" value="1"/></td> <td><?php echo strtoupper($row['code']);?></td> <input type="hidden" name="code" value="<?php echo $row['code'];?>"/> <td><input type="submit" name="submit" value="Add To Cart"/></td> </tr> </form> <?php }?> </table>
Ok..Now when user click on 'Add To Cart' we use the below codes to add,update and remove item from session ['cart_item']..
<?php session_start(); $conn=new mysqli('localhost','root','','codenair'); if(isset($_POST['submit'])){ $code=$_POST['code']; $result=mysqli_query($conn,"SELECT*FROM product WHERE code='$code'"); $row=$result->fetch_assoc(); $item=array( $row['code']=>array( 'name'=>$row['name'], 'price'=>$row['price'], 'quantity'=>$_POST['quantity'], 'code'=>$row['code'] )); if(!empty($_SESSION['cart_item'])){ if(in_array($row['code'],$_SESSION['cart_item'])){ foreach($_SESSION['cart_item'] as $k=>$v){ if($row['code']==$k) $_SESSION['cart_item'][$k]['quantity']=$_POST['quantity']; } }else{ $_SESSION['cart_item']=array_merge($_SESSION['cart_item'],$item); } }else{ $_SESSION['cart_item']=$item; } } //Delete Item if(isset($_GET["action"])){ if($_GET['action']=='remove'){ foreach($_SESSION["cart_item"] as $k => $v) { if($_GET["code"] == $k) unset($_SESSION["cart_item"][$k]); header('location:index.php'); } } } //Empty Total Cart Item if(isset($_GET['action'])){ if($_GET['action']=='empty'){ unset($_SESSION['cart_item']); header('location:index.php'); } } ?>
In this 3rd Step we will retrieve added item from session['cart_item']
..
<?php session_start(); if(!empty($_SESSION['cart_item'])){ $total=0; ?> <b style='color:green'>Your Shopping Cart</b> <table border="1" cellspacing="0"> <tr> <th>Name</th> <th>Quantity</th> <th>Price</th> <th>Action</th> </tr> <?php foreach($_SESSION['cart_item']as $row){?> <tr> <td><?php echo $row['name'];?></td> <td><?php echo $row['quantity'];?></td> <td>$ <?php echo number_format($row['price']*$row['quantity'],2);?></td> <td><a href="?action=remove&code=<?php echo $row['code'];?>">Delete</a></td> </tr> <?php $total +=$row['price']*$row['quantity']; }?> <tr> <td colspan="2" align="right"><b>Total Price:</b></td> <td><b>$<?php echo number_format($total);?></b></td> <td><a href="?action=empty">Empty Cart</a></td> </tr> </table> <?php }else{ echo '<b style=color:red>Your Shopping Cart is Empty</b>'; } ?>
PHP MySQL Shopping Cart Full Codes index.php ::
<?php session_start(); $conn=new mysqli('localhost','root','','codenair'); if(isset($_POST['submit'])){ $code=$_POST['code']; $result=mysqli_query($conn,"SELECT*FROM product WHERE code='$code'"); $row=$result->fetch_assoc(); $item=array( $row['code']=>array( 'name'=>$row['name'], 'price'=>$row['price'], 'quantity'=>$_POST['quantity'], 'code'=>$row['code'] )); if(!empty($_SESSION['cart_item'])){ if(in_array($row['code'],$_SESSION['cart_item'])){ foreach($_SESSION['cart_item'] as $k=>$v){ if($row['code']==$k) $_SESSION['cart_item'][$k]['quantity']=$_POST['quantity']; } }else{ $_SESSION['cart_item']=array_merge($_SESSION['cart_item'],$item); } }else{ $_SESSION['cart_item']=$item; } } //Delete Item if(isset($_GET["action"])){ if($_GET['action']=='remove'){ foreach($_SESSION["cart_item"] as $k => $v) { if($_GET["code"] == $k) unset($_SESSION["cart_item"][$k]); header('location:index.php'); } } } //Empty Total Cart Item if(isset($_GET['action'])){ if($_GET['action']=='empty'){ unset($_SESSION['cart_item']); header('location:index.php'); } } ?> <?php $result=mysqli_query($conn,"SELECT*FROM product"); ?> <title>PHP MySQL Shopping Cart</title> <link rel="stylesheet" type="text/css" href="style.css"/> <table border="1" cellspacing="0"> <tr> <th>Name</th> <th>Price</th> <th>Quantity</th> <th>Code</th> <th>Action</th> </tr> <?php while($row=$result->fetch_array()){?> <form method="POST" action=""> <tr> <td><?php echo $row['name'];?></td> <td>$<?php echo $row['price'];?></td> <td><input type="text" name="quantity" value="1"/></td> <td><?php echo strtoupper($row['code']);?></td> <input type="hidden" name="code" value="<?php echo $row['code'];?>"/> <td><input type="submit" name="submit" value="Add To Cart"/></td> </tr> </form> <?php }?> </table> <!--Displaying Product from Cart--> <?php if(!empty($_SESSION['cart_item'])){ $total=0; ?> <b style='color:green'>Your Shopping Cart</b> <table border="1" cellspacing="0"> <tr> <th>Name</th> <th>Quantity</th> <th>Price</th> <th>Action</th> </tr> <?php foreach($_SESSION['cart_item']as $row){?> <tr> <td><?php echo $row['name'];?></td> <td><?php echo $row['quantity'];?></td> <td>$ <?php echo number_format($row['price']*$row['quantity'],2);?></td> <td><a href="?action=remove&code=<?php echo $row['code'];?>">Delete</a></td> </tr> <?php $total +=$row['price']*$row['quantity']; }?> <tr> <td colspan="2" align="right"><b>Total Price:</b></td> <td><b>$<?php echo number_format($total);?></b></td> <td><a href="?action=empty">Empty Cart</a></td> </tr> </table> <?php }else{ echo '<b style=color:red>Your Shopping Cart is Empty</b>'; } ?>
style.css to design cart and product table
input[type=text]{ padding:4px; border-radius:4px; width:50px; } input[type=submit]{ padding:5px; border-radius:4px; background:black; color:white; cursor:pointer; } table { border-collapse: collapse; width: 40%; } th, td { padding: 8px; } tr:nth-child(even){background-color: #f2f2f2} th { background-color: #4CAF50; color: white; }That's it Friends How to Create php MySQL Shopping Cart..Keep visiting for More Codes..
Thank You..
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.