use CGI; use CGI::Carp qw (fatalsToBrowser); use DBI; use JSON; use Template; use Env; use strict; use warnings; use vars qw ($q $tt $dbh); use lib '.';
init(); grid_data() if ($q->param('mode') and $q->param('mode') eq 'grid_data'); main();
sub main {
my $vars = { title => 'Flexigrid Perl Demo', script_name => $ENV{SCRIPT_NAME}, };
print $q->header('text/html'); $tt->process('template_flexigrid.htm', $vars) || die $tt->error(), "\n"; exit;
}
sub grid_data {
my @rows; my @bind;
my $where = '';
my $query = $q->param('query') || ''; my $qtype = $q->param('qtype') || ''; my $page = $q->param('page'); my $limit = $q->param('rp'); my $sort = $q->param('sortname'); my $order = $q->param('sortorder');
my $offset = ($page -1) * $limit;
if ($query) { $where = "where $qtype like ?"; $query = '%' . $query . '%'; push @bind, $query; }
my $sql = qq~select SQL_CALC_FOUND_ROWS
id, iso, name, printable_name, iso3, numcode
from country
$where
order by $sort $order
limit $offset,$limit ~;
my $h = $dbh->prepare($sql) or die $DBI::errstr; $h->execute(@bind) or die $DBI::errstr;
my %r; my @c = @{ $h->{NAME_lc} };
$h->bind_columns( \( @r{ @c } ));
while ($h->fetch) {
my @cell;
$r{default} = ($r{iso} eq 'AR') ? 1 : 0;
foreach (qw (iso name printable_name iso3 numcode default)) { push @cell, $r{$_} }
push @rows, { id => $r{id}, cell => [ @cell ] };
} $h->finish;
my $result = { page => $page, total => _total(), rows => [ @rows ] };
my $h = $dbh->prepare('SELECT FOUND_ROWS() as total'); $h->execute; my $row = $h->fetchrow_hashref;
return $row->{total};
}
sub init {
$q = new CGI;
unless (defined $tt) {
$tt = new Template({ INCLUDE_PATH => $ENV{DOCUMENT_ROOT} . './', INTERPOLATE => 0, }) or die "$Template::ERROR\n";
}
unless (defined $dbh) {
$dbh = DBI->connect('DBI:mysql:database=test;host=', 'test', 'test', { RaiseError => 0, PrintError => 1, AutoCommit => 1 }) or die $DBI::errstr;
}
}
END {
$dbh->disconnect
}
template_flexigrid.htm ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>
>clairvyさん
教えていただいた、URLでアクセスしたら。
<h1>Software error:</h1>
<pre>Undefined subroutine &main::to_json called at************grid.pl line 117.
</pre>
<p>
giving this error message
and the time and date of the error.
エラーになりました。
ご指摘通り、JSON形式で返す箇所に、問題があるのですね。