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 ;-)
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).
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;
}
#!/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>";