bolusho Posted June 19, 2013 Share Posted June 19, 2013 i want to design a shopping cart without using a database, presently, i have a code but users have to input the price of their product, i want to change that, so that the prices would be determined by the the quantity. below is the form i've created: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[url=http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd[/url]"> <html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]" xml:lang="en"> <head> <title>My Sessions </title> </head> <body> <form method="post" action="order.php"> <p> Mouse: </br> <input type="text" name="mouse" size="3" <?php if ( $_POST['mouse'] ) { print ' value="' . $_POST['mouse'] . '"'; } ?> > at $10.99 each </p> <p> Dell Laptop: </br> <input type="text" name="dell" size="3" <?php if ( $_POST['dell'] ) { print ' value="' . $_POST['dell'] . '"'; } ?> > at $20.99 each </p> <p> Ipad: </br> <input type="text" name="ipad" size="3" <?php if ( $_POST['ipad'] ) { print ' value="' . $_POST['ipad'] . '"'; } ?> > at $30.95 each </p> <p> Iphone: </br> <input type="text" name="iphone" size="3" <?php if ( $_POST['iphone'] ) { print ' value="' . $_POST['iphone'] . '"'; } ?> > at $40.95 each </p> <p> Blackberry: </br> <input type="text" name="blackberry" size="3" <?php if ( $_POST['blackberry'] ) { print ' value="' . $_POST['blackberry'] . '"'; } ?> > at $50.95 each </p> <p> <button type="submit">Add to cart</button> </p> </form> <form action="clearcart.php" method="post"> <p><input type="submit" value="Clear Cart" /></p> </form> <hr /> <p><a href="cart.php">Go to my cart</a></p> </body> </html> the form above is a new form, my problem is how do i fix in prices into my already written shopping cart code which is below. i'd appreciate it if any one can help with a new code that would accept the and fix prices, quantity and products into a cart. below is my already written code: <?php #We used the superglobal $_GET here /*if (!($_GET['name'] && $_GET['email'] && $_GET['password'])) { #with the header() function, no output can come before it. #echo "Please make sure you've filled in all required information."; $query_string = $_SERVER['QUERY_STRING']; header("Location: ".$url); exit(); } extract($_GET, EXTR_PREFIX_SAME, "get");*/ echo date("F d, Y h:i a"); /*function email_mesg($data_array, $template_file) { #get template contents, and replace variables with data $email_message = str_replace("#name#", $data_array['name'], $email_message); $email_message = str_replace("#email#", $data_array['email'], $email_message); email_mesg($_GET, $_SERVER['DOCUMENT_ROOT']."/myemail_template.txt");*/ //Start the session session_start(); $customer_name = $_SESSION['name']; if (!($customer_name)) { $customer_name = $_GET['name']; } $customer_email = $_SESSION['email']; if (!($customer_email)) { $customer_email = $_GET['email']; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]"> <html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]" xml:lang="en"> <head> <title>Efe Shopping Cart</title> </head> <body> <form action="checkout.php" method="post"> <p><input type="submit" value="Checkout" /></p> </form> <h1>Efe Shopping Cart</h1> <?php $products = array ( array("productname" => "blackberry", "price" => "59.99", "description" => "Blackberry mobile phone"), array("productname" => "Mouse", "price" => "12.95", "description" => "Gaming mouse. 9200 DPI"), array("productname" => "iPad", "price" => "100.59", "description" => "The new apple ipad"), array("productname" => "Dell laptop", "price" => "250.00", "description" => "The best dell laptop available."), array("productname" => "iphone", "price" => "170.99", "description" => "The new iphone 5.") ); echo "<pre>"; print_r($products); echo "</pre>"; //If we have previously initiated a session in addtocart.php, the shopping cart is //defined as an associative array, $_SESSION['cart']. //check if the $_SESSION['cart'] variable has been defined, as it //won't be if the user has come straight to this page. //check if there are currently any items in the shopping cart. if (isset($_GET['remember'])) { #the customer wants us to remember him/her for next time $_SESSION['name'] = $customer_name; $_SESSION['email'] = $customer_email; } if (!isset($_SESSION['cart']) || (count($_SESSION['cart']) == 0)) { echo '<p>The cart is empty.</p>'; } else { echo '<table border="1"> <tr><th>Item</th><th>Unit price</th><th>No. of units</th><th>Subtotal</th></tr>'; $total = 0; //iterate over all the items in $_SESSION['cart']. //Each item represents a "row," or a single product to buy, in the shopping cart. foreach($_SESSION['cart'] as $item) //note that the format of the $item associative array is the same as the one //defined in addtocart.php, since the array has simply been passed to this page //via the $_SESSION variable. { echo "<tr><td>{$item['item']} </td><td>\${$item['unitprice']}</td><td>{$item['quantity']}</td> <td>$".($item['unitprice'] * $item['quantity'])."</td></tr>"; $total += ($item['unitprice'] * $item['quantity']); } echo '</table>'; echo "<p><strong>Grand total: </strong>\$$total</p>"; } $message = "thanks for your patronage, please feel free to check back with us."; //create a mail function to send messages $email_message = "Message Date: ".date("F d, Y h:i a")." Name: ".$name." Email: ".$email." message: ".$message; #construct the email headers $to = $_GET['email']; $from = "shosoo1@yahoo.com"; $email_subject = "Efe shopping cart"; mail($to, $email_subject, $message, "From: ".$from); ?> <hr /> <p><a href="buyintocart.php">Go buy something</a></p> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.