Hey, I am trying to write a system which will allow the user to update the quantity of products they wish to purchase.
It allows me to update the quantity of every single quantities incremented by a sinle value, but I cant have different items have different quantities.
Here is the code I'm using:
function get_quantity($id, $quantity) {
$quantity_array = array ("product_id"=> $id, "quantity"=> $quantity);
return $product_array;
}
if ($delete) {
$products_array = array_diff ($products_array, array ($delete));
}
if (isset($add)) {
$products_array[] = $add;
}
print "<br>";
print "<table border=0 align=center width=500 class=main>";
$products_array = array_unique($products_array);
foreach ($products_array as $id) {
if (!isset($quantity)) {
$quantity = 1;
}
$result = mysql_query("select * from products where id = $id");
while ($array = @mysql_fetch_array($result, MYSQL_ASSOC)) {
$myquantity = get_quantity($id, $quantity);
print "<tr><td>";
print "<form action='cart.php' action=POST>";
print "<table class=main><tr>";
print "<td>";
print "<b><u>".$array[name]."</u></b> | ";
$array[price] = ($array[price])*($quantity);
print "<i>$".$array[price]."</i><br>";
print "</td><td>";
print "Quantity: <input type='text' name='quantity' value='".$myquantity[quantity]."' maxlength=1 size=1>";
print "</td><td>";
print "<input type='submit' value='Update'></form></td></tr>";
print "<tr><td colspan=2>";
print $array[description]."</td></tr></table>";
print "</td></tr>";
}
}
print "<tr><td><a href='productindex.php?PHPSESSID=".$sessid."'>Keep Shopping</a></td></tr></table>";
This is the URL:
http://www.peroxsys.com/store/productindex.php
Shop and add a few items to the cart, then change the quantity and see what I mean. Thanks for the help!
Product Quantities
Moderator: General Moderators
First off, if you have a db, use it. When a user enters the shopping experience, start a session, save it in an alive table(alive: dtstamp update on each page access), and for every item added, place it in a session_temp_table and access/manipulate its values at the visitors request. Leave sessions open for 30 mins after their last update in the session table, then delete the table after that.
But, I know I absolutely hate it when someone says "do it my way" and don't help me with the way I was doing it, so if you want to do it your way...
You need to differentiate between the quatity being changed and also retain the other values:
Pass all current values in the form POST though hidden fields, and make your form fields named quantity have the id attached, and :
then explode the id off of the end and use that as the differentiater.
But, I know I absolutely hate it when someone says "do it my way" and don't help me with the way I was doing it, so if you want to do it your way...
You need to differentiate between the quatity being changed and also retain the other values:
Pass all current values in the form POST though hidden fields, and make your form fields named quantity have the id attached, and :
Code: Select all
<input type="text" name="quantity_<?php echo $myquantityї'id'] ?>"-
MetalDimension
- Forum Newbie
- Posts: 3
- Joined: Sat Nov 16, 2002 12:42 am
But see I can't refer to variables as $quantity_$id. I need the user to see a quantity or one in itially, and then if they change it, the new form field has a value of their change. I can't have this:
Code: Select all
print "Quantity: <input type='text' name='quantity_$id' value='".$quantity_$id."' maxlength=1 size=1>";please read Sticky: Before Post Read: Concerning Passing Variables in PHP 4.2+
DOes this solve your problem?
DOes this solve your problem?
-
MetalDimension
- Forum Newbie
- Posts: 3
- Joined: Sat Nov 16, 2002 12:42 am
No actually that really doesn't have anything to do with my problem...
But I thank everyone for their help. I think I'm going to approach it a different way, because this way is too complicated and not really worth it - there are better ways, such as just allowing the user to specify the quantity on the individual product page.
Thanks anyways!
But I thank everyone for their help. I think I'm going to approach it a different way, because this way is too complicated and not really worth it - there are better ways, such as just allowing the user to specify the quantity on the individual product page.
Thanks anyways!