Jump to content
Larry Ullman's Book Forums

Drop Down List On Products Page


Recommended Posts

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>';}}}?>

Link to comment
Share on other sites

You're missing a closing select tag.

 

If you want the quantities accessible in the cart script, you'll have to construct (for each quantity) a link including the quantity to your cart script and retrieve it using the usual $_GET syntax.

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

Which quantity are you talking about? The quantity set by the customer or the stock quantity? I'm asking because the second code sample you've provided seems to indicate you're talking about stock quantities.

 

If it's the quantity set by a customer (i.e. add to cart) how are you passing that quantity to the cart script and how are you accessing that quantity in the cart script?

 

If it's the stock quantity, what debugging steps have you taken to ensure that the quantity is being successfully retrieved from the database?

  • Upvote 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Can you post the code you are using to set the quantity in the session.

 

If you're setting this quantity in $_SESSION in the cart script, can you also post the code which you are using to send the quantity to the cart script.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

That's not really helpful, Kerry; I don't see any $_GET variable that resembles anything that would be used for a user set quantity for a product. What code are you using to send the quantity to the cart script? (please do not just repost the whole script) i.e. just the part which actually sets the $_GET variable for the quantity.

 

Sorry if this sounds harsh, but do you know the basics of PHP and in particular how to set and retrieve a $_GET variable? Because your answers seem to indicate you don't.

 

If you're not able to do this, your best bet is to get following book http://www.amazon.co...s=9780321784070, learn the basics then come back to this book (which assumes PHP knowledge) and have another go.

  • Upvote 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

If you're not able to provide the basics of how you're setting and retrieving your quantity $_GET variable, I doubt anyone will be able to help you without just rewriting the code for you, I can't help you any further.

Link to comment
Share on other sites

 Share

×
×
  • Create New...