Home  • University • Campus

Shopping cart developed by Moshiur Rahman

#create a database
drop database if exists shopping_cart;
create database shopping_cart;
use shopping_cart;
create table products(id int(10) auto_increment primary key, product_name text, price text, uom text);
insert into products(product_name,price,uom)values('mobile','5000','pcs');
insert into products(product_name,price,uom)values('headPhone','500','pcs');
insert into products(product_name,price,uom)values('laptop','50000','pcs');

//create a php file and copy or write the following code
<table border="1"> <?php session_start(); ?>
	<tr><th>ID</th><th>Name</th><th>Price</th><th>UOM</th><th>Number of pcs/kg</th><th>Add to cart</th></tr>
<?php
	$db=new mysqli('localhost','root','','shopping_cart') or die("database not connected");
	$table=$db->query("select * from products") or die("table not connected");
	while(list($id,$product_name,$price,$uom)=$table->fetch_row()){
		echo "<tr><td>$id</td>";
		echo "<td>$product_name</td>";
		echo "<td>$price</td>";
		echo "<td>$uom</td>";
		echo "<form action='' method='post'><td>
			<input type='number' name='pcs' value='0'></td>";
		echo "<td>
			
				<input type='hidden' name='cid' value='$id'>
				<input type='hidden' name='product_name' value='$product_name'>
				<input type='hidden' name='price' value='$price'>
				<input type='hidden' name='uom' value='$uom'>
				<input type='submit' name='btnAdd' value='Add to Cart'>
			</form>
			</td>
		</tr>";
	}


?>
</table>
<br>
<div>
	<table border="1" cellspacing="0px" cellpadding="10px">
		<tr><th>ID</th><th>Name</th><th>UOM</th><th>Total pcs/kg</th><th>Price</th><th>Total Amount</th><th>Remove item</th></tr>
</div>
<?php
//create a session variable if not exists
	if(!isset($_SESSION['cart'])){
   		$_SESSION['cart'] = array();
	}
//write them for skipping undefined warning	
	isset($_SESSION['cart'][$id]['id']) ? $_SESSION['cart'][$id]['id'] : "";
	isset($_SESSION['cart'][$id]['product_name']) ? $_SESSION['cart'][$id]['product_name'] : "";
	isset($_SESSION['cart'][$id]['uom']) ? $_SESSION['cart'][$id]['uom'] : "";
	isset($_SESSION['cart'][$id]['pcs']) ?  $_SESSION['cart'][$id]['pcs'] : "";
	isset($_SESSION['cart'][$id]['gross']) ? $_SESSION['cart'][$id]['gross'] : "";

	if(isset($_POST['btnAdd'])){
		
// this option is for taking form data
		 $id=$_POST["cid"];
		 $product_name=$_POST["product_name"];
		 $price=$_POST["price"];
		 $uom=$_POST["uom"];
		 $pcs=$_POST["pcs"];
		 $total=$price*$pcs;
		 $_SESSION['grossTotal']=0;
		
// check whether a data is existed or not if existed then unset the specific id and again add data 		 
		 
		if(array_key_exists($id, $_SESSION['cart'])){
			//echo "yes";
			
			//unset($_SESSION['cart'][$id]);
			 $_SESSION['cart'][$id]['id']=$id;
			 $_SESSION['cart'][$id]['product_name']=$product_name;
			 $_SESSION['cart'][$id]['uom']=$uom;
			 $_SESSION['cart'][$id]['pcs']=$pcs;
			 $_SESSION['cart'][$id]['price']=$price;
			 $_SESSION['cart'][$id]['gross']=$pcs*$price;
   		}else{
			
			// if not existed then add data
			 $_SESSION['cart'][$id]['id']=$id;
			 $_SESSION['cart'][$id]['product_name']=$product_name;
			 $_SESSION['cart'][$id]['uom']=$uom;
			 $_SESSION['cart'][$id]['pcs']=$pcs;
			 $_SESSION['cart'][$id]['price']=$price;
			 $_SESSION['cart'][$id]['gross']=$pcs*$price;
			 $cartgross=$_SESSION['cart'][$id]['gross'];
		}
	}


//use for removing item
	if(isset($_POST['btnRemove'])){
				$rid=$_POST['rid'];
				unset($_SESSION['cart'][$rid]);
		
	}
//count total amount	
			$totalgr=0;
			foreach(  $_SESSION['cart'] as $gid=>$ar){
					 $totalgr+=$ar['gross'];
			 }

			$sl=0;
//show the cart
			foreach($_SESSION['cart'] as $item){
				$sl+=1;
				echo "<tr>";
				echo "<td>$sl</td>";
				echo "<td>$item[product_name]</td>";
				echo "<td>$item[uom]</td>";
				echo "<td>$item[pcs]</td>";
				echo "<td>$item[price]</td>";
				echo "<td>$item[gross]</td>";
				echo "<td><form action='' method='post'>
							<input type='hidden' name='rid' value='$item[id]'>
							<input type='submit' name='btnRemove' value='Remove Item'>
						</form>
					</td>";
				echo "</tr>";
				
			}echo "<tr>
					<td colspan='5'>Gross Total</td>
					<td style='background-color:lightgray'>".$totalgr."</td>
				</tr>";
 
	
?>

</table>

Output is like that

Shopping cart image

Comments 4


thanks dear...
welcome
thx
WOW!!!!!!!!!!!!!!!!!!!!!! কার্টেসি Abdullah Al Mamun........................

Share

About Author
Md. Moshiur Rahman
Copyright © 2024. Powered by Intellect Software Ltd