Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.68 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6.   <meta name="Author" content="Richard Bowen">
  7.   <link rel="stylesheet" href="css/style.css" type="text/css" />
  8.   <title>Homemade Jams e-Retail prototype - Order Summary</title>
  9. </head>
  10.  
  11. <body>
  12.  
  13.   <h2>Order Summary</h2>
  14.   <hr>
  15.  
  16.   <form method="post" action="save_purchases.php">
  17.  
  18.   <?php
  19.   //error_reporting(0);
  20.  
  21.   //Establish connection to SQL server in order to take information from the database
  22.   require "connect.php";
  23.  
  24.   // In this program we get the customer name sent from the form and then
  25.   // for each of the items chosen we match with the database, keep a runnning
  26.   // total of the cost and confirm the details back to the visitor
  27.  
  28.       //Define Customer email as entered on order summary page
  29.         $custemail = $_POST['emailgiven'];
  30.  
  31.       echo "<br>custemail: ";
  32.       echo "$custemail";
  33.  
  34.       //Times looped
  35.       $numberofitemspurchased = 0;
  36.  
  37.       //Define values for quantities as entered on previous page
  38.       //Come back at the end > change these 10 manual ones to $_POST[]
  39.       $QTY = array($_POST['q1'], $_POST['q2'], $_POST['q3'], $_POST['q4'], $_POST['q5'], $_POST['q6'], $_POST['q7'], $_POST['q8'], $_POST['q9'], $_POST['q10']);
  40.       //$CHK = array($_POST[0], $_POST['tk2'], $_POST['tk3']);
  41.  
  42.  
  43.       echo "<br>QTY1";
  44.       echo "$QTY[0]";
  45.       echo "<br>QTY2";
  46.       echo "$QTY[1]";
  47.       echo "<br>QTY3";
  48.       echo "$QTY[2]";
  49.  
  50.  
  51.       //Define subtotal array
  52.       $subtotal = array();
  53.  
  54.         //Define query
  55.       //Selecting all item queries from tables "items" and "jamtypes"
  56.       $query = "SELECT i.*, jt.* FROM items i, jamtypes jt WHERE i.JamTypeID = jt.JamTypeID";
  57.         //$query = "SELECT ItemID, ItemDescription, ItemName, ItemPrice, ItemWeight FROM `items`";
  58.  
  59.         $result = $db->query($query);
  60.  
  61.         echo "<p>You selected:</p>";
  62.  
  63.       //Defining variable total before the loop in case there are no items and loop never gets executed
  64.         $total=0.00;
  65.  
  66.         while ($row = mysqli_fetch_array ($result))
  67.         {
  68.             // this extracts data out of database
  69.             extract ($row);
  70.  
  71.             // this reads through the data sent by the form
  72.             foreach ($_POST as $varname => $varvalue)
  73.         {
  74.  
  75.             //Define temporary variable and adjust to align with variable array
  76.             //$i is the item number the user bought on previous page
  77.             $i = $ItemID - 1;
  78.  
  79.             //VALIDATION - I want to ignore Quantities that aren't whole integers above zero
  80.             //To avoid unwanted listings when listing back to the user
  81.             //First I'm only continuing if the data that was submitted on the previous form in the quantity field is an integer
  82.             if (ctype_digit($QTY[$i]))
  83.             {
  84.  
  85.               //Now we know it's an integer...
  86.               //I'm checking to make the value is not 0 in order to progress through the code
  87.               if (!empty($QTY[$i]))
  88.               {
  89.  
  90.                 //Match form selection with database
  91.                         if ($ItemID==$varname)
  92.                         {
  93.  
  94.                             //echo "<a href=$item_img_url>click here for image</a>
  95.                   echo "<br>item id: ", $ItemID, "<br>";
  96.                   echo "<td><img src='", $ItemImage, "' alt='", $ItemName, "' height='128' width='128'></td>";
  97.                   echo "<br>";
  98.                   echo $QTY[$i], "x ", $ItemName, " ", $JamType, " at <i>£", $ItemPrice, "0</i> each";
  99.                   echo "<br>";
  100.  
  101.                   //Format number to 2 decimal places (proper currency formatting)
  102.                   $subtotal[$i]= $QTY[$i] * $ItemPrice;
  103.                   $subtotal[$i] = number_format($subtotal[$i], 2);
  104.                   echo " <b>£", $subtotal[$i], "</b><br><br>";
  105.  
  106.                   //$confirmeditem = array();
  107.                   //$confirmedqty = array();
  108.  
  109.                   //$confirmeditem[$timeslooped] = $ItemID;
  110.                   //$confirmedqty[$timeslooped] = $QTY[$i];
  111.  
  112.  
  113.                   //$ItemID
  114.  
  115.                             echo "<input type='hidden' name='itm$numberofitemspurchased' value = $ItemID>";
  116.                   echo "<input type='hidden' name='qty$numberofitemspurchased' value = $QTY[$i]>";
  117.                             echo "<input type='hidden' name='emailgiven' value = $custemail>";
  118.  
  119.                             $total=$total+$subtotal[$i];
  120.  
  121.                   //echo "<br>timeslooped: ";
  122.                   //$timeslooped++;
  123.  
  124.                   //echo "<br>timeslooped: ";
  125.                   //++$timeslooped;
  126.  
  127.                   $numberofitemspurchased = $numberofitemspurchased + 1;
  128.  
  129.  
  130.                 }
  131.  
  132.               }
  133.  
  134.             }
  135.  
  136.             }
  137.  
  138.         }
  139.  
  140.       echo "<br><b>Total sum: £";
  141.       $total = number_format($total, 2);
  142.         echo $total;
  143.  
  144.       //Calculate and Display VAT @ 20%
  145.       $VAT = $total * 0.2;
  146.       $VAT = number_format($VAT, 2);
  147.       echo "<br>Plus 20% VAT: £", $VAT, "</b>";
  148.  
  149.       //Calculate & Display Grand Total
  150.       $GRAND = $total + $VAT;
  151.       $GRAND = number_format($GRAND, 2);
  152.       echo "<br><h3>Grand Total: ";
  153.       echo "£", $GRAND, "</h3>";
  154.  
  155.       echo "<input type='hidden' name='arraylength' value=$numberofitemspurchased>";
  156.  
  157.   // mysqli_close($connection);
  158.   ?>
  159.  
  160.   <br>
  161.   <br>
  162.  
  163.   <!--  This creates a button on the form which, when clicked causes the action, above, to happen -->
  164.   <input type="submit" value="Confirm Purchases">
  165.  
  166.   </form>
  167.  
  168. </body>
  169.  
  170. <footer>
  171.     <hr>
  172.     <p>
  173.     Check valid HTML 5:<br>
  174.     <a href="http://validator.w3.org/check?uri=referer">
  175.     <img src="images/check-valid-html5.png" alt="Valid HTML 5" height="36" width="94"></a>
  176.     </p>
  177.  
  178.   <a href="home.php">Return to home page</a><br>
  179.     Richard Bowen 13040615 eBusiness &copy; 2017
  180.  
  181. </footer>
  182.  
  183. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement