Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Understanding X number of results per page.

Status
Not open for further replies.

rdyoll

Technical User
Aug 11, 2003
39
US
Hi, I'm trying to get the logic down for showing X amount of results per page...as in a Guestbook or similar application. I understand how to have the results print on the page, but setting a limit, and creating the link for the remaining results to be displayed on the next page is driving me nuts. Can someone try to explain the logic on how this is done.
 
Code:
# here's some example code:

use CGI qw/:standard/;

# btw, this is just sample code and you should
# use taint

# Two variables are key, they define what the result set
# will look like.

$current_start_number = param('start_at') || 0; 
$how_many             = param('how_many') || 10;

# $current_start_number tells the script where in 
# the result set we are (which record), and 
# $how_many tells the script how many records we are 
# displaying per page.

$next_start_number = $start_number + $how_many;
$prev_start_number = (($start_number - $how_many) > 0) ?
                     $start_number - $how_many : 0;

# now, your result set for any given page will be defined
# by those two first variables, and the links to "go back"
# or "go forward" are easily defineable with the 
# $next_start_number variable and it's counterpart, 
# $prev_start_number.


@result_set = map { "result number $_\n" }, (1..123);

for($x = 0; $x <= $how_many; $x++){
  
  print @result_set[$current_start_number + $x];

  }

print &quot;<A HREF='...?start_at=$next_start_number&how_many=$how_many'>
NEXT
</a>&quot;;

print &quot;<A HREF='...?start_at=$prev_start_number&how_many=$how_many'>
PREVIOUS
</a>&quot;;

This is just one way to do it. TMTOWTDI.

Hope this helps.

--jim
 
Thank you very much. I'll study the logic. Cheers!
 
I ran the little example script on my localhostserver/perl setup under taint, no prob, except using (fatalsToBrowser)I had to remove a comma at the position shown below:
@result_set = map { &quot;result number $_\n&quot; }, (1..123);
****************************here ^

Anyway, thank you for a great explanation. Now, I'm having a little difficulty in placing that information into an actual script I wrote. When opening the datafile I have something like this:

open (DATA, &quot;$db&quot;);
while (<DATA>) {
chomp;
($this,$that,$other) = split(/\|/,$_,3);
print &quot;<tr><td>$this</td><td>$that</td><td>$other</td></tr>\n&quot;;
}
close (DATA);

My query is how or where do I put these lines:
@result_set = map { &quot;result number $_\n&quot; }, (1..123);

for($x = 0; $x <= $how_many; $x++){

print @result_set[$current_start_number + $x];

}

Do I just open the DATA file and read like this:
open (DATA, &quot;$db&quot;);
chomp;
($this,$that,$other) = split(/\|/,$_,3);
@result_set = map {&quot;<tr><td>$this</td><td>$that</td><td>$other</td></tr>\n&quot;}(1..123);
for($x = 0; $x <= $how_many; $x++){

print @result_set[$current_start_number + $x];

}
close (DATA);

I've tried many different combinations, and I'm getting very close, but no cigar, yet.
 
use a link to display the guestbook
<a href=&quot;guestbook.cgi?page=0&quot;>see my guestbook</a>

###the cgi####
use strict;
use CGI ':standard';

my $pagecount;
$pagecount = param('page');
$guests_to_display;

open(DATA, &quot;$the_file&quot;);
@eachguest = <DATA>;
close(DATA);

print &quot;Content-type: text/html\n&quot;;
for ($count = $pagecount; $count < ($guests_to_display + $pagecount); $count++){

<print guest data>
}

print &quot;click here to see <a href=\&quot;guestbook.cgi?page=&quot;;
$show_next = ($guests_to_display + $pagecount);
print &quot;$show_next&quot;;
print &quot;\&quot;>more....</a>\n&quot;;
 
I finally got it! Thank you both, Coderifous & arcnon ...[thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top