Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: last 10 ... next 10?
by alfie (Pilgrim) on Mar 28, 2001 at 11:13 UTC | |
This has also the advantage that it saves lots of memory and is quite possibly faster. If you want to do it in perl and/or don't know if your database supports the LIMIT selection, you should try with slices of the aray: This gives you the wanted elements. Don't forget that you start counting with 0 for the first element (so this returns the 4th to the 6th element). Thanks to alakaboo and his great explenation in this note :-)
Until you don't go further into
detail about the layout of your database or how you proceed
your data within your perl script I can't offer better
answer (maybe other more enlighened monks can ;)
| [reply] [d/l] |
|
(dkubb) Re: (2) last 10 ... next 10?
by dkubb (Deacon) on Mar 28, 2001 at 11:30 UTC | |
I assume you are fetching the results from a database using DBI, in which case you should take alfie's advice, and look into the LIMIT command, if your database supports it. However, most have something similar in concept. As for displaying the HTML results, you can combine HTML::Template and HTML::Pager to get the results you want. HTML::Pager was designed for exactly the purpose you describe, and is quite slick. It abstracts alot of the details for maintaining state across each script execution. | [reply] [d/l] |
|
Re: last 10 ... next 10?
by epoptai (Curate) on Mar 28, 2001 at 22:33 UTC | |
First set $start to one unless we're getting a param. Then use two counters to track where to start and how many to process. Then calculate the 'previous n' and 'next n' links.
| [reply] [d/l] [select] |
|
Re: last 10 ... next 10?
by Beatnik (Parson) on Mar 28, 2001 at 14:20 UTC | |
Greetz Beatnik ... Quidquid perl dictum sit, altum viditur. | [reply] |
|
Re: last 10 ... next 10?
by knobunc (Pilgrim) on Mar 28, 2001 at 19:00 UTC | |
If your database does not support LIMIT or an equivalent to allow you to retrieve the appropriate number of rows at an offset then you can do the following:
Below this is a lot of Mason specific blabbering. If you feel like decoding my feverish screed about how I hook the above up to the HTML that the user sees then keep going, otherwise there is probably not much else. Since I am using Mason I wrote a component to handle tables that I pass the parameters for the number of rows to display and the offset to start at and then generate widgets in the html to allow the user to move forward or backward through the dataset generating the correct URL with the appropriate offset and count plugged in. The component is smart enough to know when it has hit the beginning and does not put up a widget to allow you to scroll further backwards, and similarly at the end. To detect the end, it actually cheats, I request one more row than I want and the table component uses that ti detect if there is more data. The table component does not display the additional marker row. In order to allow movement any page that wants to support this has to have 2 page arguments that match the ones that the table component knows how to generate so that it can run the correct subroutine that executes the DB query and pass in the appropriate offset and count parameters. I actually added a further wrinkle by allowing users to click on the column headings in the tables to sort the rows (or reverse sort if they have already sorted by that row). In order to do this I added a third parameter for sorting and the table component generates little up/down arrows to show which column is sorted. The table component then generates link tags for each column heading that tells the page what the appropriate sort is. This then gets passed into the query which validates the sort and turns it into a SQL order by clause. The following is the full blown beast.
| [reply] [d/l] [select] |
|
Re: last 10 ... next 10?
by busunsl (Vicar) on Mar 28, 2001 at 11:06 UTC | |
The script has to provide the links for last/next, which will contain the pointer, which again is processed by your script. | [reply] |