Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Start session first thing in script
- // Script Error Reporting
- error_reporting(E_ALL);
- ini_set('display_errors', '1');
- // Connect to the MySQL database
- //include "connectdb2.php";
- ?>
- <?php
- // Section 1 (if user attempts to add something to the cart from the product page)
- $error="";
- if(loggedin())
- {
- if (isset($_GET['id'])) {
- $pid = $_GET['id'];
- $sql = mysql_query("SELECT * FROM products WHERE id='$pid' LIMIT 1");
- while($row = mysql_fetch_array($sql))
- {
- $id=$row['id'];
- $product_name = $row["pname"];
- $price = $row["price"];
- $stock = $row["stock"];
- $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
- }
- if ( $stock == 0){
- $error .="<div id='error'>Product is not available</div>"; // if the products stock is 0
- }
- else
- {
- $wasFound = false;
- $i = 0;
- // If the cart session variable is not set or cart array is empty
- if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
- // RUN IF THE CART IS EMPTY OR NOT SET
- $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
- } else {
- // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
- foreach ($_SESSION["cart_array"] as $each_item) {
- $i++;
- while (list($key, $value) = each($each_item)) {
- if ($key == "item_id" && $value == $pid) {
- // That item is in cart already so let's adjust its quantity using array_splice()
- array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
- // inserting to database
- $wasFound = true;
- } // close if condition
- } // close while loop
- } // close foreach loop
- if ($wasFound == false) {
- array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
- }
- }
- header("location:user.php");
- exit();
- }
- }
- }
- else {
- header("location:login.php");
- exit();
- }
- ?>
- <?php
- //Section 2 (if user chooses to empty their shopping cart)
- if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
- unset($_SESSION["cart_array"]);
- }
- ?>
- <?php
- //Section 2 (if user chooses to empty their shopping cart)
- $success="";
- if (isset($_GET['paypal']) && $_GET['paypal'] == "emptycart") {
- unset($_SESSION["cart_array"]);
- $success="<div id='good'>Your Transaction has been succesfully Completed </div>";
- }
- ?>
- <?php
- // Section 3 (if user chooses to adjust item quantity)
- $error="";
- if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
- // execute some code
- $item_to_adjust = $_POST['item_to_adjust'];
- $quantity = $_POST['quantity'];
- $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
- if ($quantity >= 100) { $quantity = 99; }
- if ($quantity < 1) { $quantity = 1; }
- if ($quantity == "") { $quantity = 1; }
- $query = mysql_query("SELECT * FROM products WHERE id='$item_to_adjust'");
- while($row = mysql_fetch_array($query))
- {
- $stock = $row['stock'] ;
- }//close while
- if ($quantity>$stock)
- {
- $error .= '<center><div id="error">*Must be less than or equal to ' .$stock.'</div></center>';
- }
- else{
- $i = 0;
- foreach ($_SESSION["cart_array"] as $each_item) {
- $i++;
- while (list($key, $value) = each($each_item)) {
- if ($key == "item_id" && $value == $item_to_adjust) {
- // That item is in cart already so let's adjust its quantity using array_splice()
- array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
- } // close if condition
- } // close while loop
- } // close foreach loop
- }
- }
- ?>
- <?php
- //Section 4 (if user wants to remove all the item from cart)
- if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
- // Access the array and run code to remove that array index
- $key_to_remove = $_POST['index_to_remove'];
- if (count($_SESSION["cart_array"]) <= 1) {
- unset($_SESSION["cart_array"]);
- } else {
- unset($_SESSION["cart_array"]["$key_to_remove"]);
- sort($_SESSION["cart_array"]);
- }
- }
- ?>
- <?php
- // Section 5 render the cart for the user to view on the page
- $cart_Total="";
- $crt_Total="";
- $cartOutput = "";
- $cartTotal = "";
- $pp_checkout_btn = '';
- $product_id_array = '';
- $con="";
- if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
- $cartOutput = "<h3>Your shopping cart is empty</h3>";
- } else {
- // Start PayPal Checkout Button
- $pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
- <input type="hidden" name="cmd" value="_cart">
- <input type="hidden" name="upload" value="1">
- <input type="hidden" name="business" value="jaylord.salanap@yahoo.com">';
- // Start the For Each loop
- $i = 0;
- foreach ($_SESSION["cart_array"] as $each_item) {
- $item_id = $each_item['item_id'];
- $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
- while ($row = mysql_fetch_array($sql)) {
- $id = $row["id"];
- $product_name = $row["pname"];
- $prod_desc = $row["description"];
- $price = $row["price"];
- $ext = $row["ext"];
- $date = date ("Y-m-d");
- }
- $pricetotal = $price * $each_item['quantity'];
- $cartTotal = $pricetotal + $cartTotal;
- setlocale(LC_MONETARY, "en_US");
- // Dynamic Checkout Btn Assembly
- $x = $i + 1;
- $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
- <input type="hidden" name="amount_' . $x . '" value="' . $price . '">
- <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> ';
- // Create the product array variable
- $cquantity = $each_item['quantity'];
- $product_id_array .= "$item_id-".$each_item['quantity'].",";
- // Dynamic table row assembly
- $cartOutput .= "<tr>";
- $cartOutput .= '<td><img src="uploads/thumbs/'.$id.'.'.$ext.'" width="50" height="50"/></td></font>';
- $cartOutput .= '<td><font color="#fff">' . $product_name. '</font></td>';
- $cartOutput .= '<td><form action="user.php" method="post"></font>
- <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>
- <font color="#fff"><input name="adjustBtn' . $item_id . '" type="submit" id="post3" value="change" /></font>
- <font color="#fff"><input name="item_to_adjust" type="hidden" value="' . $item_id . '" /></font>
- </form></td>';
- $cartOutput .= '<td><font color="#fff">PHP' . $price . ' </font></td>';
- $cartOutput .= '<td><font color="#fff">PHP' . $pricetotal . '</td>';
- $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>';
- $cartOutput .= '</tr>';
- $i++;
- }
- setlocale(LC_MONETARY, "en_US");
- //$cartTotal = money_format("%10.2n", $cartTotal);
- $cart_Total = '<div id="cart_total"><font color="#fff">Cart Total : PHP'.$cartTotal. '</font></div>';
- $crt_Total = '<font color="#fff"><div id="cart_total"><font color="#fff">PHP '.$cartTotal. '</font></div>';
- // Finish the Paypal Checkout Btn
- $pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
- <input type="hidden" name="notify_url" value="http://www.nhiglobalsolutions.saltandlightamaer.com/my_ipn.php">
- <input type="hidden" name="notify_url" value="http://www.nhiglobalsolutions.saltandlightamaer.com/my_ipn.php">
- <input type="hidden" name="return" value="http://www.nhiglobalsolutions.saltandlightamaer.com/my_ipn.php">
- <input type="hidden" name="rm" value="2">
- <input type="hidden" name="cbt" value="Return to The Store">
- <input type="hidden" name="cancel_return" value="http://www.nhiglobalsolutions.saltandlightamaer.com/user.php">
- <input type="hidden" name="lc" value="PHP">
- <input type="hidden" name="currency_code" value="PHP">
- <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!">
- </form>';
- $con = '<a href="productclient.php">Continue shopping</a>';
- }
- ?>
- <script>
- function confirmDelete(link) {
- if (confirm("Are you sure you want to remove?")) {
- doAjax(link.href, "POST"); // doAjax needs to send the "confirm" field
- }
- return false;
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement