Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a large set of records in my database. I want to display them 50 at a time with next and previous links. What would be the best way to do this?
  • Comment on What's the best way to get the first n rows, the second n rows... from a database?

Replies are listed 'Best First'.
Re: What's the best way to get the first n rows (kudra: get the whole result)
by kudra (Vicar) on May 02, 2000 at 18:25 UTC
    I won't claim this is the best way to do it, but it's a way to do it. Update: I recommend you look at one of the other solutions.

    Query the database, and put all the data in an array (I would probably make each element of the array a struct which contains all the data on a particular record). Then you can simply keep track of your location in the array (1, 49, 99, etc) and make a previous link which passes the current location -50 (if applicable) and a next link which passes the current location +50 (again, if this doesn't exceed the limits of the array). Run a loop that prints out all array entries between your current starting point and 50 more (or the end of the array, whichever comes first). Of course you need to make sure that the information in the array is available to the instance or program that prints it.

    One possible problem with this approach is that your array could contain outdated information if the database is changed frequently. You could re-query the database, but that could mess up the order, unless you are ordering by a sequencial key.

Re: What's the best way to get the first n rows
by BBQ (Curate) on May 02, 2000 at 18:32 UTC
    If you look three nodes down the line you'll find that this questioned has been answered, albeit, using slightly different terminology. Very good replies, and ideas of what not to do also...

    #!/home/bbq/bin/perl
    # Trust no1!
Re: What's the best way to get the first n rows
by toadi (Chaplain) on May 02, 2000 at 18:52 UTC
    The question is : what kind of db?

    SELECT * FROM guests ORDER BY time DESC LIMIT 50

    This is the sql line that's used for getting the first 50

    In cgi I would make from 50 a variable so if somebody's pushes next(50) the variable would add 50.

    Don't know how it's just done in perl. But I connected to the db with php to get what you wanted.

    The best hint I can give is. Search on CPAN for DBI. Look to the perl-syntax how to connect to a db.
    But most important is to know sql-syntax. Only then you can learn how to program databases(in any programming language).
    --
    My opinions may have changed,
    but not the fact that I am right

RE: What's the best way to get the first n rows, the second n rows... from a database?
by gregorovius (Friar) on May 02, 2000 at 22:36 UTC
    Look at the HTML::Embperl and DBIx::Recordset modules. The former will automatically generate HTML tables for your data (among many other things) and the latter provides a very simple way to access the results of your search in chunks of the size you need.

    A really cool thing of using these modules is that the NEXT and PREVIOUS buttons are generated automatically depending of what chunk of your dataset you're browsing.

    Check out the Embperl site for usage examples on both modules.