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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show only 20 contribution

Status
Not open for further replies.

grindange

Programmer
Jul 19, 2004
18
SE
Have a sort of guestbook made with CGI, HTML and MySQL. Now I want to just show 20 contributons at each webpage, as in this forum. I want the users to press on a link to see next twenty. That is very common on many web pages, someone who have a solution for this??
 
Depending on your database you could use the limit function.

If its text file based
Code:
sub show_guestbook {
  ($limit, $current, $prev, $next)=@_;
  open FILE, "<gbook.txt";
  $loop=0;
  while (<FILE>) {
    $loop++;
    if ($loop >= $current) && ($loop < $next) {
      &print_record;
    }
  }
  print "<a href=/cgi-bin/this.pl?start=$prev"><<prev</a> || current $current/$limit || >><a href="/cgi-bin/this.pl">next</a>";
}
Something like the above would do
--Paul

cigless ...
 
And you'll need to use the 'count' function in MySQL to determine how many records you have. That way you can calculate how many pages you need, or how many times to show the 'next' link. Each 'next' link will need to pass along a parameter telling which page to show. You'll then use that param in your limit statement to get the correct page of records.

 
Very simply:-

Lets say you have 186 records and you want to show 20 per page...

You need to know how many links to offer - similar to this site

int ( total records (186) / records per page (20) = 9 )

Modulo can be used to calculate the remainder...

186 % (modulo) 20 = 6

Maybe you could use a query string - ?page=4 - and work from there

i.e. 20 x 4 = 80 - so you would need to display the appropriate records


Kind Regards
Duncan
 
This is accomplished through the use of SQL... Here is what your query will look like:

my $string = "select * from `table_name` order by `id` limit 0,19"

select * means select all the fields of table_name
the "order by" param ensures record 1-20 is always the same records

The magic is in the "limit 0,19. This will only return the first 20 records.

Now to piece it together with links:

<a href="script.cgi?offset="0">

use the CGI.pm module to get the value of "offset"
Code:
use CGI; #Declare the use of CGI.pm
my $GET=new CGI; #Assign the call to the CGI.pm
my $offset=$GET->param('offset'); #Get the offset
my $higer = $offset + 20; #Add 20 to the offset

###Connect to your database etc...###

###prepare the string###
my $string = "select * from `table_name` order by `id` limit $offset , $higher"

###Execute the script and print the results###

I didn't test anything but it should work.

 
Yes, I forgot some ";" and missplaced some -->"<-- but whatever...

P.s.

You can say "limit 20 , 39" or even "limit 6 , 8"

I think this is the best method, no extra scripting, no extra moduel (You probably allready use the CGI.pm) and no extra data in your array from a query that returns more than you need. Try it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top