English is fine.
I have written some code below but havent tested it, so take it with a pinch of salt.
I would say that the best way of paginating is to control the records displayed by LIMITing the SQl.
Use the following process:
- determine how many records to show in each page, i.e. 20
- do a count(*) for the statement that you require to determine how many records are in the database.
- do ceil(num_results / rows_per_page) to determine the amount of pages
- create a seperate loop for the number of pages in your template.
- perform your SQL statement with the correct limit clause on the end to limit the records returned to those required.
?php
// stuff to connect to mysql
// ...
// set default variables
rows_per_page = 20;
if empty_GETpage || is_int_GETpage _GETpage = 1;
// get number of records in query
num_reqults_query = mysql_querySELECT COUNT* FROM people;
num_results = mysql_resultnum_reqults_query, 0;
// following sets current page to the maximum possible amount in case
// someone changes page number by hand in adress bar
if _GETpage-1 * rows_per_page+1 num_results _GETpage = ceilnum_results / rows_per_page;
// how many pages? where to start and stop limit
num_pages = ceilnum_results / rows_per_page;
limit_start = rows_per_page * _GETpage-1;
limit_stop = rows_per_page;
// and now we get the relevant records
results_query = mysql_querySELECT id, first_name, last_name FROM people LIMIT .limit_start.,.limit_stop;
// now for the template stuff
// first we set the loop for the page numbers
tmpl-newLooppages;
for page=1; page = num_pages; page++
{
tmpl-addRowarraypage = page,
is_current = page == _GETpage ? 1 0
;
}
tmpl-addLoop;
// now we add the records
tmpl-newLooppeople;
while row = mysql_fetch_arrayresult_query, MYSQL_ASSOC
{
tmpl-addRow row;
}
tmpl-addLoop;
// do rest of tmpl stuff
?
Template
table
tr
thID/th
thFirst Name/th
thLast Name/th
/tr
tmpl_loop name=people
tr
tdtmpl_var name=id //td
tdtmpl_var name=first_name //td
tdtmpl_var name=last_name //td
/tr
/tmpl_loop
-- pages --
tr
td colspan=3
tmpl_loop name=pages
tmpl_if name=is_current
tmpl_var name=page /
tmpl_else /
a href=?page=tmpl_var name=page /
tmpl_var name=page /
/a
/tmpl_if
/tmpl_loop
/td
/tr
/table
Let me know if this helps or if something is confusing.
Regards,
Kelvin