Ex No: 15 |
Create a Shopping-Cart using PHP and MySQL |
Aim: |
To create a Shopping-Cart using PHP and MySQL |
Algorithm |
|
|
Step1: Create a new Database in MySQL "cartdb". |
|
Step2: Create a new Table "product_table" with the following columns. |
|
A) "prod_id" as Primary key, data type varchar(5). |
|
B) "prod_name" data type varchar(50). |
|
C) "prod_desc" data type text. |
|
D) "prod_price" data type float. |
|
E) "prod_image" data type varchar(10). |
|
Step3: Create a new Table "cart_table" with the following columns. |
|
A) "cartid" as Primary key, data type integer with autoincrement enabled. |
|
B) "prod_id" data type varchar(5). |
|
C) "qty" data type integer. |
|
Step4: Create "index.php" and "dbcon.php" file to access database and display cart details. |
|
Step4: Create "cart.php" file to insert selected product in the cart. |
|
Step5: Create "remove.php" to remove product form the cart. |
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Shopping Cart</title>
<script>
function add(prodid)
{
qty = document.getElementById('qty').value;
window.location='cart.php?prodid='+prodid+'&qty='+qty;
}
function remove(cartid)
{
window.location='remove.php?cartid='+cartid;
}
</script>
</head>
<body>
<?php
include("dbcon.php");
$obj = new db_con();
$table = $obj->get_prod_disp();
?>
<h1>Shopping Cart Data</h1>
<form >
<?php
echo $table;
?>
</form>
<hr>
<h1>Cart Details</h1>
<form >
<table border=1>
<thead>
<tr><th>Product</th><th>Quantity</th><th>Price</th><th>Amount</th><th></th></tr>
</thead>
<tbody>
<?php
$table = $obj->get_cart();
echo $table;
?>
</tbody>
</table>
</form>
</body>
</html>
dbcon.php
<?php
class db_con
{
private $host;
private $db;
private $usr;
private $pwd;
private $con;
function __construct()
{
$this->host = "localhost:3306";
$this->db = "cartdb";
$this->usr = "root";
$this->pwd = '';
$this->con = mysqli_connect($this->host,$this->usr,$this->pwd,$this->db);
if(mysqli_connect_errno())
{
echo "Connection Error";
}
}
function get_prod_disp()
{
$qry = "SELECT prod_id, prod_name, prod_desc, prod_price, prod_image FROM product_table";
$res = mysqli_query($this->con,$qry) or die("Error in Query");
$numrows = mysqli_num_rows($res);
if($numrows>=1)
{
$table="";
while($val = mysqli_fetch_array($res))
{
$img = "<img src='images/$val[4]' width='60px' height='60px'>";
$table = $table."<table><tr><td colspan='2'>$img</td></tr>
<tr><td>Product </td><td>$val[1]</td></tr><tr>
<td>Description </td><td>$val[2]</td></tr><tr>
<td>Price </td><td>$val[3]</td></tr>
<td>Quantity </td><td><input type='text' name='qty' id='qty' size='2' value='1'></td></tr>
<tr><td colspan='2'><input type='button' value='Add to cart' onclick='add($val[0])'></td></tr>
</table>";
}
}
else
{
$table = "<table><tr><th colspan='2'><h1>No Product in Database</h1></th></tr></table>";
}
return $table;
}
function get_cart()
{
$qry = "select cartid, cart_table.prod_id, prod_name, prod_price, qty
from product_table inner join cart_table on(product_table.prod_id = cart_table.prod_id)";
$res = mysqli_query($this->con,$qry) or die("Unable to Get Cart Details");
$numrows = mysqli_num_rows($res);
if($numrows>=1)
{
$row="";
$total = 0;
while($val = mysqli_fetch_array($res))
{
$amount = $val[4]*$val[3];
$total = $total + $amount;
$row = $row. "<tr><td>$val[2]</td><td>$val[4]</td><td>$val[3]</td><td>$amount</td>
<td><input type='button' value='Remove' onclick='remove($val[0])'></td></tr>";
}
$row = $row."<tr><td colspan='4'>Total </td><td>$total</td>";
}
else
{
$row = "<tr><th colspan='5'><h1>No Product in Cart</h1></th></tr>";
}
return $row;
}
function add_cart($pid, $qty)
{
$qry="INSERT INTO cart_table(prod_id, qty) VALUES ('$pid',$qty)";
$res = mysqli_query($this->con,$qry) or die("Unable to Insert Product");
$numrows = mysqli_affected_rows($this->con);
if($numrows == 1)
{
return true;
}
else
{
return false;
}
}
function remove_cart($cartid)
{
$qry = "DELETE FROM cart_table WHERE cartid = '$cartid'";
$delres = mysqli_query($this->con,$qry) or die("Unable to Delete Product");
$numrows = mysqli_affected_rows($this->con);
if($numrows == 1)
{
return true;
}
else
{
return false;
}
}
}
?>
cart.php
<?php
<?php
include("dbcon.php");
$obj = new db_con();
//Get user form data
$pid = $_GET["prodid"];
$qty = $_GET["qty"];
if($qty>=1)
{
echo $qty." ".$pid;
$addcart = $obj->add_cart($pid,$qty);
if($addcart)
{
header("Location: index.php");
}
else
{
echo "Unable to Delete Product";
}
}
?>
remove.php
<?php
<?php
include("dbcon.php");
$obj = new db_con();
//Get user form data
$cartid = $_GET["cartid"];
if($cartid!="")
{
$removecart = $obj->remove_cart($cartid);
if($removecart)
{
header("Location: index.php");
}
else
{
echo "Unable to Delete Product";
}
}
?>
|
Result: |
Thus a webpage is designed using php to perform Array Processing. |