Home  • Programming • PHP

Shopping cart(updated :increment by 1) 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>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'>";
		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 existed
	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=1;
		 $total=$price*$pcs;
		 $_SESSION['grossTotal']=0;
		
		 
		// check whether a data is existed or not if existed then update the amount of products  		 
		if(array_key_exists($id, $_SESSION['cart'])){
			 $_SESSION['cart'][$id]['pcs']+=$pcs;
			 $_SESSION['cart'][$id]['gross']=$_SESSION['cart'][$id]['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;
			
			
		}
	}
	//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'];
			 }
			 
			//show the cart
			$sl=0;
			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

create-a-database-drop-if

if you refresh the page after updating the item you will find that the item is still incremented by 1. To solve this create another file and header this file from that file and also header that file from this file after incrementing(update operation) the item

Dear all friends,

its your time to solve how to decrement the item number by 1 and when the item number is 0 then that item will be removed. Thanks

Comments 4


great job
darun...........
good job. Thx.
good job.

Share

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