CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How you display results per page using MySQL and HTML::Template

 



hwnd
User

Mar 2, 2013, 11:43 PM

Post #1 of 3 (637 views)
How you display results per page using MySQL and HTML::Template Can't Post

I figured I would share a way to show results per page querying from a MySQL table using HTML::Template. Please feel free to comment or add your opinion on changes that could be made or simplified.


Code
  

#!/usr/bin/perl -T

use strict;
use warnings FATAL => 'all';
use DBI;
use CGI qw( :standard );
use CGI::Carp qw( fatalsToBrowser );
use HTML::Template;


my $page_id = param('id');
$page_id = 1 if !defined ( $page_id ) ||
$page_id !~ /^\d+$/ || $page_id < 1;

my $per_page = param('use');
$per_page = 5 if !defined ( $per_page ) ||
$per_page !~ /^\d+$/ || $per_page < 1;

my $tmpl = HTML::Template->new(filename => 'html.tmpl');

my $dbh = DBI->connect('DBI:mysql:database:hostname',
'**********', '**********') or die $DBI::errstr;

my $query = sprintf ('SELECT id, date, headline, display FROM news
ORDER BY id LIMIT %d, %d', $page_id - 1, $per_page + 1);

my $sth = $dbh->selectall_arrayref ( $query );
$dbh->disconnect();

my @rows;
for (my $i = 0; $i < $per_page && $i < @{$sth}; $i++)
{
my @cols = @{$sth->[$i]};
my %row = (
id => $cols[0],
date => $cols[1],
headline => $cols[2],
news => $cols[3],
);
push @rows, \%row;
}

my $back_link = ( $page_id > 1 ) ?
a({-href=> url() . '?id=' .
( $page_id - $per_page ) . ';use=' .
$per_page}, 'Back') . " | " : "Back | ";

my $next_link = ( @{$sth} > $per_page ) ?
a({-href=> url() . '?id=' .
( $page_id + $per_page ) . ';use=' .
$per_page}, 'Next') : "Next";

$tmpl->param(
PREV => $back_link,
NEXT => $next_link,
DB_RECORDS => \@rows,
);

print header, $tmpl->output;



wickedxter
User

Mar 4, 2013, 5:40 PM

Post #2 of 3 (608 views)
Re: [hwnd] How you display results per page using MySQL and HTML::Template [In reply to] Can't Post

Ever though of useing a web framework, like Dancer, Mojo or catalyst?


hwnd
User

Mar 4, 2013, 6:03 PM

Post #3 of 3 (604 views)
Re: [wickedxter] How you display results per page using MySQL and HTML::Template [In reply to] Can't Post

Hmm. Honestly has never crossed my mind. What's beneficial of using those?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives