views:

30

answers:

1

I am going to be implementing a small custom CMS for a website. I was wondering if there are any more popular methods for implementing pagination using OOP. Thanks!

+1  A: 

OOP is very general question. You can design your CMS in a hundred OOP and non-OOP ways. Well, this is the common scenario, how pagination works:

In your controller:

A. Extract total count of rows in the table

list($total_rowcount) = $db->query('SELECT count(*) FROM table WHERE ...");

B. Define total page count (in the table)

$page_count = ceil($total_rowcount / $rowcount_per_page);

C. Extract rows for the current selected page

$start_element = $page * $rowcount_per_page;

$end_element = $start_element + $rowcount_per_page;

$rows = $db->query('SELECT count(*) FROM table WHERE ... LIMIT " . $start_element . ',' . $end_element );

In the template

D. show all elements

$rowcount_per_page = count($rows);

for ($i = 0; $i < $rowcount_per_page; $i++)

{ show( $rows[$i] ); }

E. Show all page links

for ($i = 0; $i < $page_count; $i++)

{ echo ($i == $page ? $i : '<a href="?page='.$i.'">'.$i.'</a>'); }

That's it.

topright