ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Shopping Cart in PHP

addtocart
Today, I’m going to teach you how to create a shopping cart. Shopping cart can be usually seen in the website that has sales product. This is very useful because this is where you can order multiple products in every transaction that you made. You will also see here how powerful the functionality of the session. It contain features such as Listing Product, Adding , Updating and Removing the item in the cart. You are going to use Bootstrap Templates for the design of this project.
Let’s begin:
1.Create a MySQL Database and name it “productdb”.
2.Execute the query for adding the table in the MySQL database.

  1. CREATE TABLE IF NOT EXISTS `tblproduct` (
  2. `PRODUCTID` int(11) NOT NULL AUTO_INCREMENT,
  3. `MODEL` varchar(30) NOT NULL,
  4. `BRAND` varchar(30) NOT NULL,
  5. `DESCRIPTION` varchar(99) NOT NULL,
  6. `PRICE` double NOT NULL,
  7. PRIMARY KEY (`PRODUCTID`)

3. Execute the query for inserting data in the table.

  1. INSERT INTO `tblproduct` (`PRODUCTID`, `MODEL`, `BRAND`, `DESCRIPTION`, `PRICE`) VALUES
  2. (1, 'HK-LE3212UVP', 'Sharp', 'LCD VIDEO-KARAOKE 32''''', 33298),
  3. (2, 'HK-LE1110UVP', 'Sharp', 'LCD VIDEO-KARAOKE 19''''', 22198),
  4. (3, '21V-FS720S', 'Sharp', 'Pure Flat TV ', 7190),
  5. (4, 'ES-D708', 'Sharp', 'Spin Dryer 7kg ', 4998),
  6. (5, 'ES-D958', 'Sharp', 'Spin Dryer 9.5 KG', 5698),
  7. (6, 'SJ-DT55AS', 'Sharp', '5.4 CU.FT S/D SEMI AUTO ', 10900);

4. Create a connection between PHP script and MySQL Database. Name it “config.php”

  1. $server = 'localhost';
  2. $dbuser = 'root';
  3. $dbpass = '';
  4. $dbname = 'productdb';
  5. $con    = mysql_connect($server, $dbuser, $dbpass);
  6. if (isset($con)) {
  7. # code...
  8. $dbSelect = mysql_select_db($dbname);
  9. if (!$dbSelect) {
  10. echo "Problem in selecting database! Please contact administraator";
  11. }
  12. } else {
  13. echo "Problem in database connection! Please contact administraator";
  14. }

5. Create a design for the index. Name it “index.php”

  1. <!-- CSS plugins -->
  2.  
  3. &lt;?php
  4. include "config.php";
  5. ?&gt;
  6. &lt;html&gt;
  7. &lt;head&gt;
  8. &lt;title&gt;Product&lt;/title&gt;
  9. &lt;!-- CSS plugins --&gt;
  10. &lt;link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"&gt;
  11. &lt;/head&gt;
  12. &lt;body&gt;
  13. &lt;!-- containner --&gt;
  14. &lt;div class="container"&gt;
  15. &lt;!-- row --&gt;
  16. &lt;div class="row"&gt;
  17. &lt;h1 class="page-header"&gt;Product&lt;/h1&gt;
  18. &lt;!-- side bar --&gt;
  19. &lt;div class="sidebar col-md-2"&gt;
  20. &lt;!-- panel --&gt;
  21. &lt;div class="panel panel-default " style="min-height:400px;"&gt;
  22. &lt;!-- ul --&gt;
  23. &lt;ul &gt;
  24. &lt;li class="list-unstyled" &gt;&lt;h4 class="page-header"&gt;MENU&lt;/h4&gt;&lt;/li&gt;
  25. &lt;li class="list-unstyled"&gt;
  26. &lt;!-- link product --&gt;
  27. &lt;a href="index.php"&gt;
  28. Product
  29. &lt;/a&gt;
  30. &lt;!-- end link product --&gt;
  31. &lt;/li&gt;
  32. &lt;li class="list-unstyled" &gt;
  33. &lt;!-- link cart --&gt;
  34. &lt;a href="cart.php"&gt;
  35. Cart
  36. &lt;/a&gt;
  37. &lt;!-- end link cart --&gt;
  38. &lt;/li&gt;
  39. &lt;/ul&gt;
  40. &lt;!-- end ul --&gt;
  41. &lt;/div&gt;
  42. &lt;!-- end panel --&gt;
  43. &lt;/div&gt;
  44. &lt;!-- end Sidebar --&gt;
  45.  
  46. &lt;!-- Content --&gt;
  47. &lt;div class="col-md-10"&gt;
  48. &lt;table class="table table-hover"&gt;
  49. &lt;tr&gt;
  50. &lt;th&gt;Product Id&lt;/th&gt;
  51. &lt;th&gt;Model&lt;/th&gt;
  52. &lt;th&gt;Brand&lt;/th&gt;
  53. &lt;th&gt;Description&lt;/th&gt;
  54. &lt;th&gt;Price&lt;/th&gt;
  55. &lt;th&gt;Action&lt;/th&gt;
  56. &lt;/tr&gt;
  57. &lt;tbody&gt;
  58.  
  59. &lt;?php
  60. $sqlQuery = "SELECT * FROM `tblproduct`";
  61. $result = mysql_query($sqlQuery) or die(mysql_error());
  62. while ($row = mysql_fetch_array($result)) {
  63. # code...
  64. echo '&lt;tr&gt;
  65. &lt;td&gt;' . $row['PRODUCTID'] . '&lt;/td&gt;
  66. &lt;td&gt;' . $row['MODEL'] . '&lt;/td&gt;
  67. &lt;td&gt;' . $row['BRAND'] . '&lt;/td&gt;
  68. &lt;td&gt;' . $row['DESCRIPTION'] . '&lt;/td&gt;
  69. &lt;td&gt;' . $row['PRICE'] . '&lt;/td&gt;
  70. &lt;td&gt;&lt;a href="process.php?Id=' . $row['PRODUCTID'] . '&amp;Price=' . $row['PRICE'] . '" class="btn btn-xs btn-primary"&gt;Add to cart&lt;/a&gt;&lt;/td&gt;
  71. &lt;/tr&gt;';
  72. }
  73. ?&gt;
  74. &lt;/tbody&gt;
  75. &lt;/table&gt;
  76. &lt;/div&gt;
  77. &lt;!-- end Content --&gt;
  78.  
  79. &lt;/div&gt;
  80. &lt;!-- end row --&gt;
  81. &lt;/div&gt;
  82. &lt;!-- end container --&gt;
  83. &lt;/body&gt;
  84. &lt;/html&gt;
  85.  
  86. <!-- containner -->

6.Do the following code for the cart list. Name it “cart.php”

  1. &lt;?php
  2. // start the session to fire the session handler
  3. // include the configuration
  4. include "config.php";
  5. // Update the quantity in the cart
  6. if (isset($_POST['Update'])) {
  7. $max = count($_SESSION['janobecart']);
  8. for ($i = 0; $i &lt; $max; $i++) {
  9. $pid = $_SESSION['janobecart'][$i]['PRODUCTID'];
  10. // parse the quantity in interger.
  11. $qty = intval($_REQUEST['qty' . $pid]);
  12. // selecting the items that are already in the cart
  13. $sql = "SELECT * FROM `tblproduct` WHERE `PRODUCTID` =" . $pid;
  14. $result = mysql_query($sql) or die(mysql_error());
  15. while ($row = mysql_fetch_array($result)) {
  16. // checking the quantity of the item.
  17. // It can't be null
  18. if ($qty &gt; 0 &amp;&amp; $qty &lt;= 999) {
  19. // calculting the quantity and the price of an item
  20. $price                                  = $row['PRICE'] * $qty;
  21. // updating the items in the cart.
  22. $_SESSION['janobecart'][$i]['QUANTITY'] = $qty;
  23. $_SESSION['janobecart'][$i]['PRICE']    = $price;
  24. }
  25. }
  26. }
  27. }
  28. ?&gt;
  29.  
  30. &lt;!--Design  --&gt;
  31. &lt;html&gt;
  32. &lt;head&gt;
  33. &lt;title&gt;Add to cart&lt;/title&gt;
  34. &lt;link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"&gt;
  35.  
  36. &lt;/head&gt;
  37. &lt;body&gt;
  38. &lt;!-- form --&gt;
  39. &lt;form class="form-group" method="POST" action=""&gt;
  40. &lt;!-- containner --&gt;
  41. &lt;div class="container"&gt;
  42. &lt;!-- row --&gt;
  43. &lt;div class="row"&gt;
  44. &lt;h1 class="page-header"&gt;Cart&lt;/h1&gt;
  45. &lt;!-- side bar --&gt;
  46. &lt;div class="sidebar col-md-2"&gt;
  47. &lt;!-- panel --&gt;
  48. &lt;div class="panel panel-default " style="min-height:400px;"&gt;
  49. &lt;!-- ul --&gt;
  50. &lt;ul &gt;
  51. &lt;li class="list-unstyled" &gt;&lt;h4 class="page-header"&gt;MENU&lt;/h4&gt;&lt;/li&gt;
  52. &lt;li class="list-unstyled"&gt;
  53. &lt;!-- link product --&gt;
  54. &lt;a href="index.php"&gt;
  55. Product
  56. &lt;/a&gt;
  57. &lt;!-- end link product --&gt;
  58. &lt;/li&gt;
  59. &lt;li class="list-unstyled" &gt;
  60. &lt;!-- link cart --&gt;
  61. &lt;a href="cart.php"&gt;
  62. Cart
  63. &lt;/a&gt;
  64. &lt;!-- end link cart --&gt;
  65. &lt;/li&gt;
  66. &lt;/ul&gt;
  67. &lt;!-- end ul --&gt;
  68. &lt;/div&gt;
  69. &lt;!-- end panel --&gt;
  70. &lt;/div&gt;
  71. &lt;!-- end Sidebar --&gt;
  72.  
  73. &lt;!-- Content --&gt;
  74. &lt;div class="col-md-10"&gt;
  75. &lt;table class="table table-bordered"&gt;
  76. &lt;tr&gt;
  77. &lt;th&gt;Product Id&lt;/th&gt;
  78. &lt;th&gt;Model&lt;/th&gt;
  79. &lt;th&gt;Brand&lt;/th&gt;
  80. &lt;th&gt;Description&lt;/th&gt;
  81. &lt;th&gt;Price&lt;/th&gt;
  82. &lt;th&gt;Quantity&lt;/th&gt;
  83. &lt;th&gt;Subtotal&lt;/th&gt;
  84. &lt;/tr&gt;
  85. &lt;tbody&gt;
  86.  
  87. &lt;?php
  88. if (isset($_SESSION['janobecart'])) {
  89. $count_cart = count($_SESSION['janobecart']);
  90. for ($i = 0; $i &lt; $count_cart; $i++) {
  91. $proid    = $_SESSION['janobecart'][$i]['PRODUCTID'];
  92. $qty      = $_SESSION['janobecart'][$i]['QUANTITY'];
  93. $subtot   = $_SESSION['janobecart'][$i]['PRICE'];
  94. $sqlQuery = "SELECT * FROM `tblproduct` WHERE PRODUCTID=" . $proid;
  95. $result = mysql_query($sqlQuery) or die(mysql_error());
  96. while ($row = mysql_fetch_array($result)) {
  97. # code...
  98. echo '&lt;tr&gt;
  99. &lt;td&gt;' . $row['PRODUCTID'] . '&lt;/td&gt;
  100. &lt;td&gt;' . $row['MODEL'] . '&lt;/td&gt;
  101. &lt;td&gt;' . $row['BRAND'] . '&lt;/td&gt;
  102. &lt;td&gt;' . $row['DESCRIPTION'] . '&lt;/td&gt;
  103. &lt;td&gt;' . $row['PRICE'] . '&lt;/td&gt;
  104. &lt;td&gt;
  105. &lt;input type="input" class="input input-xs"  name="qty' . $proid . '" value=' . $qty . '  maxlength="3" size="3"  /&gt;
  106. &lt;input type="submit" class="btn btn-xs btn-primary"  name="Update" value="Update" /&gt;
  107. &lt;a  href="process.php?RemoveId=' . $proid . '" class="btn  btn-danger btn-xs"   /&gt;Remove&lt;/a&gt;
  108. &lt;/td&gt;
  109. &lt;td&gt;' . $subtot . '&lt;/td&gt;
  110. &lt;/tr&gt;';
  111. }
  112. // computing the total price of the items in the cart
  113. @$tot += $subtot;
  114. $_SESSION['tot'] = $tot;
  115. }
  116. } else {
  117. }
  118. ?&gt;
  119. &lt;tr&gt;
  120. &lt;!-- Displaying the total amount of the items in the cart. --&gt;
  121. &lt;td colspan="6"&gt;&lt;h3 align="right"&gt;Total&lt;/h3&gt;&lt;/td&gt;
  122. &lt;td&gt;
  123. &lt;h3&gt;
  124. &lt;?php
  125. echo isset($_SESSION['tot']) ? $_SESSION['tot'] : '0.00';
  126. ?&gt;
  127. &lt;/h3&gt;
  128. &lt;/td&gt;
  129. &lt;/tr&gt;
  130. &lt;/tbody&gt;
  131. &lt;/table&gt;
  132. &lt;/div&gt;
  133. &lt;!-- end Content --&gt;
  134.  
  135. &lt;/div&gt;
  136. &lt;!-- end row --&gt;
  137. &lt;/div&gt;
  138. &lt;!-- end container --&gt;
  139. &lt;/form&gt;
  140. &lt;!-- end form --&gt;
  141. &lt;/body&gt;
  142. &lt;/html&gt;

7. Do the following code for adding and removing the item in the cart. Name it “process.php”

  1. &lt;?php
  2. /*start the session */
  3. /*set the config*/
  4. include "config.php";
  5. /*set a session variable */
  6. if (!isset($_SESSION['janobecart'])) {
  7. $_SESSION['janobecart'] = '';
  8. }
  9. /*function for adding  product in the cart*/
  10. if (isset($_GET['Id'])) {
  11. $pid   = intval($_GET['Id']);
  12. $price = $_GET['Price'];
  13. /*checking if the session variable is empty or not*/
  14. if (!empty($_SESSION['janobecart'])) {
  15. // count the session array varible
  16. $max = count($_SESSION['janobecart']);
  17. /* segregating session array variable.*/
  18. for ($i = 0; $i &lt; $max; $i++) {
  19. /*checking if the item does exist*/
  20. if ($pid == $_SESSION['janobecart'][$i]['PRODUCTID']) {
  21. $exist = 1;
  22. }
  23. }
  24. /* confirming the item if it does not exist.*/
  25. if (!isset($exist)) {
  26. /*adding the item in the session array variable*/
  27. if (is_array($_SESSION['janobecart'])) {
  28. $max                                       = count($_SESSION['janobecart']);
  29. $_SESSION['janobecart'][$max]['PRODUCTID'] = $pid;
  30. $_SESSION['janobecart'][$max]['PRICE']     = $price;
  31. $_SESSION['janobecart'][$max]['QUANTITY']  = 1;
  32. } else {
  33. $_SESSION['janobecart']                 = array();
  34. $_SESSION['janobecart'][0]['PRODUCTID'] = $pid;
  35. $_SESSION['janobecart'][0]['PRICE']     = $price;
  36. $_SESSION['janobecart'][0]['QUANTITY']  = 1;
  37. }
  38. } else {
  39. /*  return false.*/
  40. ?&gt;
javascript

  1. &lt;script type="text/javascript"&gt;
  2. /*    &lt;!-- Pop-up Message  --&gt;*/
  3. alert('Item is already in the cart.')
  4. /*&lt;!-- End pop-up message --&gt;*/
  5. /* redirect to main page.*/
  6. window.location='cart.php'
  7. /*end redirect*/
  8. &lt;/script&gt;
  1. &lt;?php
  2. }
  3. } else {
  4. /*adding the item in the session array variable*/
  5. $_SESSION['janobecart']                 = array();
  6. $_SESSION['janobecart'][0]['PRODUCTID'] = $pid;
  7. $_SESSION['janobecart'][0]['PRICE']     = $price;
  8. $_SESSION['janobecart'][0]['QUANTITY']  = 1;
  9. }
  10. ?&gt;
javascript

  1. &lt;script type="text/javascript"&gt;
  2. /*      &lt;!-- Pop-up Message  --&gt;*/
  3. alert('Item has been added in the cart.')
  4. /* &lt;!-- End pop-up message --&gt;*/
  5. /* redirect to main page.*/
  6. window.location='cart.php'
  7. /* end redirect*/
  8. &lt;/script&gt;
  1. &lt;?php
  2. }
  3. /*function for removing item in the cart.*/
  4. if (isset($_GET['RemoveId'])) {
  5. $pid = intval($_GET['RemoveId']);
  6. $max = count($_SESSION['janobecart']);
  7. for ($i = 0; $i &lt; $max; $i++) {
  8. if ($pid == $_SESSION['janobecart'][$i]['PRODUCTID']) {
  9. /*  removing item in the session array*/
  10. unset($_SESSION['janobecart'][$i]);
  11. /* unset the total price in the cart*/
  12. unset($_SESSION['tot']);
  13. ?&gt;
javascript

  1. &lt;script type="text/javascript"&gt;
  2. /*    &lt;!-- Pop-up Message  --&gt;*/
  3. alert('Item has been removed in the cart.')
  4. /* &lt;!-- End pop-up message --&gt;
  5. redirect to main page.*/
  6. window.location='cart.php'
  7. /* end redirect*/
  8. &lt;/script&gt;
  1. &lt;?php
  2. }
  3. }
  4. /*Returning all the values of an array*/
  5. $_SESSION['janobecart'] = array_values($_SESSION['janobecart']);
  6. }
  7. ?&gt;

Complete Sourcecode is available here.

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!