Click to See Complete Forum and Search --> : multiple pages in a script


etheracide
12-11-2002, 05:52 PM
I am writing several scripts which would all benefit from the automatic creation of multiple pages. The number of 'posts' per page will usually be set by the admin and saved to the admin settings database. This number will be extracted by the script to decide how many posts to put per page and also how many pages are created in total.

I have tried many things. I have posted in other places and tried all the returned suggestions. Nothing has worked yet. If you have any suggestions, or would even except my short source code and add the necessary code..it would be GREATLY appreciated!

You will be given credit for adding the functionality in the read-me's, faq's, and on the pages of my site where they are offered for download.

Help on one file should allow me to implement the feature throughout all of my scripts.

Thank you in advance for any help you can provide.

jeffmott
12-11-2002, 07:40 PM
I don't know the specifics of your code so this is somewhat generalized and probably shouldn't be copied straight into your program.

use CGI 'param';
use POSIX 'ceil';

my $msgs_per_pg = 25;

# param('pg') assumed to hold the current page number being viewed
# @postdb assumed to hold your posts, or references to posts

# print posts in the page range
my $pg = int( abs(param('pg')) || 1 ) - 1;
for ( $pg * $msgs_per_pg .. ($pg + 1) * $msgs_per_pg - 1 ) {
  $_ = $postdb[$_] or last;
# ...
}

# print links to the other pages
if ( ( my $pages = ceil( scalar(@postdb) / $msgs_per_pg ) ) > 1 ) {
&nbsp; print qq` <a href="script.pl?pg=$_">$_</a> ` for 1 .. $pages;
}