Jump to content
Larry Ullman's Book Forums

kerry

Members
  • Posts

    34
  • Joined

  • Last visited

Everything posted by kerry

  1. This is the book that I have used. In fact I have used both of Larry's book and the code is from the book. It is just that I want to edit Larry's code to add this drop down box. If I set the quantity as one as in Larry's code it comes into the cart ok. It is just using the drop down value that will not come into the cart.
  2. This is the cart script <?php session_start(); $_SESSION['cart']; $page_title = 'Add to cart'; if (isset ($_GET['pid']) && is_numeric($_GET['pid']) ) { $pid = (int) $_GET['pid']; if (isset($_SESSION['cart'][$pid])) { $_SESSION['cart'][$pid]['quantity']++; }else{ //new product require_once ('mysqli_connect.php'); $q="SELECT price, product_name, quantity_stock FROM product WHERE product_id=$pid"; $r=mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { list($price, $product_name) = mysqli_fetch_array ($r, MYSQLI_NUM); $_SESSION['cart'][$pid] = array ('quantity' => $quantity_stock, 'price' => $price, 'product_name' => $product_name); }else{ echo '<div align="center">Your basket has been updated</div>'; } } }else{ //no print ID echo '<div align="center"> Your basket has been updated</div>'; } if (isset($_POST['submitted'])) { foreach ($_POST['qty'] as $k => $v) { $pid = (int) $k; $qty = (int) $v; if ($qty ==0) { // Delete. unset ($_SESSION['cart'][$pid]); } elseif ($qty >0 ) { // Change quantity. $_SESSION['cart'][$pid]['quantity'] = $qty; } } // End of FOREACH. } // End of SUBMITTED IF.
  3. It is the quantity set by the drop down list (quantity_stock) I am (i think) accessing it in the cart script by $_SESSION['cart'][$pid] = array ('quantity' => $quantity_stock); But think that is where I am having the problem. The quantity is unlimited so it does not matter about retrieveing it from the database
  4. oops, yes had forgot the closing select. Here is the code from my cart page. No quantity is in the qty box $q="SELECT price, product_name, quantity_stock FROM product WHERE product_id=$pid"; $r=mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { list($price, $product_name) = mysqli_fetch_array ($r, MYSQLI_NUM); $_SESSION['cart'][$pid] = array ('quantity' => $quantity_stock, 'price' => $price, 'product_name' => $product_name); }else{ echo '<div align="center">Your basket has been updated</div>'; }
  5. I am wanting to add to the cart (on a different page) through a drop down list the quantity of 4 or 8. It displays 4 or 8, but does not display the addcart image to bring these quantities to the cart page. <?php $row = FALSE; // Assume nothing! if (isset($_GET['pid']) && is_numeric($_GET['pid']) ) { // Make sure there's a print ID! $pid = (int) $_GET['pid']; // Get the print info: require_once ('mysqli_connect.php'); $q="SELECT product_name, price, print_id, product_category, description, image_name, product_id, quantity_stock FROM product WHERE product_id= $pid"; $r = mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { // Good to go! $row = mysqli_fetch_array ($r, MYSQLI_ASSOC); echo '<table border="0">'; echo "<tr><td><img id='image1' border='0' img src=\"/orders/show_image.php?image=$pid&name=" . urlencode($row['image_name']) . "\" $image[3] alt=\"{$row['product_name']}\" width=\"400\" height=\"168\"/></td> <td><p class=head_blue><b>{$row['product_name']}</b> <p class=desc><b>{$row['description']}<br/></p> <p class=desc><b>£{$row['price']}</b></p> <select name='quantity_stock'> <option value='4'>4</option> <option value='8'>8</option> <a href=\"basket.php?pid=$pid\"><img src='images/addcart.gif' width='83' height='23' border='0'/></a></td></tr></table>"; if ($row['quantity_stock'] >=1) { } else { echo '<p> NOT IN STOCK</p></td></tr></table>';}}}?>
  6. Hi Larry I have tried this, but it does not matter how many items I put in my basket it only says that there is 1 item in my cart <?php session_start(); function writeShoppingCart() { $items = 0; foreach ($_SESSION['cart'] as $item) { $items += $item['quantity']; } return '<p>You have <a href="basket.php"> '.count($items).' in your shopping cart</a></p>'; } echo writeShoppingCart();?>
  7. I have created the shopping cart as in the book: I am tring to show on each page how many items are in the cart. But, it only shows 1 item even if there are 5 items in the cart. The code I am using is: <?php session_start(); function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>You have no items in your shopping cart</p>'; } else { $items = explode(',', $cart); return '<p>You have <a href="basket.php"> '.count($items).' in your shopping cart</a></p>'; } } echo writeShoppingCart();?> I guess it is not bringing an array from the cart, but dont know how to show this in the above. A snippet from my cart is: session_start(); $_SESSION['cart']; $page_title = 'Add to cart'; if (isset ($_GET['pid']) && is_numeric($_GET['pid']) ) { $pid = (int) $_GET['pid']; if (isset($_SESSION['cart'][$pid])) { $_SESSION['cart'][$pid]['quantity']++; }else{ //new product require_once ('../mysqli_connect.php'); $q="SELECT price, product_name FROM product WHERE product_id=$pid"; $r=mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { list($price, $product_name) = mysqli_fetch_array ($r, MYSQLI_NUM); $_SESSION['cart'][$pid] = array ('quantity' => 1, 'price' => $price, 'product_name' => $product_name); }else{ echo '<div align="center">Your basket has been updated</div>'; } } }else{ echo '<div align="center"> Your basket has been updated</div>'; } if (isset($_POST['submitted'])) { foreach ($_POST['qty'] as $k => $v) { $pid = (int) $k; $qty = (int) $v; if ($qty ==0) { unset ($_SESSION['cart'][$pid]); } elseif ($qty >0 ) { $_SESSION['cart'][$pid]['quantity'] = $qty; } } } if (!empty($_SESSION['cart'])) { require_once ('../mysqli_connect.php'); $q="SELECT product_id, description, price, product_name FROM product WHERE product_id IN ("; foreach ($_SESSION['cart'] as $pid => $value) {
  8. I took the code from your article (peachpit) and then used the book with the form. All works except for getting existing variables. It starts from view.php which has the product on. The product_id is sent to the next page reviews.php through the link http://www.xx/reviews.php?pid=101 And, yes this page is sent back to itself once added to the database. I therefore presume: $product_id = (int) $_GET['product_id']; // is correct as I am getting the product_id It does not matter if I put value="<?php echo $_GET['product_id']; ?>"/> or value="<?php echo $_POST['product_id']; ?>"/> Neither adds to the database.
  9. I have had a look at your book and guess this is what you mean. I must have done something wrong as it still doesn't put the product_id in the database: $product_id = (int) $_GET['product_id']; $rating = mysqli_real_escape_string($dbc, $_POST['rating']); $review = mysqli_real_escape_string($dbc, $_POST['review']); $q='INSERT INTO review (product_id, rating, review) VALUES (?,?,?)'; $stmt=mysqli_prepare($dbc,$q); mysqli_stmt_bind_param($stmt, 'iss', $product_id, $rating, $review); mysqli_stmt_execute($stmt); if (mysqli_stmt_affected_rows($stmt)==1){ echo'<p>Your review has been added </p>'; }else{ echo'<p>error</p>'; } } ?> <form action="reviews.php" method="post" accept-charset="utf-8"> <fieldset><legend>Review this product</legend> <p><label for="rating">Rating</label><input type="radio" name="rating" value="5" />5 <input type="radio" name="rating" value="4" />4 <input type="radio" name="rating" value="3" />3 <input type="radio" name="rating" value="2" />2 <input type="radio" name="rating" value="1" />1</p> <p><Label for="review">Review</Label><textarea name="review" rows="8" cols="40" value="<?php echo $_POST['review'];?>"/></textarea></p> <p><input type="submit" value="submit review" ></p> <input type="hidden" name="product_id" id="product_id" value="<?php echo $_GET['product_id']; ?>"/> </fieldset> </form>
  10. Hi Larry I have already tried that, but that still did not put the product_id in the database.
  11. Hi. Thanks for the reply. I have solved the problem, but I cannot get the product_id to insert into the database: The $pid (product_id) is carried from the page required. http://www.xx/reviews.php?pid=101 but whatever I try it does not put the product_id in the review database. <?php require_once ('../mysqli_connect.php'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $pid = $_GET ['pid']; $product_id = mysqli_real_escape_string($dbc, $_POST['product_id=$pid']); $review = mysqli_real_escape_string($dbc, $_POST['review']); $q='INSERT INTO review (product_id, review) VALUES (?,?)'; $stmt=mysqli_prepare($dbc,$q); mysqli_stmt_bind_param($stmt, 'is', $product_id, $review); mysqli_stmt_execute($stmt); if (mysqli_stmt_affected_rows($stmt)==1){ echo'<p>Your review has been added </p>'; }else{ echo'<p>error</p>'; } } ?> <form action="reviews.php" method="post" accept-charset="utf-8"> <fieldset><legend>Review this product</legend> > <p><Label for="review">Review</Label><textarea name="review" rows="8" cols="40" value="<?php echo $_POST['review'];?>"/></textarea></p> <p><input type="submit" value="submit review" ></p> <input type="hidden" name="product_id" id="product_id" value="<?php echo $_POST['product_id'];?>"/> </fieldset> </form>
  12. I am editing the script in the book (17.1 to add a print) to create a product review. I have created a new table (review) But all I get is an error message, so I am obviously missing something. Any advice on creating a producct review on the site or advice on where I have gone wrong would be appreciated <?php require_once ('../mysqli_connect.php'); if (isset($_POST['submitted'])) { $errors = array(); if (!empty($_POST['product_id'])) { $pi = trim($_POST['product_id']); } else { $errors[] = 'Please enter the product id'; } $ci = (!empty($_POST['reviewer_name'])) ? trim($_POST['reviewer_name']) : NULL; $re = (!empty($_POST['review'])) ? trim($_POST['review']) : NULL; $q="INSERT INTO review (product_id, reviewer_name, review)VALUES(?, ?, ?)"; $stmt = mysqli_prepare($dbc, $q); mysqli_stmt_bind_param($stmt, 'sss', $pi, $ci, $re); mysqli_stmt_execute($stmt); // Check the results... if (mysqli_stmt_affected_rows($stmt) == 1) { echo '<p>Your review has been added</p>'; $_POST = array(); } else { // Error! echo '<p style="font-weight: bold; color: #C00"> error.</p>'; } mysqli_stmt_close($stmt); } ?> <p> </p> <form enctype="multipart/form-data" action="addreview.php" method="post"> <fieldset><legend>Add a Review</legend> <p><b>Name:</b> <input type="text" name="product_name" size="30" maxlength="60" value="<?php if (isset($_POST['reviewer_name'])) echo htmlspecialchars($_POST['reviewer_name']); ?>" /> You can use a nickname</p> <p><b>Review:</b> <textarea name="description" cols="40" rows="5"><?php if (isset($_POST['review'])) echo $_POST['review']; ?></textarea> </p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit" /></div> <input type="hidden" name="submitted" value="TRUE" /> </form> MySQL client version: 5.0.77
  13. Hi Thanks for the reply. Its a good idea to indicate "no stock", "Out of stock - back soon" Any ideas on how I could add to the code above to show no stock when 0?
  14. I created the following script so that from the website if the product has no stock it does not show. Which is fine. <?php # Script to show products $page_title = 'Browse the Products'; require_once ('../mysqli_connect.php'); // shows the product if over 0 in stock $q="SELECT * FROM product WHERE product_category='fish' AND quantity_stock>'0'"; if (isset($_GET['pid']) && is_numeric($_GET['pid']) ) { $pid = (int) $_GET['pid']; if ($pid > 0) { // Overwrite the query: } } But, search engines are showing the product if it goes from outside of our websites. It is picking up the pid=(the product number) http://www.websiteaddress.co.uk/viewproduct.php?pid=329 Any ideas of a way around this? (except for deleting the product) - PHP version 5.3.5 - MYSQLI 5.0.7
  15. The product_functions.inc.php where the last statement is: <?php function get_stock_status($quantity_stock) { if ($quantity_stock > 5) { // Plenty! return 'In Stock'; } elseif ($quantity_stock > 0) { // Low! return 'Low Stock'; } else { // Out! return 'Currently Out of Stock'; } } // End of get_stock_status() function. The value of $row['quantity_stock'] is 14 so should show plenty!
  16. I created my site with PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide and then used the effortless e-commerce book to add extras shown in the book. One of these is to indicate availability. But.. It only displays the last statement from the product_functions.inc.php Below is my code that links to this: <?php include ('includes/product_functions.inc.php'); $row = FALSE; if (isset($_GET['pid']) && is_numeric($_GET['pid']) ) { $pid = (int) $_GET['pid']; require_once ('../mysqli_connect.php'); $q="SELECT product_name, price, print_id, product_category, description, image_name, product_id FROM product WHERE product_id= $pid"; $r = mysqli_query ($dbc, $q); if (mysqli_num_rows($r) > 0) { $row = mysqli_fetch_array ($r, MYSQLI_ASSOC); ) { echo '<table width="90%" border="0" cellspacing="2" cellpadding="2">'."\r\n"; //Here is the link to the stock status as created in the product_functions. Mine is in quantity_stock in database as opposed to stock in the book. echo '<h3>'.get_stock_status($row['quantity_stock']).' </h3>'; echo '</table>'; } } if (!$row) { $page_title = 'Error'; echo '<div align="center">This page has been accessed in error!</div>'; } mysqli_close($dbc); ?> - PHP version 5.3.5 - MYSQLI 5.0.7
  17. Hi Larry Thanks for the reply. Do you have any plans to create the code for adding vouchers for people and for example free if ordering over £30? I think that this would be a good addition or extra for the book. I created a new table (discount_codes): num_vouchers int(11) expiry timestamp discount_amount float min_basket_cost float active tinyint(1) vouchercode varchar(25) ID int(11) MySQL client version: 5.0.77
  18. phpMyAdmin - 2.11.9.5. I am sure that the database is correct. The first bit of code is from the book (cart) and the second bit of code is for the voucher code, which I have tried to use from the cart code. When the voucher code is entered into the box, it displays Sorry, this was a limited edition voucher code, there are no more instances of that code left Below is the code that I tried to create for the voucher code. Thanks for advice on where I have gone wrong. [/size] [size=3]<?php ////// VOUCHER STARTS HERE /////////////////////// if (isset($_POST['submitted'])){ //require_once (MYSQL); //if( $voucherCode != '' ){ // we need the date, to see if the voucher has expired $cts = date('Y-m-d H:i:s'); $q = "SELECT *, if('{$cts}' > expiry, 1, 0)AS expired FROM discount_codes WHERE vouchercode='{$voucherCode}' LIMIT 1";[/size] [size=3] $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if (@mysqli_num_rows($r) == 0) { // A match was made. //$row = mysqli_fetch_array ($r, MYSQLI_ASSOC); //echo '<p class="error">Sorry, the voucher code you entered is invalid.</p>'; //}else{ //if( $voucher['active'] == 1 ) //{ if( $voucher['expired'] == 1 ) { echo '<p class="error">Sorry, this voucher has expired</p>'; return false; } else { if( $voucher['num_vouchers'] != 0 ) { if( $subtotal >= $voucher['min_basket_cost'] )[/size] [size=3]{ $discountCode = $voucherCode; $discountCodeId = $voucher['ID']; if( $voucher['discount_operation'] == '%' ) { $subtotal = $subtotal - (($subtotal)/100)*$voucher['discount_amount']; $voucher_notice = 'A ' . $voucher['discount_amount'] . '% discount has been applied to your order'; return true; } elseif( $voucher['%'] == '-' ) { $subtotal = $subtotal - $voucher['discount_amount']; $voucher_notice = 'A discount of £' . $voucher['discount_amount'] . ' has been applied to your order'; return true; } } else { echo '<p class="error">= Sorry, your order total is not enough for your order to qualify for this discount code</p>'; return false; } } else { echo '<p class="error">Sorry, this was a limited edition voucher code, there are no more instances of that code left</p>';return false; } } } else { echo '<p class="error">Sorry, the voucher code you entered is no longer active</p>';return false;}}//}//} ?> <form action="basketv.php" method="post" > <p><b>Voucher code:</b> <input type="text" name="voucher" size="20" maxlength="40" /></p> <input type="submit" name="submit" value="Submit" /> <input type="hidden" name="submitted" value="TRUE" /> </form>[/size] [size=3] [/size] [size=3]
  19. I have tried (without much success) to add a voucher code to the basket as in the book, but it is not updating the discount. Does anyone have any advice on what I have done wrong. I am using phpmmyadmin and have created the fields in here. <?php session_start(); $_SESSION['cart']; ?> <?php #script together. This combines addtocart and view cart. if (isset ($_GET['pid']) && is_numeric($_GET['pid']) ) { $pid = (int) $_GET['pid']; if (isset($_SESSION['cart'][$pid])) { $_SESSION['cart'][$pid]['quantity']++; }else{ //new product require_once ('../mysqli_connect.php'); $q="SELECT price, product_name FROM product WHERE product_id=$pid"; $r=mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { list($price, $product_name) = mysqli_fetch_array ($r, MYSQLI_NUM); $_SESSION['cart'][$pid] = array ('quantity' => 1, 'price' => $price, 'product_name' => $product_name); }else{ echo '<div align="center">Your basket has been updated</div>'; } } }else{ //no print ID echo '<div align="center"> Your basket has been updated</div>'; } if (isset($_POST['submitted'])) { foreach ($_POST['qty'] as $k => $v) { $pid = (int) $k; $qty = (int) $v; if ($qty ==0) { // Delete. unset ($_SESSION['cart'][$pid]); } elseif ($qty >0 ) { // Change quantity. $_SESSION['cart'][$pid]['quantity'] = $qty; } } } if (!empty($_SESSION['cart'])) { require_once ('../mysqli_connect.php'); $q="SELECT product_id, description, price, product_name FROM product WHERE product_id IN ("; foreach ($_SESSION['cart'] as $pid => $value) { $q .= $pid . ','; } $q=substr($q, 0, -1) . ') ORDER BY product_name ASC'; $r = mysqli_query ($dbc, $q); // Create a form and a table: echo '<form action="basketv.php" method="post"> <table border="0" width="90%" cellspacing="3" cellpadding="3" align="center"> <tr> <td align="left" width="30%"><b>Description</b></td> <td align="left" width="30%"><b>Product Name</b></td> <td align="right" width="10%"><b>Price</b></td> <td align="center" width="10%"><b>Qty</b></td> <td align="right" width="10%"><b>Total Price</b></td> </tr> '; $total =0; // Total cost of the order. $postage=1.00; while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) { $subtotal = $_SESSION['cart'][$row['product_id']]['quantity'] * $_SESSION['cart'][$row['product_id']]['price']; $total1 += ($subtotal); // Print the row. echo "\t<tr> <td align=\"left\">{$row['description']}</td> <td align=\"left\">{$row['product_name']}</td> <td align=\"right\">£{$_SESSION['cart'][$row['product_id']]['price']}</td> <td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]['quantity']}\" /></td> <td align=\"right\">£" . number_format ($subtotal, 2) . "</td> </tr>\n"; } $total += ($postage + $total1); mysqli_close($dbc); echo '<tr> <td colspan="4" align="right"><b> Postage:</b></td><br/> <td align="right">£' . number_format ($postage, 2) . '</td> <br/> <br/> <br/> <br/> <td colspan="4" align="right"><b>Voucher Discount:</b></td> <td align="right">£' . number_format ($voucher, 2) . '</td> <td colspan="4" align="right"><b>Total:</b></td> <td align="right">£' . number_format ($total, 2) . '</td> </tr> </table><p align="left">Enter a quantity of 0 to remove an item. <div align="left"><input type="submit" name="submit" value="Update" /></div> <input type="hidden" name="submitted" value="TRUE" /> <p align="center"><a href="logincustomer.php"><img src="checkout.gif" width="83" height="27" border="0"/></a></p>'; } else { echo '<p>Your cart is currently empty.</p>'; } ?> <?php { if( $voucherCode != '' ){ // we need the date, to see if the voucher has expired $cts = date('Y-m-d H:i:s'); $q = "SELECT *, if('{$cts}' > expiry, 1, 0)AS expired FROM discount_codes WHERE vouchercode='{$voucherCode}' LIMIT 1"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if (@mysqli_num_rows($r) == 0) { // A match was made. $row = mysqli_fetch_array ($r, MYSQLI_ASSOC); echo '<p class="error">Sorry, the voucher code you entered is invalid.</p>'; }else{ if( $voucher['active'] == 1 ) { if( $voucher['expired'] == 1 ) { echo '<p class="error">Sorry, this voucher has expired</p>'; return false; } else { if( $voucher['num_vouchers'] != 0 ) { if( $subtotal >= $voucher['min_basket_cost'] ) { $discountCode = $voucherCode; $discountCodeId = $voucher['ID']; if( $voucher['discount_operation'] == '%' ) { $subtotal = $subtotal - (($subtotal)/100)*$voucher['discount_amount']; $voucher_notice = 'A ' . $voucher['discount_amount'] . '% discount has been applied to your order'; return true; } elseif( $voucher['%'] == '-' ) { $subtotal = $subtotal - $voucher['discount_amount']; $voucher_notice = 'A discount of £' . $voucher['discount_amount'] . ' has been applied to your order'; return true; } } else { echo '<p class="error">= Sorry, your order total is not enough for your order to qualify for this discount code</p>'; return false; } } else { echo '<p class="error">Sorry, this was a limited edition voucher code, there are no more instances of that code left</p>';return false; } } } else { echo '<p class="error">Sorry, the voucher code you entered is no longer active</p>';return false;}}}} ?> <form action="basketv.php" method="post" > <p><b>Voucher code:</b> <input type="text" name="voucher" size="20" maxlength="40" /></p> <input type="submit" name="submit" value="Submit" /> </form>
  20. I have tried to remove this from the while loop, but then it does not work. Can you please advise on how I can create the postage outside of the loop so it works. Thanks
  21. I have used the shopping cart as in the book, but I have a problem. If there is one product in the cart the shipping is applied once (even if there are 2 of this item) the problem is if I add another product, it adds the shipping twice. Code is below: <?php #script together. This combines addtocart and view cart. $_SESSION['total'] = $_POST['total']; $_SESSION['product_name'] = $_POST['product_name']; $page_title = 'Add to cart'; if (isset ($_GET['pid']) && is_numeric($_GET['pid']) ) { $pid = (int) $_GET['pid']; if (isset($_SESSION['cart'][$pid])) { $_SESSION['cart'][$pid]['quantity']++; }else{ //new product require_once ('../mysqli_connect.php'); $q="SELECT price FROM product WHERE product_id=$pid"; $r=mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { list($price) = mysqli_fetch_array ($r, MYSQLI_NUM); $_SESSION['cart'][$pid] = array ('quantity' => 1, 'price' => $price); }else{ echo '<div align="center">Your basket has been updated</div>'; } } }else{ //no print ID echo '<div align="center"> Your basket has been updated</div>'; } if (isset($_POST['submitted'])) { foreach ($_POST['qty'] as $k => $v) { $pid = (int) $k; $qty = (int) $v; if ($qty ==0) { // Delete. unset ($_SESSION['cart'][$pid]); } elseif ($qty >0 ) { // Change quantity. $_SESSION['cart'][$pid]['quantity'] = $qty; } } // End of FOREACH. } // End of SUBMITTED IF. // Display the cart if it's not empty... if (!empty($_SESSION['cart'])) { $q="SELECT product_id, description, price, product_name FROM product WHERE product_id IN ("; foreach ($_SESSION['cart'] as $pid => $value) { $q .= $pid . ','; } $q=substr($q, 0, -1) . ') ORDER BY product_name ASC'; $r = mysqli_query ($dbc, $q); // Create a form and a table: echo '<form action="basket.php" method="post"> <table border="0" width="90%" cellspacing="3" cellpadding="3" align="center"> <tr> <td align="left" width="30%"><b>Description</b></td> <td align="left" width="30%"><b>Product Name</b></td> <td align="right" width="10%"><b>Price</b></td> <td align="center" width="10%"><b>Qty</b></td> <td align="right" width="10%"><b>Total Price</b></td> </tr> '; $total =0; // Total cost of the order. while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) { $subtotal = $_SESSION['cart'][$row['product_id']]['quantity'] * $_SESSION['cart'][$row['product_id']]['price']; $postage=1.00; $total += ($postage + $subtotal); echo "\t<tr> <td align=\"left\">{$row['description']}</td> <td align=\"left\">{$row['product_name']}</td> <td align=\"right\">£{$_SESSION['cart'][$row['product_id']]['price']}</td> <td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]['quantity']}\" /></td> <td align=\"right\">£" . number_format ($subtotal, 2) . "</td> </tr>\n"; } // End of the WHILE loop. $_SESSION['total']=$total; $_SESSION['order_id'] =$order_id; $pn = $_SESSION['product_name']; mysqli_close($dbc); // Close the database connection. echo '<tr> <td colspan="4" align="right"><b> Postage:</b></td><br/> <td align="right">£' . number_format ($postage, 2) . '</td> <br/> <br/> <br/> <br/> <td colspan="4" align="right"><b>Total:</b></td> <td align="right">£' . number_format ($total, 2) . '</td> </tr> </table><p align="left">Enter a quantity of 0 to remove an item. <div align="left"><input type="submit" name="submit" value="Update" /></div> <input type="hidden" name="submitted" value="TRUE" /> <p align="center"><a href="logincustomers.php"><img src="checkout.gif" width="83" height="27" border="0"/></a></p>'; } else { echo '<p>Your cart is currently empty.</p>'; } ?>
  22. I tried this, but no quantities are shown: <?php $_SESSION['cart'][$row['quantity]]; echo "<p>There are {$row['quantity']} items in your basket</p>"; ?>
  23. I have created the cart from the book (as per the code) and this works great, but on every page I would like for example, "You have 2 items in your cart" which is taking the quantity from the cart. Do you have any code on how this can be achieved on every page? Thanks
×
×
  • Create New...