Forum Moderators: coopster & phranque

Message Too Old, No Replies

(perl) cut off - How make click next page >>

(perl) cut off - How make click next page >>

         

StopSpam

9:19 am on Jul 1, 2003 (gmt 0)

10+ Year Member



(perl) cut off - How make click next page >>

i got a script that reads data.
the data file contains 500 names is is way to mushto load and show all in one page .

so iwant to cut it of at 50 and then make a link to next page. anyone a idea how to do this ...

so after50 names i get clickhere for page 2 3 4 next ect

please post comments and url to page withmore info on how to make this

Thx ;-)

gsx

9:36 am on Jul 1, 2003 (gmt 0)

10+ Year Member



You need at least on variable - page number (or start position). Possible two, number of results:

So for page one, show.php?number=50&start=0, for page two use show.php?number=50&start=50

You need to write the code to use the variables to start and stop and the relevant parts. An easy but slow way to do this is to put a record number counter in the code. So set counter=0 and add one each time you go past a record. Only print those that are between the 'start' variable and the 'start+number' calculation. This is not the most efficient way, but it is one of the simplest.

Then you need to print at the bottom of the page the 1, 2, 3, 4... which is simple to do - you start at 1 and go to (total number of results)/(number) - but round this up, not to nearest. The start variable is easy for each (number displayed - 1)*(number).

StopSpam

9:54 am on Jul 1, 2003 (gmt 0)

10+ Year Member



your sample is php i use perl script ..

soyou mean read first 1-50 names of data file
then read only 51-100 ect..

yeh thats what i had in mind to .. but its still complex to make

gsx

11:58 am on Jul 1, 2003 (gmt 0)

10+ Year Member



Sorry, perl/php/asp whichever, the principle is the same.

I don't mean read just some in, read the whole file (as you are now), but only OUTPUT the relevant ones (believe or not it seem quicker):

Assuming $number and $start are the variables:

$counter = 0;
foreach $record (@records)
{
if($counter >= $start and $counter <= $start+$number)
{
# PRINT THIS RECORD
print $record;
}
else
{
# DONT DO ANYTHING HERE - WE ARE OUT OF THE REQUESTED RANGE
}
$counter = $counter + 1;
}

StopSpam

12:07 pm on Jul 1, 2003 (gmt 0)

10+ Year Member



hi thx ... this makes sense to me ;-)

so start would then be :

$start = '50'; # then cuts list at 50 names

then it reads only first 50 right?
how do i call the script to read the next 50 and the next?

ewanfisher

11:48 pm on Jul 2, 2003 (gmt 0)

10+ Year Member



Try this script out. Make sure you change the path to perl so that it is the correct path to perl on your machine, and change the location of the input file.

#!/usr/bin/perl
use CGI;
my $q = new CGI;

$start = 0; # sets the start number if its the first time you access the script.
$start = $q->param("start");
$number = "50"; # change this to however many you want per page.
$finish = $start + $number;

print $q->header; # prints the HTML headers

# change the next line to the location of the file.
open (INPUT, "/home/ewan/names.dat");
@records = <INPUT>;
close(INPUT);

$counter = 0;
foreach $record (@records) {
if($counter >= $start and $counter <= $finish) {
# PRINT THIS RECORD
print "$record<BR>";
} else {
# DONT DO ANYTHING HERE - WE ARE OUT OF THE REQUESTED RANGE
}
$counter = $counter + 1;
}

print "<A href="?start=$finish">Next</A>";

StopSpam

10:23 am on Jul 3, 2003 (gmt 0)

10+ Year Member



Thank you all for replying ...

the last code works great ....
it has small 500 error in it

print "<A href="?start=$finish">Next</A>";

must be with 2x \

print "<A href=\"?start=$finish\">Next</A>";

this works very nice i go try builth it in the script

thx to all ;-)

StopSpam

10:36 am on Jul 3, 2003 (gmt 0)

10+ Year Member



the:

use CGI;
my $q = new CGI;

is this compatable with all unix and linux servers
red head open bsd free bsd and so on?

ewanfisher

1:20 pm on Jul 10, 2003 (gmt 0)

10+ Year Member



It uses the CGI.pm module. But thats all I can comment for sure.