Shopping Cart in PHP OOP
Hi Friends today we are going to learn how to create a simple session based shopping cart using php object oriented programming concepts..This is very simple codes i hope this will help you understand cart concept in php oop..
Shopping Cart in PHP OOP |
Database and Table=>
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 product
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');
Create 4 files named:
1. index.php
2. cartclass.php
3. cart.php
4. style.css
now copy the below codes and save it..
1. index.php
<?php include('cartclass.php'); $obj=new Cart(); //Sending Product code to CartClass if(isset($_POST['add'])){ $code=$_POST['code']; $result=$obj->addtocart($code); } ?> <html> <head> <title>PHP MySQL Shopping Cart in PHP OOP</title> <link rel="stylesheet" type="text/css" href="style.css"/> </head> <table> <tr> <th>Name</th> <th>Price</th> <th>Code</th> <th>Quantity</th> <th>Action</th> </tr> <?php $query="SELECT*FROM product"; $result=$obj->fetch($query); if($result){ while($row=$result->fetch_assoc()){?> <form method="POST" action=""> <tr> <td><?php echo $row['name'];?></td> <td>$<?php echo $row['price'];?></td> <td><?php echo strtoupper($row['code']);?></td> <td><input type="text" name="quantity" value="1"/></td> <input type="hidden" name="code" value="<?php echo $row['code'];?>"/> <td><input type="submit" name="add" value="Add To Cart"/></td> </tr> </form> <?php } } ?> </html>
2. cartclass.php
<?php session_start(); class Cart{ protected $host="localhost"; protected $user="root"; protected $pass=""; protected $dbname="codenair"; public $link; private $error; public function __construct(){ $this->connectDB(); } //Creating Database Connection public function connectDB(){ $this->link=new mysqli($this->host,$this->user,$this->pass,$this->dbname); if(!$this->link){ $this->error="Connection Error".$this->link->connect_error; return false; } } //Fetch Product From Database public function fetch($query){ $result=$this->link->query($query); if($result->num_rows>0){ return $result; }else{ return false; } } //fetch Single Product and Adding Cart public function addtocart($code){ $result=mysqli_query($this->link,"SELECT*FROM product WHERE code='$code'"); $row=mysqli_fetch_array($result); $item=array( $row['code']=>array( 'name'=>$row['name'], 'price'=>$row['price'], 'quantity'=>$_POST['quantity'], 'code'=>$row['code'] )); if(!empty($_SESSION['cart'])){ if(in_array($row['code'],$_SESSION['cart'])){ foreach($_SESSION['cart'] as $k=>$v){ if($row['code']==$k) //Updating Existing Product Quantity $_SESSION['cart'][$k]['quantity']=$_POST['quantity']; header('location:cart.php'); } }else{ $_SESSION['cart']=array_merge($_SESSION['cart'],$item); header('location:cart.php'); } }else{ $_SESSION['cart']=$item; header('location:cart.php'); } } //Removing Product public function remove($code){ foreach($_SESSION["cart"] as $k => $v) { if($code == $k) unset($_SESSION["cart"][$k]); header('location:cart.php'); } } } ?>
3. cart.php
<?php include("cartclass.php"); $obj=new Cart(); if(isset($_GET["action"])){ $code=$_GET['code']; $result=$obj->remove($code); } if(!empty($_SESSION['cart'])){ $total=0; ?> <link rel="stylesheet" type="text/css" href="style.css"/> <table> <tr> <th>Name</th> <th>Price</th> <th>Quantity</th> <th>Total Price</th> <th>Action</th> </tr> <?php foreach($_SESSION['cart'] as $row){ ?> <tr> <td><?php echo $row['name'];?></td> <td>$<?php echo number_format($row['price'],2);?></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'];?>">Remove</a></td> </tr> <?php $total +=$row['price']*$row['quantity']; };?> <tr> <td colspan="3"><b>Total price:</b></td> <td colspan="4"><b>$<?php echo number_format($total,2);?></b></td> </tr> </table> <?php }else{ echo '<div><b>Your Cart is Empty</b></div>'; }?> <a href="index.php">Continue Shopping</a>
4. style.css
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 Shopping Cart in PHP OOP Concepts..
Please share and Comments..Thank You..
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.