Inspired by Perl module Data::Page
The main concept is that you pass in the number of total entries, the number of entries per page, and the current page number. You can then call methods to find out how many pages of information there are, and what number the first and last entries on the current page really are.
$datapage = new DataPage(total_entries, entries_per_page, current_page, total_numbers)
$datapage = new DataPage(80, 10, 1, 6);
total_entries get or sets the total number of entries
$datapage->total_entries = 80; // default 0
entries_per_page gets or sets the total number of entries per page
$datapage->entries_per_page = 20; // default 10
current_page gets or sets the current page number
$datapage->current_page = 2; // default 1
total_numbers gets or sets the total numbers numeration
$datapage->total_numbers = 6; // default 10
entries_on_this_page This methods returns the number of entries on the current page
echo $datapage->entries_on_this_page();
first_page This method returns the first page. This is put in for reasons of symmetry with last_page, as it always returns 1
echo $datapage->first_page();
last_page This method returns the total number of pages of information
echo $datapage->last_page();
first This method returns the number of the first entry on the current page
echo $datapage->first();
last This method returns the number of the last entry on the current page
echo $datapage->last();
previous_page This method returns the previous page number, if one exists. Otherwise it returns undefined
if($datapage->previous_page()){ echo $datapage->previous_page(); }
next_page This method returns the next page number, if one exists. Otherwise it returns undefined
if($datapage->next_page()){ echo $datapage->next_page(); }
skipped This method is useful paging through data in a database using SQL LIMIT clauses. It is simply $datapage->first - 1
$select = $pdo->prepare("SELECT * FROM table ORDER BY rec_date LIMIT ?, ?"); $select->execute(array($datapage->skipped(), $datapage->entries_per_page()));
change_entries_per_page This method changes the number of entries per page and the current page number such that the first item on the current page will be present on the new page.
$datapage->change_entries_per_page(20);
start_number This method return first numeration and end_number This method return last numeration
for($i=$datapage->start_number(); $i<=$datapage->end_number(); $i++){ if($i == $datapage->current_page()){ echo " <strong>$i</strong> "; }else{ echo " <a href='?page=$i'>$i</a> "; } }
array_numbers This method return total numeration in array
foreach($datapage->array_numbers() as $row){ if($row == $datapage->current_page()){ echo echo " <strong>$row</strong> "; }else{ echo " <a href='?page=$row'>$row</a> "; } }
Lucas Tiago de Moraes
lucastiagodemoraes@gmail.com