Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on sort of a product info updating system using CGI and DBI. Right now, I query my db for the product name and quantity and display the info of all products on a single webpage. I display a name column and current quantity column, both non-editable. I also have an "add" column and a "subtract" column next to each product's current quantity level where I can enter a number to add or subtract from the quantity. My question is, how do I go about grabbing all the values to add or subtract and do the calculations on the appropriate products?

Replies are listed 'Best First'.
Re: Recalculating DB values
by jwest (Friar) on Jul 03, 2001 at 02:15 UTC
    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
    
(jeffa) Re: Recalculating DB values
by jeffa (Bishop) on Jul 03, 2001 at 02:16 UTC
    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--