Product Quantities

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MetalDimension
Forum Newbie
Posts: 3
Joined: Sat Nov 16, 2002 12:42 am

Product Quantities

Post by MetalDimension »

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!
MeOnTheW3
Forum Commoner
Posts: 48
Joined: Wed Nov 13, 2002 3:28 pm
Location: Calgary, AB

Post by MeOnTheW3 »

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 :

Code: Select all

<input type="text" name="quantity_<?php echo $myquantity&#1111;'id'] ?>"
then explode the id off of the end and use that as the differentiater.
MetalDimension
Forum Newbie
Posts: 3
Joined: Sat Nov 16, 2002 12:42 am

Post by MetalDimension »

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>";
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

MetalDimension
Forum Newbie
Posts: 3
Joined: Sat Nov 16, 2002 12:42 am

Post by MetalDimension »

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!
Post Reply