View Full Version : Indexing loop items
epistachium
02-10-2007, 04:31 AM
Hey all!
My script generates an array of hashes after extracting data from a database:
while (@data = $sth->fetchrow_array()) {
my %hash = map {@keys->[$_], @data->[$_] 0..$#data;
push @output, \%hash;
}
That is displayed on a webpage through the TMPL_LOOP tool from HTML::Template onto an online form that will be submitted next with edits to the list.
Once displayed, I want to be able to retrieve any one particular item from that loop for editing the database. If it were a single variable passed to the template, then I would send it to the subsequent script via:
my $variable = ¶m('foo');
However, I do not know how to specify to the next script that it should be the i-th item of the loop that it should edit, and not the m-th, or n-th, or p-th.
can I use a variable inside the 'my variable = ¶m()' statement, that is, something like ¶m('foo$i'); or something like that? Any clues?
yell if this does not contain enough detail.
KevinADC
02-10-2007, 05:44 AM
can I use a variable inside the 'my $variable = ¶m()' statement, that is, something like ¶m('foo$i'); or something like that? Any clues?
you could if the form field names match whatever foo$i is, but you also have to use double-quotes to form annonymous strings ike that:
my $variable = param("foo$i");
otherwise the $ is treated literally as a dollar sign. Could maybe help more if we knew what the form code looks like.
epistachium
02-12-2007, 12:47 AM
Thanks for your reply, Kevin.
Here is a more detailed version of the code, incorporating your suggestion but still without success:
In essence, the process I am trying to create is akin to an order form after displaying a list of products from a database. As in the original posting, the disclaimer speaks of my inexperience with coding, perl or html, so if you can provide good sites with examples of perl/cgi order forms, that would be equally useful.
#####################
To acquire the data to be displayed, I use the following (quoting from original posting):
my $index = 1;
while (@data = $sth->fetchrow_array()) {
my %hash = map {@keys->[$_], @data->[$_] 0..$#data;
$hash{'index'} = $index;
push @output, \%hash;
$index++;
}
$template->param(DATA_LOOP => \@output);
print $template->output();
#####################
Displaying the data on the web (leaving out some formatting tags):
<form name="xxx" method="post" enctype="multipart/form-data">
<TMPL_LOOP NAME=DATA_LOOP>
<tr>
<td>
<TMPL_VAR NAME="field_01">
<input type="hidden" name="field_01<TMPL_VAR NAME="index">">
</td>
<td>
<TMPL_VAR NAME="field_02">
<input type="hidden" name="field_02<TMPL_VAR NAME="index">">
</td>
<td>
<TMPL_VAR NAME="field_03">
<input type="hidden" name="field_03<TMPL_VAR NAME="index">">
</td>
etc...
</tr>
</TMPL_LOOP>
<input type="submit" name="submit">
</form>
#####################
and then, when one of the fields for a particular interation of the loop is modified by the user, I want to capture the information associated with that particular iteration. My code right now for the third part looks something like:
my $submit = ¶m('submit');
if ($submit) {
for (my $i=1; $i<=$some_variable; $i++ {
my $field_01 = ¶m("field_01.$i");
if ($field_01 >= 1) {
'Perform following steps';
} else {
'Warn the user that nothing has changed'
}}
bla bla bla}
#####################
Once again, many thanks!
KevinADC
02-12-2007, 01:16 AM
are you really using this code?
while (@data = $sth->fetchrow_array()) {
my %hash = map {@keys->[$_], @data->[$_] 0..$#data;
$hash{'index'} = $index;
push @output, \%hash;
$index++;
}
it's not even valid code. The map function has no ending brace: }
I am simply confused by your code. I don't understand why you just don't get the name/value of the form field you want to do something with. They have unique names:
field_01
field_02
etc
etc
maybe what you want to do is name them all the same and get them back in list context instead of scalar context? But I don't know.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.