469,271 Members | 981 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Paging with Html and data base.

Dear Group.

I am new to perl and cgi scripting. I am currently making an interface for my application. One of the module, the data from database is to be displayed in paging. I mean, If I have 100 records, I need to diplay each in a set of 10 element in 10 page each.

eg: as we search in the google.com search page.

Thanks in advance.

Regards
Shyam
Feb 6 '07 #1
7 4583
KevinADC
4,059 Expert 2GB
two possibilites:

http://search.cpan.org/~lbrocard/Dat...b/Data/Page.pm
http://search.cpan.org/~llap/Data-Pa...ata/Pageset.pm

there are probably more too:

http://search.cpan.org/search?m=all&q=paging

or try and code a paging routine yourself.
Feb 6 '07 #2
Thank U kevin.

I checked it, as a newbie I have some problem with the code,have you got a piece of code,that connect to the mysql database and display the data in paging. if so can u send it to me.....
Feb 7 '07 #3
KevinADC
4,059 Expert 2GB
Thank U kevin.

I checked it, as a newbie I have some problem with the code,have you got a piece of code,that connect to the mysql database and display the data in paging. if so can u send it to me.....

What problems do you have? Did you try anything? This is going to be a learning process, not a "give you the code" process. I don't have code to give you anyway.

Is this some type of school work?
Feb 7 '07 #4
What problems do you have? Did you try anything? This is going to be a learning process, not a "give you the code" process. I don't have code to give you anyway.

Is this some type of school work?
No, it is not a school work!

I tried to use the use HTML::Pager;
It generate an demo page which display the time.

But I dont know where to call the data base query, I made the querry to get the total number of records so that that can get the , rows => $max_number,

but I dont know how to fetch the data sets from database. is there any methords for that? what argument is need to pass to prepare the sql statement
so that the result will be displayed in table format.

Regards
Shyam
Feb 7 '07 #5
KevinADC
4,059 Expert 2GB
No, it is not a school work!

I tried to use the use HTML::Pager;
It generate an demo page which display the time.

But I dont know where to call the data base query, I made the querry to get the total number of records so that that can get the , rows => $max_number,

but I dont know how to fetch the data sets from database. is there any methords for that? what argument is need to pass to prepare the sql statement
so that the result will be displayed in table format.

Regards
Shyam
Sorry, but my SQL knowledge fits on the head of a pin.
Feb 7 '07 #6
Hi.
paste the code below in a .pl file and run. then modify the array filling part to fill the array with your database fields.

Prakash


#!/usr/bin/perl -w
use CGI;



my $query = new CGI;
my $bornum;
my $itemnum;
my $i;

my @overduedata;


my $pageNum =$query->param('pageNum');
unless($pageNum) {
$pageNum=0;}

my $offset=$query->param('offset');
unless($offset) {
$offset=10;}



#filling the array with 100 records
# you ought to be using fetcharray_hashref or something like that to fetch your records in your array
#note, you will be fetching all records everytime you click on next page, but display only 1-10, or 11-20 etc
$i=0;
my $numOfRec = 100;
while ($i < $numOfRec){
$bornum = "bornum" . $i;
$itemnum = "itmnum" . $i;

push (@overduedata, { bornum => $bornum, itemnum => $itemnum });
$i = $i + 1;
}


#put the headers
print "Content-Type: text/html\n\n";
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";

print "\n<form>";

print "\nNo: of records per page : <input type=text name=offset>";
print "\n<input type=submit value=submit>";
print "\n</form>";

print "\n<br> No: of rec per page = " . $offset . " -- pageNum = " . $pageNum ;
print "<table border=1>";
#now display the array
my $startDisplay = ($pageNum) * $offset;
my $endDisplay = ($pageNum + 1) * $offset;

$i = $startDisplay;


while ($i < $endDisplay){
print "<tr><td>" . $i . "</td><td>" . $overduedata[$i]->{'bornum'} . "</td><td>" . $overduedata[$i]->{'itemnum'} . "</td></tr>";
$i = $i + 1;
}
print "</table>";


#now display link table
my $numofPages = $numOfRec / $offset;
$i = 0;

print "<table border=1><tr>";
while ($i < $numofPages){
print "<td> <a href = ?pageNum=" . $i . "&offset=" . $offset . ">" . $i . "</a></td>";
$i = $i + 1;
}
print "<tr></table>";
Feb 8 '07 #7
Hi.
paste the code below in a .pl file and run. then modify the array filling part to fill the array with your database fields.
Prakash
Thank You Prakash.

I also made a script. please do check the same.

Thanks and Regards

Shyam

#!/usr/bin/perl -w


use CGI::Carp qw(fatalsToBrowser);
use DBI;
use CGI qw/:standard/;
use DBI qw/:sql_types/;
use CGI;
use POSIX;
$|++;


my $env=$ENV{QUERY_STRING};
my@env=split(/=/, $env);
my $page= $env[1];


my ($results_per_page);
if (!$limit) {$limit = 0;}


my $dbname = 'DBI:mysql:db:localhost';
my $dbusername = 'root';
my $dbpassword = '';



my $dbh2 = DBI->connect($dbname, $dbusername, $dbpassword)
or die ("Connection to database failed.\n");
my $sql = "SELECT id,uname,maxdom FROM tb_user ";
my $sth2 = $dbh2->prepare($sql) or die("Error in SQL\n");

$sth2->execute() or die ("Error in SQL\n");
my $results = $sth2->rows;
$results_per_page = 3;
my $pagesrequired = ceil($results / $results_per_page);


print "Result Page ";

for (my $i = 0; $i <= $pagesrequired -1; $i++) {

if ($i == 0) {
if ($limit == 0) {
print "<a href=\"home.pl?users&limit=0\">";
print $i + 1;
print "</a>";
}
else {
print $i + 1;
}
}

if ($i > 0) {
if ($limit != ($i * $results_per_page)) {
print " | <a href=\"home.pl?users&limit=";
print ($i * $results_per_page);
print "\">\n";
print $i + 1, "</a>";
}
else {
print " | ", $i + 1;
}
}
}



return $t;
}







my $dbh = DBI->connect("DBI:mysql:db:localhost", "root", "")
or die "Couldn't connect to database: " . DBI->errstr;


my $sth = $dbh->prepare("SELECT id,uname,maxdom FROM tb_user order by maxdom limit ?,? ")
or die "Couldn't prepare statement: " . $dbh->errstr;

$sth->bind_param(1, undef,SQL_INTEGER);
$sth->bind_param(2, undef,SQL_INTEGER);
$sth->execute($page,$results_per_page)
or die "Cannot Execute $DBI::errstr";





print start_html('USER LIST'),
table({-border=>'1',width=>'100%'}),

Tr({-align=>'CENTER', -valign=>'TOP'},
[
th( ["User", "Domain","Action"] )
]);

while (my ($id,$uname,$maxdom) = $sth->fetchrow_array()) {

if ($uname eq "") {

$uname="UNDEF";

}

print Tr ({-align=>'CENTER',},
[td(['<a href=?editid='.$id.'>'.$uname.'</a>',$maxdom,'<a href=?deleteid='.$id.'>Del</a>'])

]);


}


print end_html;


1;
Feb 9 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Dan Nash | last post: by
3 posts views Thread by Marc Grutte | last post: by
reply views Thread by Eugene Anthony | last post: by
reply views Thread by =?Utf-8?B?V0I=?= | last post: by
reply views Thread by Don Miller | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.