As always, there is more than one way to do it. I'd probably
go about it like this:
First, when generating the fields for the add and subtract
columns, give them predictable names. Perhaps
'add_productid' and 'subtract_productid' where productid is
the same as the identifier you use in your database, probably the
primary key.
Then, get the parameters passed in to your script. If your
CGI query object is $q, you can do this with a call like
my %params = $q->Vars;
Then, loop through the keys of %params, searching for keys that
match the names you are expecting. So, if you encounter a key
for 'add_1', and you're willing to update the quantity of the
product with the identifier of '1' (not that you should be using numbers
as hash keys as I have below, strictly speaking), you can set the quantity
using the value of that parameter. The same holds true with
'subtract'.
This would give you a piece of code that looks a little like:
my %params = $q->Var;
for (keys(%params)) {
if ($_ =~ /add_(\d+)/) {
$quantity{$1} += $q->param($1);
}
if ($_ =~ /subtract_(\d+)/ {
$quantity{$1} -= $q->param($1);
}
}
Of course, this lacks details specific to your project (such as manipulating your database). It's also
untested, and probably a sub-optimal solution. But it should
start you in the right direction. Hope this helps!
--jwest
-><- -><- -><- -><- -><-
All things are Perfect
To every last Flaw
And bound in accord
With Eris's Law
- HBT; The Book of Advice, 1:7
| [reply] [d/l] [select] |
Rather vague question, but sounds relatively simple enough.
Each 'add' and 'subtract' will be links back to your CGI
script that will pass the product id as well as the new values along. You can precalculate them when you generate
the links or you can fetch the current value from the
database just before you update the values.
In your script, test for an add or subtract request and
either use the new value passed from the script or fetch
the current value from the database.
If you choose the former, just UPDATE with that value
(don't forget to validate ALL user input - trust no one!).
If you did a lookup, add one or subtract one accordingly
(i recommend one sub for add and one for subtract in this
case), and UPDATE to the database.
If you wish to do a 'global' add or subtract - then you
will need to pass all the id's of each product - be sure
and use the POST method, as this request will be quite
large.
Jeff
R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--
| [reply] |