Thread: Shopping cart
View Single Post
Join Date: Jul 2008
Posts: 9
Reputation: mirainc is an unknown quantity at this point 
Solved Threads: 0
mirainc mirainc is offline Offline
Newbie Poster

Shopping cart

 
0
  #1
Jan 11th, 2009
Hey all..
Im in desperate need to solve my shopping cart problem.
Im doing it for a school project.
The situation is my products are listed in a table.
Users will select which product they want by selecting the add to cart link.
Once they select it, the product information are sent to the cart table.
This is my current codes which I have taken reference to from a website.

Cart.php
  1. <?php
  2. $booksid = $_GET[booksid]; //the product id from the URL
  3. $action = $_GET[action]; //the action from the URL
  4.  
  5. //if there is an product_id and that product_id doesn't exist display an error message
  6. if($booksid && !productExists($booksid)) {
  7. die("Error. Product Doesn't Exist");
  8. }
  9.  
  10. switch($action) { //decide what to do
  11.  
  12. case "add":
  13. $_SESSION['cart'][$booksid]++; //add one to the quantity of the product with id $product_id
  14. break;
  15.  
  16. case "remove":
  17. $_SESSION['cart'][$booksid]--; //remove one from the quantity of the product with id $product_id
  18. if($_SESSION['cart'][$booksid] == 0) unset($_SESSION['cart'][$booksid]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
  19. break;
  20.  
  21. case "empty":
  22. unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
  23. break;
  24.  
  25. }
  26. if($_SESSION['cart']) { //if the cart isn't empty
  27. //show the cart
  28.  
  29. echo "<table border=\"1\" padding=\"3\" width=\"40%\">"; //format the cart using a HTML table
  30.  
  31. //iterate through the cart, the $product_id is the key and $quantity is the value
  32. foreach($_SESSION['cart'] as $booksid => $quantity) {
  33.  
  34. //get the name, description and price from the database - this will depend on your database implementation.
  35. //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
  36. $sql = sprintf("SELECT title, author, price, category FROM books WHERE booksid = %d;",
  37. $booksid);
  38.  
  39. $result = mysql_query($sql);
  40.  
  41. //insert books value into cart
  42. $sql = "INSERT INTO cart (cartid, cartqty, cartsessionid,)
  43. VALUES ($cartid, 1, 1)";
  44. $result = dbQuery($sql);
  45.  
  46. //Only display the row if there is a product (though there should always be as we have already checked)
  47. if(mysql_num_rows($result) > 0) {
  48.  
  49. list($title, $author, $price, $category) = mysql_fetch_row($result);
  50.  
  51. $line_cost = $price * $quantity; //work out the line cost
  52. $total = $total + $line_cost; //add to the total cost
  53.  
  54. echo "<tr>";
  55. //show this information in table cells
  56. echo "<td align=\"center\">$title</td>";
  57. //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
  58. echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$booksid\">X</a></td>";
  59. echo "<td align=\"center\">$line_cost</td>";
  60.  
  61. echo "</tr>";
  62.  
  63. }
  64.  
  65. }
  66.  
  67. //show the total
  68. echo "<tr>";
  69. echo "<td colspan=\"2\" align=\"right\">Total</td>";
  70. echo "<td align=\"right\">$total</td>";
  71. echo "</tr>";
  72.  
  73. //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
  74. echo "<tr>";
  75. echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
  76. echo "</tr>";
  77. echo "</table>";
  78.  
  79.  
  80.  
  81. }else{
  82. //otherwise tell the user they have no items in their cart
  83. echo "You have no items in your shopping cart.";
  84.  
  85. }
  86.  
  87. function productExists($booksid) {
  88. //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
  89. $sql = sprintf("SELECT * FROM books WHERE booksid = %d;",
  90. $booksid);
  91.  
  92. return mysql_num_rows(mysql_query($sql)) > 0;
  93. }
  94.  
  95.  
  96. ?>

Products.php
  1. <?php require_once('../Connections/myDatabase.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  7.  
  8. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  9.  
  10. switch ($theType) {
  11. case "text":
  12. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  13. break;
  14. case "long":
  15. case "int":
  16. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  17. break;
  18. case "double":
  19. $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
  20. break;
  21. case "date":
  22. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  23. break;
  24. case "defined":
  25. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  26. break;
  27. }
  28. return $theValue;
  29. }
  30. }
  31.  
  32. mysql_select_db($database_myDatabase, $myDatabase);
  33. $query_title = "SELECT * FROM books WHERE books.booksid = 1";
  34. $title = mysql_query($query_title, $myDatabase) or die(mysql_error());
  35. $row_title = mysql_fetch_assoc($title);
  36. $totalRows_title = mysql_num_rows($title);
  37. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  38. <html xmlns="http://www.w3.org/1999/xhtml">
  39. <head>
  40. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  41. <title>Untitled Document</title>
  42. </head>
  43.  
  44. <body>
  45. <table border="1">
  46.  
  47. <tr>
  48. <td>&nbsp;</td>
  49. <td>Title</td>
  50. <td>Author</td>
  51. <td>Price</td>
  52. <td>Category</td>
  53. <td>&nbsp;</td>
  54. </tr>
  55. <tr>
  56. <td><?php echo $row_title['booksid']; ?></td>
  57. <td><?php echo $row_title['title']; ?></td>
  58. <td><?php echo $row_title['author']; ?></td>
  59. <td><?php echo $row_title['price']; ?></td>
  60. <td><?php echo $row_title['category']; ?></td>
  61. <td><a href="Cart.php?action=add&id=1">Add To Cart</a></td></tr></table>
  62.  
  63.  
  64. <a href="Cart.php">View Cart</a>
  65.  
  66. </body>
  67. </html>
  68. <?php
  69. mysql_free_result($title);
  70. ?>

Cart table
cartid
cartqty
cartsessionid

Products table
booksid
title
author
price
category

Any help is much appreciated!
Last edited by peter_budo; Jan 11th, 2009 at 2:28 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote