Guest User

Untitled

a guest
May 24th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. cart.php code:
  2.  
  3. <?php session_start();
  4. //starting the session
  5. include("adminarea/includes/DBcon.php");
  6. include ("functions/php1.php");
  7. include ("header.php");
  8. require 'obj.php';
  9.  
  10. ?>
  11.  
  12. <link rel="stylesheet" href = "styles/styling.css" media="all" />
  13.  
  14. <body>
  15.  
  16. <?php
  17. //Fetches information from the database and displays them with the help of obj.php
  18. if(isset($_GET['pID'])){
  19. $res = mysqli_query($connection, 'select * from product where pID='.$_GET['pID']);
  20. $prod = mysqli_fetch_object($res);
  21. $obj = new obj();
  22. $obj->pID = $prod->pID;
  23. $obj->pName = $prod->pName;
  24. $obj->pPrice = $prod->pPrice;
  25. $obj->qty = 1;
  26.  
  27. //to check if products exists in cart or not
  28. $index = -1;
  29. $cart = unserialize(serialize($_SESSION['cart']));
  30. for($i=0;$i<count($cart);$i++)
  31. if($cart[$i]->pID==$_GET['pID'])
  32. {
  33. $index = $i;
  34. break;
  35. }
  36.  
  37. if($index==-1)
  38. $_SESSION['cart'][] = $obj;
  39. else{
  40. $cart[$index]->qty++;
  41. $_SESSION['cart']=$cart;
  42. }
  43. echo "
  44. <script>
  45. window.open('cart.php','_self')
  46. </script>
  47. ";
  48.  
  49.  
  50. $_SESSION['pID'] = $_POST['pID'];
  51. $_SESSION['pName'] = $_POST['pName'];
  52. $_SESSION['pPrice'] = $_POST['pPrice'];
  53. $_SESSION['qty'] = $_POST['qty'];
  54.  
  55. }
  56.  
  57.  
  58. if(!(isset($_SESSION['cart']))){
  59. echo "
  60. <script>
  61. alert('Shopping cart is empty!')
  62. window.location.href='index.php';
  63. </script>
  64. ";
  65. }
  66.  
  67. //if statement to delete the chosen product inside the cart
  68. if(isset($_GET['index']))
  69. {
  70. $cart = unserialize(serialize($_SESSION['cart']));
  71. unset ($cart[$_GET['index']]);
  72. $cart = array_values($cart);
  73. $_SESSION['cart'] = $cart;
  74. }
  75.  
  76.  
  77. ?>
  78.  
  79. <!-- This is to display the shopping cart table-->
  80.  
  81. <table cellpadding="5" cellspacing="4" border ="9" align="center" width="100%" border="9" bgcolor="darkred">
  82. <td style="color:#FFF" colspan="10" align="center"><h2><u><i>Shopping Cart:</i></u></h2>
  83. <tr>
  84. <th style="color:#FFF">Option</th>
  85. <th style="color:#FFF">Id</th>
  86. <th style="color:#FFF">Name</th>
  87. <th style="color:#FFF">Price</th>
  88. <th style="color:#FFF">Quantity</th>
  89. <th style="color:#FFF">SubTotal</th>
  90. </tr>
  91. <?php
  92.  
  93. $cart = unserialize(serialize($_SESSION['cart']));
  94. $s = 0;
  95. $index = 0;
  96. for($i=0; $i<count($cart); $i++){
  97. $s += $cart[$i] ->pPrice * $cart[$i]->qty;
  98.  
  99. ?>
  100. <tr>
  101. <td>
  102. <div class="shopcart">
  103.  
  104. <input id="input" type="submit" name="ctable"/><a href="cart.php?index=<?php echo $index;?>" onClick="return confirm('Please confirm deletion of the chosen product.')">Remove</a></input></td>
  105.  
  106. <td style="color:#FFF" align="center"><?php echo $cart[$i] ->pID; ?> </td>
  107. <td style="color:#FFF" align="center"><?php echo $cart[$i] ->pName; ?></td>
  108. <td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice; ?></td>
  109. <td style="color:#FFF" align="center"><?php echo $cart[$i] ->qty; ?></td>
  110. <td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice * $cart[$i]->qty;?></td>
  111. </tr>
  112.  
  113. <?php }
  114. $index++;
  115.  
  116.  
  117. ?>
  118. <tr>
  119. <td colspan="5" align="right" style="color:#FFF">Total</td>
  120. <td style="color:#FFF" align="center">€<?php echo $s;?></td>
  121. </tr>
  122. </table>
  123.  
  124. <br>
  125. <a id="a" style="margin-left: 10px;" href="products.php"> Go back</a><br><br>
  126.  
  127. <div id="checkout">
  128.  
  129. <form id="checkout" method="post" action="checkout.php">
  130. <input id="input" type="submit" name="check" value="Checkout" style="background-color:gray; width:200px; margin-right: 10px;">
  131. </div>
  132.  
  133. </div>
  134.  
  135.  
  136. <?php include("footer.php") ?>
  137.  
  138. </body>
  139. </html>
  140.  
  141. checkout.php code:
  142.  
  143. <?php session_start();
  144.  
  145. require 'obj.php';
  146. include("adminarea/includes/DBcon.php");
  147.  
  148.  
  149. $to = "techologyy@gmail.com";//direction
  150.  
  151. $subject = "Purchase Details:";
  152. $message = $_SESSION['pID'];
  153. $message .= $_SESSION['pName']."\r\n";
  154. $message .= $_SESSION['pPrice']."\r\n";
  155. $message .= $_SESSION['qty']."\r\n";
  156. $message .= "Checkout by the user has been successful :) '$message'";
  157.  
  158. $headers = 'From: techologyy@gmail.com' . "\r\n"; //from
  159.  
  160.  
  161.  
  162. //mail paramter with correct order
  163. mail($to, $subject, $message, $headers);
  164.  
  165.  
  166. //echo to display alert
  167. echo "
  168. <script>
  169. alert('The checkout has been done successfully! Thank you')
  170. window.location.href='index.php';
  171. </script>
  172. "; //returns the user back to homepage
  173. ?>
Add Comment
Please, Sign In to add comment