Another PHP paging code I have written many PHP paging classes before, but today I feel that this paging program is very good. It is simple and practical, the code is reasonable and there is no redundant code. It is a good paging function.
Another PHP tutorial paging code
I have written many PHP paging classes before, but today I feel that this paging program is very good. It is simple and practical, the code is reasonable and there is no redundant code. It is a good paging function.
*/
class multipage {
var $total;
var $perpage;
var $pages;
var $maxpage;
var $offset = 9;
var $curr_page;
function init($total, $perpage, $maxpage) { //Initialization number of pages
$this->total;
$this->perpage;
$this->maxpage;
$this->offset = 9;
}
function getpagelist() {//Get the paging list
$result_pages = "";
$this->pages = ceil($this->total / $this->perpage);
if ($this->pages > $this->maxpage) {
$from = $this->curr_page - $this->offset;
if ($from < 1) {
$from = 1;
}
$to = $from + $this->maxpage - 1;
if ($to > $this->pages) {
$to = $this->pages;
If (($to - $from) < $this->maxpage) {
$from = $from - 1;
}
}
} else {
$from = 1;
$to = $this->pages;
}
$p = 0;
for($i = $from; $i <= $to; $i++) {
$result_pages[$p] = $i;
$p++;
}
return $result_pages;
}
function getfirst() { //Get the first page
if ($this->curr_page > 1 && $this->pages > 1) {
Return 1;
} else {
Return "";
}
}
function getlast() { //Get the last page
if ($this->pages > 1 && $this->curr_page < $this->pages) {
Return $this->pages;
} else {
Return "";
}
}
function getprev() {//Previous page
$prevpage = $this->curr_page - 1;
if ($prevpage > 0) {
Return $prevpage;
} else {
$prevpage = "";
Return $prevpage;
}
}
function getnext() {//Next page
$nextpage = $this->curr_page + 1;
if ($nextpage <= $this->pages) {
Return $nextpage;
} else {
$nextpage = "";
Return $nextpage;
}
}
function gettotal() {//How many pages in total
if ($this->pages > 0) {
Return $this->pages;
} else {
Return 1;
}
}
}//How to use paging class
$page = new multipage();
$page->gettotal(); //Total page
$page->getnext();//Next page