Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Recalculating DB values

by jwest (Friar)
on Jul 03, 2001 at 02:15 UTC ( [id://93340]=note: print w/replies, xml ) Need Help??


in reply to Recalculating DB values

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://93340]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-24 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found