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

Greetings, I have this script.. http://www.straightupgoods.com/cgi/dl/text.txt It basically alphabatizes the files found within the database and prints them out accordingly, ie index.cgi?ab, would print out all titles found that started with A or B alphabetically, well what my problem is and I refer you to http://www.straightupgoods.com/cgi/dl/index.cgi?cde which has the most records currently, I want to span those records over multiple pages.. So i'd like to print say 10 per page then have a link saying 'Next 10' I've got through multiple tutorials and message boards and still cant figure it out, if all you perl munk gods out there could give me a hand in performing such a task it would be very much apprechiated!!! Thanks alot! - Elliot Ness

Replies are listed 'Best First'.
Re: Next 10....
by Fastolfe (Vicar) on Nov 02, 2000 at 08:55 UTC
    If you're pulling against a competant database, use the LIMIT SQL key word to limit your matches to a range. I believe you can specify a starting point and a number of records to retrieve. So, modify your CGI to accept an additional argument or two (say, 'start' and 'number'), and on the page, if there are additional records to retrieve, provide a 'next' link with a 'start' value of start+number. Your SQL statement would then look something like this:
    $sth = $dbh->prepare('SELECT ... FROM ... WHERE ... LIMIT ?,?') or die + "..."; $sth->execute(..., param('start') || 0, param('number') || 10) or die +"...";
    Make sense?

    If your SQL database doesn't let you limit queries like that, have it pull everything into, say, an array, and just deal with an array slice that represents the information you want:

    my $start = param('start') || 0; my $num = param('number') || 10; @results = $sth->execute(...); @actual = @results[$start..($start+$num)]; # limit them my $more_to_do = 1 if defined $results[$start + $num + 1]; my $less_to_do = 1 if $start; print_previous_link if $less_to_do; print_results(@actual); print_next_link if $more_to_do;
Re: Next 10....
by chromatic (Archbishop) on Nov 02, 2000 at 09:17 UTC
      While that's nice and functional, and please correct me if I'm wrong, but that probably doesn't involve any cacheing. Since finding matches 400-410 involves finding the first 400 matches and discarding them, i would imagine that the DBI cacheing mechanism as well as CGI::Cache might be useful here, minimally. Unless I'm wrong about the cacheing, i might consider a stronger cacheing system. Perhaps when a user extracts a few from the beginning, the system-persistent CGI can do some forward-cacheing, expecting the user to submit for some more results. Just a thought on intelligent and efficient programming...
      AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
        Caching introduces additional constraints. What if someone updates a record in the database and the cache isn't updated at the same time? What if more than one process accesses the database at once? The odds of that may be low, but they increase as site traffic increases (and webmasters are all for that!).

        That's presuming the original poster moves to a persistent CGI environment -- if each script invocation is a separate process, caching won't help at all. A good database will implement its own caching scheme, whether it's sorting results or keeping track of common queries.

        Besides that, keeping the cache around might eat up a lot of memory which could be used to spawn more processes. And anyway, if performance is such a big deal, replacing the tied hash with a real relational database (supporting LIMIT) will probably give the biggest improvement, aside from Fast::CGI or mod_perl.

Re: Next 10....
by eduardo (Curate) on Nov 02, 2000 at 12:14 UTC
    a good place to start looking might be merlyn's awsome WebTechniques columns (look on his home node to find the link...) and i would start looking for ideas with this one...
Re: Next 10....
by Anonymous Monk on Nov 03, 2000 at 01:28 UTC
    Greetings once again, thanks for everyones reply.. In reply to fastolfe, sorry for the newbie status but could you direct me as to where I would put such a code? I blieve it was the second example since this isn't a MySQL drivin database, just a flatfile .db database.. Thanks!