Cat101
03-23-2005, 06:11 PM
Any insight into the following very annoying Array Reference problem would be greatly appreciated! I'm hoping there is a very stupid mistake that I have overlooked in my frustration.
The code is test code for a reporting site. What I want to do is retrieve data from a database, display the data in a table/form to the user and then when they click continue, for the script to check whether the data has already been retrieved from the database, if so, display it again. If not, then run the database query and display that.
This is part of a much more complex set of reports that I wish to allow the user to maniuplate parts of the table without having to re-run lengthy queries.
I am therefore attempting to pass a reference to the dataset array as a hidden field when the form is submitted. The script checks for data in this field parameter before running the qeury to the database. What I expected to happen was that the parameter would be sent with a single reference to the array. That reference could then be dereferenced to display the contents.
In my test script however, there are four lines of data generated by the query and what actually is passed to the query string, is coincidentally four array references with the hidden field parameter name.
i.e. <....>Broken.pl?data=ARRAY%280x84d3608%29&data=ARRAY%280x84d3650%29&data=ARRAY%280x84d3698%29&data=ARRAY%280x84d36e0%29&Continue=Continue
Consequently I assume that the checking script cannot dereference and display the data and so only displays the column headings when the Continue button is clicked.
I cannot get my head around this and what I assumed to be simple is driving me mad. Any help would be greatly appreciated.
Thanks..
Script: (Specific Database connections/Urls etc have been removed and replaced with <..>)
#!/usr/local/bin/perl
## Don't Buffer output
$| = 1;
use strict;
use CGI qw(:standard :html3);
my $q = new CGI;
# Hard-coding as use of $q->url() is restricted
my $curr_url = '<.....>/Curr/Broken.pl';
# CONNECTION
my $dbh;
eval{ $dbh = <connect> ;};
if($@){ print $q->p("Error Connecting to Database: $@");}
# PARAMETERS
# Default sort on first column
my $sort = 0;
if ($q->param("sort")){
$sort = $q->param("sort");
}
## RETRIEVAL
# Unless Data has already been retrieved, run query
my $sqlsth;
my $table_data;
if (not defined $q->param("data")){
my $sql = "SELECT * FROM <...>";
eval{ $sqlsth = $dbh->prepare($sql);};
if($@){print $q->p("Database Error Preparing Statements: $@");}
eval{ $sqlsth->execute(@exec_param); };
if($@){print $q->p("Database Error Executing Statements: $@");}
$table_data = $sqlsth->fetchall_arrayref;
}
else {
$table_data = $q->param("data");
}
## DISPLAY
# Title
print $q->h4({-align=>"center"},font({-color=>"ForestGreen"},"Title"));
# Form/Table
print $q->start_form({-method=>"GET",-action=>($curr_url)});
print $q->start_table({-border=>0,-align=>"center",-width=>"50%"});
# Print Headers as sort value links
for (my $i=0;$i<4;$i++){
print $q->th({-class=>"yellow1"},($q->a({-href=>($curr_url)."?sort=".$i},"Column ".$i)));
}
# Sort by relevant column and type
my $column = $sort;
my @sorted_table = sort {($a->[$column]=~/\D/)?($a->[$column] cmp $b->[$column]):($a->[$column] <=> $b->[$column])} @$table_data;
# Data
foreach my $row(@sorted_table){
print $q->Tr($q->td([@$row]));
}
# Generate Hidden Field
print $q->hidden({-name=>"data",-value=>$table_data});
# End and Submit
print $q->end_table;
print $q->p({-align=>"center"},$q->submit({-name=>"Continue"}));
print $q->end_form;
# DISCONNECT
$dbh->disconnect;
The code is test code for a reporting site. What I want to do is retrieve data from a database, display the data in a table/form to the user and then when they click continue, for the script to check whether the data has already been retrieved from the database, if so, display it again. If not, then run the database query and display that.
This is part of a much more complex set of reports that I wish to allow the user to maniuplate parts of the table without having to re-run lengthy queries.
I am therefore attempting to pass a reference to the dataset array as a hidden field when the form is submitted. The script checks for data in this field parameter before running the qeury to the database. What I expected to happen was that the parameter would be sent with a single reference to the array. That reference could then be dereferenced to display the contents.
In my test script however, there are four lines of data generated by the query and what actually is passed to the query string, is coincidentally four array references with the hidden field parameter name.
i.e. <....>Broken.pl?data=ARRAY%280x84d3608%29&data=ARRAY%280x84d3650%29&data=ARRAY%280x84d3698%29&data=ARRAY%280x84d36e0%29&Continue=Continue
Consequently I assume that the checking script cannot dereference and display the data and so only displays the column headings when the Continue button is clicked.
I cannot get my head around this and what I assumed to be simple is driving me mad. Any help would be greatly appreciated.
Thanks..
Script: (Specific Database connections/Urls etc have been removed and replaced with <..>)
#!/usr/local/bin/perl
## Don't Buffer output
$| = 1;
use strict;
use CGI qw(:standard :html3);
my $q = new CGI;
# Hard-coding as use of $q->url() is restricted
my $curr_url = '<.....>/Curr/Broken.pl';
# CONNECTION
my $dbh;
eval{ $dbh = <connect> ;};
if($@){ print $q->p("Error Connecting to Database: $@");}
# PARAMETERS
# Default sort on first column
my $sort = 0;
if ($q->param("sort")){
$sort = $q->param("sort");
}
## RETRIEVAL
# Unless Data has already been retrieved, run query
my $sqlsth;
my $table_data;
if (not defined $q->param("data")){
my $sql = "SELECT * FROM <...>";
eval{ $sqlsth = $dbh->prepare($sql);};
if($@){print $q->p("Database Error Preparing Statements: $@");}
eval{ $sqlsth->execute(@exec_param); };
if($@){print $q->p("Database Error Executing Statements: $@");}
$table_data = $sqlsth->fetchall_arrayref;
}
else {
$table_data = $q->param("data");
}
## DISPLAY
# Title
print $q->h4({-align=>"center"},font({-color=>"ForestGreen"},"Title"));
# Form/Table
print $q->start_form({-method=>"GET",-action=>($curr_url)});
print $q->start_table({-border=>0,-align=>"center",-width=>"50%"});
# Print Headers as sort value links
for (my $i=0;$i<4;$i++){
print $q->th({-class=>"yellow1"},($q->a({-href=>($curr_url)."?sort=".$i},"Column ".$i)));
}
# Sort by relevant column and type
my $column = $sort;
my @sorted_table = sort {($a->[$column]=~/\D/)?($a->[$column] cmp $b->[$column]):($a->[$column] <=> $b->[$column])} @$table_data;
# Data
foreach my $row(@sorted_table){
print $q->Tr($q->td([@$row]));
}
# Generate Hidden Field
print $q->hidden({-name=>"data",-value=>$table_data});
# End and Submit
print $q->end_table;
print $q->p({-align=>"center"},$q->submit({-name=>"Continue"}));
print $q->end_form;
# DISCONNECT
$dbh->disconnect;