PHP program to implement a Shopping Cart.

cart.rar

Question: Write a PHP program to implement basic Shopping Cart using the concept of Database & Session.

SQL to create database:

-- phpMyAdmin SQL Dump
-- version 3.2.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 02, 2013 at 10:51 AM
-- Server version: 5.1.41
-- PHP Version: 5.3.1

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `homeshop`
--

-- --------------------------------------------------------

--
-- Table structure for table `product`
--

CREATE TABLE IF NOT EXISTS `product` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `desc` varchar(255) NOT NULL,
  `price` int(11) NOT NULL,
  `category` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `product`
--

INSERT INTO `product` (`id`, `name`, `desc`, `price`, `category`) VALUES
(1, 'Micromax Canvas 2', 'Micromax', 10000, 'mobile'),
(2, 'Zen XYZ', 'Zen', 5000, 'tablet'),
(3, 'Nokia Lumia', 'Nokia', 30000, 'mobile'),
(4, 'Sony Bravia', 'Sony', 25000, 'TV');

config.php

<?php

ini_set("error_reporting",E_ALL);
session_start();

?>

db-connect.php

<?php
$link = mysql_connect("localhost","root","") or die("Could not connect.");

mysql_select_db("homeshop");
?>

header.php

<a href="category.php">HomeShop</a>
<?php
if(isset($_SESSION['cart']))
{
	$total_products = count($_SESSION['cart']) - 1;
	$total_quantity = $_SESSION['cart']['total'];

	echo "($total_products - $total_quantity)";
	// echo "<pre>";
	// print_r($_SESSION['cart']);
	// echo "</pre>";
}
?>
<br /><br />

footer.php

<br />
Copyright HomeShop <?php echo date("Y"); ?>

category.php (This is the home page)

<?php
require_once("config.php");
?>
<html>
	<body>
		<?php require_once("header.php"); ?>

		<?php

		require_once("db-connect.php");

		$sql = "SELECT DISTINCT category FROM product";

		$rs = mysql_query($sql);

		while($row = mysql_fetch_assoc($rs))
		{
			$category = $row['category'];
			echo "<a href='product.php?cat=$category'>$category</a>";
			echo "<br />";	
		}

		?>

		<?php require_once("footer.php"); ?>
	</body>
</html>

product.php

<?php
require_once("config.php");
?>
<html>
	<body>
		<?php require_once("header.php"); ?>

		<?php		
		require_once("db-connect.php");

		$category = $_GET['cat'];

		$sql = "SELECT id, name FROM product WHERE category='$category'";
		$rs = mysql_query($sql);

		while($row = mysql_fetch_assoc($rs))
		{
			$prod_id = $row['id'];
			$prod_name = $row['name'];
			echo "<a href='product-details.php?pid=$prod_id'>$prod_name</a>";
			echo "<br />";
		}

		?>

		<?php require_once("footer.php"); ?>
	</body>
</html>

product-details.php

<?php
require_once("config.php");
?>
<html>
	<body>
		<?php require_once("header.php"); ?>

		<?php require_once("db-connect.php"); ?>

		<?php
		$product_id = $_GET['pid'];
		$_SESSION['prod_id'] = $product_id;

		$sql  = "SELECT * FROM product WHERE id='$product_id' LIMIT 1";
		$rs = mysql_query($sql);

		$row = mysql_fetch_assoc($rs);
		echo $row['name']."-".$row['desc'];
		echo "<br />";
		echo $row['price'];
		?>
		<form method="POST" action="add-cart.php">
			Quantity:
			<input type="text" name="quantity" value="1" />
			<br />
			<input type="submit" name="submit_form" value="Add to Cart" />
		</form>

		<?php require_once("footer.php"); ?>
	</body>
</html>

add-cart.php

<?php
require_once("config.php");

$product_id = $_SESSION['prod_id'];
$quantity = $_POST['quantity'];

if(isset($_SESSION['cart'][$product_id]))
{
	$_SESSION['cart'][$product_id] += $quantity;
}
else
{
	$_SESSION['cart'][$product_id] = $quantity;
}

if(isset($_SESSION['cart']['total']))
{
	$_SESSION['cart']['total'] += $quantity;
}
else
{
	$_SESSION['cart']['total'] = $quantity;
}

// echo "<pre>";
// print_r($_SESSION['cart']);
header("Location: category.php");
exit;

?>