Usually you have some means to "order" the records (e.g., a "id" field that 
is 1,2,3,4,5,  a "name" field that is a,b,c,d, an inserted_on datetime 
field that is 2012-07-23m, 2012-07-24, etc etc etc).

When the order of the records is established in a list:

all_records = ['a','b','c','d',.......]



you can (this is python, not strictly web2py)
>>> all_records[0]
'a'
>>> all_records[-1]
'd'
>>> all_records[2:4]
['c', 'd']

NB: list indexes are 0 indexed (the first record is [0], not [1])
So, back to your example, you'd need on the first page to show records from 
0 to 9, on the second page records from 10 to 19, and so on.

Given that in web2py you can catch (with the previous example) the 
/myapp/mycontroller/myfunction/*thenumberofthepage* with request.args(0), 
still refreshing some math you need to "associate" the number of pages to 
the range you want. This is what you need to calculate:

page 1 : all_records[0:10]
page 2: all_records[10:20]
page 3: all_records[20:30] 

and so on.

page_number = request.args(0)
records_per_page = 10
records_to_show = all_records[(page_number-1)*records_per_page:page_number*
records_per_page]

and that's pretty much about it. There are smarter way, different 
implementations, etc etc but this is the straightforward way to show a 
"universal" method based on a simple calculation.

NB: 
all_records = ['a','b']
all_records[10:20]
>>> []

no exceptions are thrown. If you get an empty list you are "requesting a 
too far page", i.e. a page with a too high *thenumberofthepage*












On Monday, July 23, 2012 10:38:01 PM UTC+2, adohertyd wrote:
>
> Thank you for that makes perfect sense. This might be a stupid question 
> but do I need to create an individual HTML page for each result set? The 
> whole pagination thing is a mystery to me!
>
> On Monday, 23 July 2012 21:33:34 UTC+1, Niphlod wrote:
>>
>> That's math!
>> e.g.
>> records_per_page = 10 records
>> all_records = 137 records
>>
>> pages_needed = all_records / records_per_page (division)
>> last_page = all_records % records_per_page (module)
>>
>> that is 13 "full pages" + 1 page with 7 records.
>>
>> So, you'll have to count the total of your records, divide it by the 
>> number of "records per page" , an voila.
>>
>> If you want urls in the form of (/myapp/mycontroller/myfunction/1, 
>> /myapp/mycontroller/myfunction/2, /myapp/mycontroller/myfunction/3, etc) it 
>> would roughly translate to:
>>
>> all_records = 137
>> records_per_page = 10
>> urls = [URL('myapp', 'mycontroller', 'myfunction', args=[page+1]) forpage 
>> in range(all_records+1)]
>>
>>
>>
>> On Monday, July 23, 2012 10:20:48 PM UTC+2, adohertyd wrote:
>>>
>>> I have an application that returns a list of between 50-100 items. I 
>>> have no database layer it isn't required for my app. I want to display 10 
>>> of those items on a page, and only generate the number of pages needed to 
>>> view those results. Can anyone point me in the right direction (not the 
>>> docs, they don't make any sense to me for this problem). Thank you
>>
>>

-- 



Reply via email to