Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

Array Question

Hi all,

I have 3 arrays (each with the same number of elements) and i want to do an insert foreach element of the first array ($chart_id). What's the best way to loop through it?  

The data is pulled from user input and going into a table for lookups. I'm using Perl. :p

sub SubmitNewDaily {
   my $user_id       = param('user_id');
   my @chart_id      = param('chart_id');
   my @cat_id        = param('cat_id');
   my @quantity      = param('quantity');
   my $date          = param('date');

   my @data = (@chart_id, $date, @cat_id, $user_id, @quantity); ## I know this doesn't work... :)
   
   StartTemplate();
   print "<h2>Test Page: Params Passed to Insert</h2>";
   print "Date: $date <p>
          UserId:$user_id <p>
          Food Id: @chart_id <p>
          CategoryId: @cat_id <p>
          Quantity: @quantity <p>";
   EndTemplate();


### I want to insert a record for each one of the chart_id's in the array but i also want to have the other
### fields populated with their values. ie - the values in: @cat_id, @quantity


   foreach $chart_id(@chart_id) {
            my $out = $dbh->do("INSERT INTO diet_content(chart_id, date, cat_id, user_id, chart_quantity)
      VALUES(?,?,?,?,?)",{},@data);
   }
}

Any help would be great! Thank you.
-mookie


 
0
mookie13
Asked:
mookie13
1 Solution
 
markhoyCommented:
This:

my $user_id       = param('user_id');
  my @chart_id      = param('chart_id');
  my @cat_id        = param('cat_id');
  my @quantity      = param('quantity');
  my $date          = param('date');


is chart_id an array or does it contain a single value?
0
 
ozoCommented:
@data=map{"$chart_id[$_] $date $cat_id[$_] $user_id $quantity[$_]"}0..$#chart_id;
0
 
mookie13Author Commented:
chart_id is an array with multiple values depending on how many items were selected by the user on the previous page.

There will be the exact amount of values for each of the arrays.  The only single values are in user_id and date.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
dc197Commented:
my @chart_id      = param('chart_id');

If param('chart_id') returns something in scalar context (e.g. if it returns a single value) then @chart_id will be an array with only one element.

Is this really what you want? You say "chart_id is an array with multiple values depending..."
0
 
mookie13Author Commented:
it's an array with multiple values so long a user selected more than one item from the food chart.  in that case it would be an array with a single item or a scalar.

the way it works is a user goes to a food chart and selects the items he/she has consumed and applicable quantity.  this data is then inserted into a content table.

the content table are:

content_id
date
quantity (ie - i ate 3 carrots)
chart_id (foodchart item id)
cat_id (category)

that's why i'm passing the values to the submit part of the script as an array since there are multiple food items that can be selected.  each one of the food items selected will have a corresponding quantity (1 or more) and category.

each array will have the same amount of elements.

so when i'm doing the insert i want to insert for each @chart_id that was passed but i want the corresponding @quantity and @cat_id too.  
0
 
mookie13Author Commented:
To ozo:

Thank you for the map function idea. This works and gives me an array of all the elements.

So when I select two items I get this:

21 2003-3-12 2 1 1 14 2003-3-12 2 1 1

Which is all of the params passed:
21= chart_id
2003-3-12 = date
2 = category
1= user_id
1 = quantity

and so on.

What I'm trying to do here is insert a row for each one of the chart_id's passed.

I guess I'm confused on how to do that since they are all one string now in the @data array because it trys to insert all of the values even though there aren't that many fields.

Thoughts?







0
 
ozoCommented:
my @data = (@chart_id, $date, @cat_id, $user_id, @quantity); ## I know this doesn't work... :)

what are you trying to do here?
What are the contents of (@chart_id, $date, @cat_id, $user_id, @quantity), and what do you want @data to contain?
0
 
mookie13Author Commented:
oof... i was talking with the d00d from the Perl Journal (http://www.tpj.com) and he laughed at me and gave the code i needed.

   for ($i=0; $i<=$#chart_id; $i++) {
     @data=($chart_id[$i], $date, $cat_id[$i], $user_id, $quantity[$i]);

     my $out = $dbh->do("INSERT INTO diet_content(chart_id, date, cat_id, user_id, chart_quantity)
                        VALUES(?,?,?,?,?)",{},@data);

   }
0
 
mookie13Author Commented:
Hi all,

Not too sure about how to close this question up since I found the answer outside of EE.  

I do want to say THANKS! to all that helped. I learned something from those responses.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now