perl pagenation

They have: 27 posts

Joined: Jan 2002

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...

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? Wink

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

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..

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 Wink).

$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> &nbsp; ";
}
if ($rqpg == $pagecount) {
$next_link = "";
} else {
$next_link = "&nbsp; <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> &nbsp;";
} else {
$thislink = " <a href=\"/script.cgi?pg=$q->param(pg)&rqpg=$pageno&pgsz=$pgsz\">" . $pageno . "</a> &nbsp;";
}
$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

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..

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.