perl pagenation
Just like to seek some conceptial help on setting pages on a html.
suppose the data is in the following array
@array=qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28);
how to print a page, after parsing data from @array as
first page
1
2
3
4
5
6
7
8
9
10
1 2 3<--------------------hyperlink to page 1,2, or 3 with 10 items in each page(just like diplay in guestbook)
Thanks for any response to a newbie...
Wil posted this at 16:10 — 8th January 2002.
They have: 601 posts
Joined: Nov 2001
Sorry, I don't fully understand what your question is here. Well I do, but the data you provide is a bit vague.
Why would you have an array loaded with page numbers? Surely you would have an array with results (if this is a search?).
Are you asking for the logic or some sample code?
If you're running a SQL server then you'll enjoy the benefits of the LIMI clause which will give you a lot less headaches. It should save you a lot of coding time, because you only need to work out your start and end positions and grab the information from the database.
- wil
uatt posted this at 16:39 — 8th January 2002.
They have: 27 posts
Joined: Jan 2002
Yes I am asking some sample on displaying the data to a html page
@array is the result whether it is from sql or flat database...
I present 1.2... as the contents of content1 content2 ..
Just make it simple.
quick response..thanks..
Wil posted this at 17:20 — 8th January 2002.
They have: 601 posts
Joined: Nov 2001
OK. I'm presuming prior knowledge of Perl here (as that's what I code in and assume you are too ).
$number_of_results = (@results);
$pgsz = 10; # change this to the number of pages you want
# displayed. Or pass this along to your script
# as an option.
if ($result_count != 0) {
$pagecount = int($result_count / $pgsz);
if (($pagecount * $pgsz) != $result_count) {
$pagecount++;
}
}
if (!$q->param(rqpg)) {
$rqpg = 1;
}
else {
$rqpg = $q->param(rqpg);
}
$firstresult = (($rqpg - 1) * $pgsz) + 1;
$lastresult = $firstresult + $pgsz - 1;
if ($lastresult > $result_count) {
$lastresult = $result_count;
}
$prev_page = $rqpg - 1;
$next_page = $rqpg + 1;
if ($rqpg == 1) {
$prev_link = "";
} else {
$prev_link = " <a href=\"/script.cgi?pg=$q->param(pg)&rqpg=$prev_page&pgsz=$pgsz\">" . "Previous Page" . "</a> ";
}
if ($rqpg == $pagecount) {
$next_link = "";
} else {
$next_link = " <a href=\"/script.cgi?pg=$q->param(pg)&rqpg=$next_page&pgsz=$pgsz\">" . "Next Page" . "</a>";
}
##########
# And if you want to generate page numbers
##########
if ($pagecount > 1) {
$pagelinks = $prev_link;
$pageno = 0;
while ($pageno < $pagecount) {
$pageno++;
if ($pageno == $rqpg) {
$thislink = " <b>$pageno</b> ";
} else {
$thislink = " <a href=\"/script.cgi?pg=$q->param(pg)&rqpg=$pageno&pgsz=$pgsz\">" . $pageno . "</a> ";
}
$pagelinks = $pagelinks . $thislink;
}
$pagelinks = $pagelinks . " " . $next_link;
} else {
$pagelinks = "";
}
###############
# Then to display the results for the page you're on
###############
foreach $found (@results[$firstresult .. $lastresult]) {
###### your display routine goes in here ######
}
# And your page links can be printed out easily as they've
# all been pushed into $pagelinks.
print $pagelinks;
Hope this helps.
- wil
uatt posted this at 22:43 — 8th January 2002.
They have: 27 posts
Joined: Jan 2002
Yes this is what I want...
I think php would do the same way as perl except the treatment of $q->param(rqpg)
Thank you and this may be the simplest tutorial for most perl novice..
Wil posted this at 09:47 — 9th January 2002.
They have: 601 posts
Joined: Nov 2001
Yes, that's just the way I call a module to import all env variables passed to the script.
It shoudln't take too much to convert the concept to a PHP script, not sure how much code alterations are needed.
- wil
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.