Gary wrote:
"Joe Smith" <Jo*******@inwap.com> wrote in message
news:0uROb.85309$5V2.115114@attbi_s53...
Gary wrote:
With 100 Students displayed I need about 500 variables minimum so I can
store all new values entered on any of the 100 students and then pass
them
all to another perl script to perform the update.
Instead of using 500 variables, you can use a hash of arrays.
(Five keys in the hash, each pointing to arrays with 100 elements.)
<input type="text" name="student[0]" size="20">
<input type="text" name="stu_id[0]" size="20">
<input type="checkbox" name="delete[0]" value="1">
<input type="text" name="student[1]" size="20">
<input type="text" name="stu_id[1]" size="20">
<input type="checkbox" name="delete[1]" value="1">
Can these arrays be passed between HTML / CGI scripts like regular
variables.
Arrays cannot be passed that way but information can. That is:
1) Get original data into a Perl array (or do one row at a time).
2) Convert the data to something the browser can understand.
3) Send it as part of an HTML form that the user can modify.
4) Get the form data back when user submits form.
5) Parse the form, putting data values into Perl variables.
Note: Use arrays or hashes, not gazillion scalars.
6) Process the data, either as an entire array, or one row at
a time. The latter is much better for reporting what
was successfully modified and what was not.
Here is something from step 2:
<input type="text" name="student[0]" size="20">
That is HTML generated by a CGI or ASP or PHP or anything that
can query a database, get a bunch of rows, then write the information
for each row as an <INPUT> item in an HTML form. To keep things
simple, I deliberately did not include things like <TR>...</TR> that
would make the resulting HTML look good. Note that the argument
for name="" is not a Perl variable, but rather an identifier that
can be used to store the returned value back into a suitable Perl
variable (which could be an element of an array or hash).
Then use something like this when looping through the form keys:
if ($key =~ /(.*?)\[(\d+)\]/) {
$data{$1}[$2] = $value;
} elsif ((\w+)(.*?)/) {
$data{$1}{$2} = $value;
} else {
warn "Unparsable input: key=$key value=$value";
}
For convenience:
my @students = @{$data{student}} or warn;
my @stu_ids = @{$data{stu_id}} or warn;
my @deletes = @{$data{delete}} or warn;
Also could you comment the code above a bit - I do not use Perl
a whole lot and it would save me time working out exactly what
it does.
The following assumes you've already used CGI.pm to create $query.
@names = $query->param; # Get a list of the form element names.
# In this example, it will be ('student[0]', 'student[1]',
# 'stu_id[0]', 'stu_id[1]', 'delete[0]', 'delete[1]')
# but not necessarily in any particular order.
foreach $key (@names) {
$value = $query->param($key); # Get the value the brower sent back
# Look for things like $key='student[0]' $value='Joe Smith'
# Also accept things like $key='course{teacher}' $value='Gary'
if ($key =~ /(.*?)\[(\d+)\]/) {
$data{$1}[$2] = $value; # Store into hash of arrays
} elsif ((\w+)(.*?)/) {
$data{$1}{$2} = $value; # Store into hash of hashes
}
}
That last bit could be written as:
$key =~ /^student\[(\d+)\]/ and $student[$1] = $value;
$key =~ /^stu_id\[(\d+)\]/ and $stu_id[$1] = $value;
$key =~ /^delete\[(\d+)\]/ and $delete[$1] = $value;
$key =~ /^course(\{.*?\})/ and $course{$1} = $value;
The program doing the CGI parsing ends up with three 100-element
arrays (plus a hash) instead of 300+ variables.
You can then loop through the arrays and execute SQL statements
one student at a time. When done, send back an HTML page listing
how many of the requested changes were successful.
-Joe
--
I love my TiVo -
http://www.inwap.com/u/joe/tivo/