?
Solved

Array Question

Posted on 2003-03-09
9
Medium Priority
?
151 Views
Last Modified: 2013-12-25
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
Comment
Question by:mookie13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 7

Expert Comment

by:markhoy
ID: 8101874
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
 
LVL 84

Expert Comment

by:ozo
ID: 8102704
@data=map{"$chart_id[$_] $date $cat_id[$_] $user_id $quantity[$_]"}0..$#chart_id;
0
 

Author Comment

by:mookie13
ID: 8105283
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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
LVL 5

Expert Comment

by:dc197
ID: 8111256
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
 

Author Comment

by:mookie13
ID: 8122751
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
 

Author Comment

by:mookie13
ID: 8122944
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
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 8123987
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
 

Author Comment

by:mookie13
ID: 8124185
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
 

Author Comment

by:mookie13
ID: 8145431
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question