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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.