Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 KB | None | 0 0
  1. <?php
  2. // Start session first thing in script
  3. // Script Error Reporting
  4. error_reporting(E_ALL);
  5. ini_set('display_errors', '1');
  6. // Connect to the MySQL database
  7. //include "connectiondb.php";
  8. ?>
  9. <?php
  10. // Section 1 (if user attempts to add something to the cart from the product page)
  11. $error="";
  12. if(loggedin())
  13. {
  14. if (isset($_GET['id'])) {
  15. $pid = $_GET['id'];
  16.  
  17. $sql = mysql_query("SELECT * FROM products WHERE id='$pid' LIMIT 1");
  18. while($row = mysql_fetch_array($sql))
  19. {
  20. $id=$row['id'];
  21. $product_name = $row["pname"];
  22. $price = $row["price"];
  23. $stock = $row["stock"];
  24. $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
  25. }
  26. if ( $stock == 0){
  27. $error .="<div id='error'>Product is not available</div>"; // if the products stock is 0
  28. }
  29. else
  30. {
  31. $wasFound = false;
  32. $i = 0;
  33. // If the cart session variable is not set or cart array is empty
  34. if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
  35. // RUN IF THE CART IS EMPTY OR NOT SET
  36. $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
  37. } else {
  38. // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
  39. foreach ($_SESSION["cart_array"] as $each_item) {
  40. $i++;
  41. while (list($key, $value) = each($each_item)) {
  42. if ($key == "item_id" && $value == $pid) {
  43. // That item is in cart already so let's adjust its quantity using array_splice()
  44. array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
  45. // inserting to database
  46. $wasFound = true;
  47. } // close if condition
  48. } // close while loop
  49. } // close foreach loop
  50. if ($wasFound == false) {
  51. array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
  52. }
  53. }
  54. header("location:user.php");
  55. exit();
  56. }
  57. }
  58. }
  59. else {
  60. header("location:login.php");
  61. exit();
  62. }
  63. ?>
  64. <?php
  65.  
  66. //Section 2 (if user chooses to empty their shopping cart)
  67. if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
  68. unset($_SESSION["cart_array"]);
  69. }
  70. ?>
  71. <?php
  72. //Section 2 (if user chooses to empty their shopping cart)
  73. $success="";
  74. if (isset($_GET['paypal']) && $_GET['paypal'] == "emptycart") {
  75. unset($_SESSION["cart_array"]);
  76. $success="<div id='good'>Your Transaction has been succesfully Completed </div>";
  77. }
  78. ?>
  79. <?php
  80. // Section 3 (if user chooses to adjust item quantity)
  81. $error="";
  82. if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
  83. // execute some code
  84. $item_to_adjust = $_POST['item_to_adjust'];
  85. $quantity = $_POST['quantity'];
  86. $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
  87. if ($quantity >= 100) { $quantity = 99; }
  88. if ($quantity < 1) { $quantity = 1; }
  89. if ($quantity == "") { $quantity = 1; }
  90.  
  91. $query = mysql_query("SELECT * FROM products WHERE id='$item_to_adjust'");
  92. while($row = mysql_fetch_array($query))
  93. {
  94. $stock = $row['stock'] ;
  95. }//close while
  96. if ($quantity>$stock)
  97. {
  98. $error .= '<center><div id="error">*Must be less than or equal to ' .$stock.'</div></center>';
  99. }
  100. else{
  101. $i = 0;
  102. foreach ($_SESSION["cart_array"] as $each_item) {
  103. $i++;
  104. while (list($key, $value) = each($each_item)) {
  105. if ($key == "item_id" && $value == $item_to_adjust) {
  106. // That item is in cart already so let's adjust its quantity using array_splice()
  107.  
  108. array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
  109.  
  110. } // close if condition
  111. } // close while loop
  112. } // close foreach loop
  113. }
  114. }
  115. ?>
  116. <?php
  117.  
  118. //Section 4 (if user wants to remove all the item from cart)
  119. if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
  120. // Access the array and run code to remove that array index
  121. $key_to_remove = $_POST['index_to_remove'];
  122. if (count($_SESSION["cart_array"]) <= 1) {
  123. unset($_SESSION["cart_array"]);
  124. } else {
  125. unset($_SESSION["cart_array"]["$key_to_remove"]);
  126. sort($_SESSION["cart_array"]);
  127. }
  128. }
  129. ?>
  130. <?php
  131. // Section 5 render the cart for the user to view on the page
  132. $cart_Total="";
  133. $crt_Total="";
  134. $cartOutput = "";
  135. $cartTotal = "";
  136. $pp_checkout_btn = '';
  137. $product_id_array = '';
  138. $con="";
  139.  
  140. if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
  141. $cartOutput = "<h3>Your shopping cart is empty</h3>";
  142.  
  143. } else {
  144. // Start PayPal Checkout Button
  145. $pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
  146. <input type="hidden" name="cmd" value="_cart">
  147. <input type="hidden" name="upload" value="1">
  148. <input type="hidden" name="business" value="jaylord.salanap@yahoo.com">';
  149. // Start the For Each loop
  150. $i = 0;
  151. foreach ($_SESSION["cart_array"] as $each_item) {
  152. $item_id = $each_item['item_id'];
  153. $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
  154. while ($row = mysql_fetch_array($sql)) {
  155. $id = $row["id"];
  156. $product_name = $row["pname"];
  157. $prod_desc = $row["description"];
  158. $price = $row["price"];
  159. $ext = $row["ext"];
  160. $date = date ("Y-m-d");
  161. }
  162. $pricetotal = $price * $each_item['quantity'];
  163. $cartTotal = $pricetotal + $cartTotal;
  164. setlocale(LC_MONETARY, "en_US");
  165. // Dynamic Checkout Btn Assembly
  166. $x = $i + 1;
  167. $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
  168. <input type="hidden" name="amount_' . $x . '" value="' . $price . '">
  169. <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> ';
  170. // Create the product array variable
  171. $cquantity = $each_item['quantity'];
  172. $product_id_array .= "$item_id-".$each_item['quantity'].",";
  173. // Dynamic table row assembly
  174. $cartOutput .= "<tr>";
  175. $cartOutput .= '<td><img src="uploads/thumbs/'.$id.'.'.$ext.'" width="50" height="50"/></td></font>';
  176. $cartOutput .= '<td><font color="#fff">' . $product_name. '</font></td>';
  177. $cartOutput .= '<td><form action="user.php" method="post"></font>
  178. <font color="#fff"><input name="quantity" type="number" id="post4" value="' . $each_item['quantity'] . '" width="50" maxlength="5" width="10" min="1" max="99" /></font>
  179. <font color="#fff"><input name="adjustBtn' . $item_id . '" type="submit" id="post3" value="change" /></font>
  180. <font color="#fff"><input name="item_to_adjust" type="hidden" value="' . $item_id . '" /></font>
  181. </form></td>';
  182. $cartOutput .= '<td><font color="#fff">PHP' . $price . ' </font></td>';
  183. $cartOutput .= '<td><font color="#fff">PHP' . $pricetotal . '</td>';
  184. $cartOutput .= '<td><form action="user.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" id="post2" value="Remove from cart" onclick="return confirmDelete(this);" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
  185. $cartOutput .= '</tr>';
  186. $i++;
  187. }
  188. setlocale(LC_MONETARY, "en_US");
  189. //$cartTotal = money_format("%10.2n", $cartTotal);
  190. $cart_Total = '<div id="cart_total"><font color="#fff">Cart Total : PHP'.$cartTotal. '</font></div>';
  191. $crt_Total = '<font color="#fff"><div id="cart_total"><font color="#fff">PHP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$cartTotal. '</font></div>';
  192. // Finish the Paypal Checkout Btn
  193. $pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
  194. <input type="hidden" name="notify_url" value="http://www.nhiglobalsolutions.saltandlightamaer.com/my_ipn.php">
  195. <input type="hidden" name="notify_url" value="http://www.nhiglobalsolutions.saltandlightamaer.com/my_ipn.php">
  196. <input type="hidden" name="return" value="http://www.nhiglobalsolutions.saltandlightamaer.com/my_ipn.php">
  197. <input type="hidden" name="rm" value="2">
  198. <input type="hidden" name="cbt" value="Return to The Store">
  199. <input type="hidden" name="cancel_return" value="http://www.nhiglobalsolutions.saltandlightamaer.com/user.php">
  200. <input type="hidden" name="lc" value="PHP">
  201. <input type="hidden" name="currency_code" value="PHP">
  202. <input type="image" title="Click here to go to Paypal" img src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
  203. </form>';
  204. $con = '<a href="productclient.php">Continue shopping</a>';
  205. }
  206. ?>
  207.  
  208. <script>
  209.  
  210. function confirmDelete(link) {
  211. if (confirm("Are you sure you want to remove?")) {
  212. doAjax(link.href, "POST"); // doAjax needs to send the "confirm" field
  213. }
  214. return false;
  215. }
  216.  
  217. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement