Pagination is by far the best way to display large numbers of results. With the help of the following codes, developers can display large amounts of data in multiple pages. On the Internet, paging is generally used for search results or browsing all information
php basic paging
The code is as follows | Copy code | ||||
$conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('dbname',$conn) or trigger_error("SQL" , E_USER_ERROR);<🎜><🎜>// find out how many rows are in the table $sql = "SELECT COUNT(*) FROM numbers"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0];<🎜><🎜>// number of rows to show per page $rowsperpage = 10; // find out total pages $totalpages = ceil($numrows / $rowsperpage);<🎜><🎜>// get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if<🎜>< 🎜>// if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages ; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if<🎜><🎜>// the offset of the list, based on current page $offset = ($currentpage - 1 ) * $rowsperpage;<🎜><🎜>// get the info from the db $sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage"; $result = mysql_query( $sql, $conn) or trigger_error("SQL", E_USER_ERROR);<🎜><🎜>// while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // echo data echo $list['id'] . " : " . $list['number'] . " "; } // end while/****** build the pagination links ******/ // range of num links to show $range = 3;// if not on page 1, don't show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " << "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " < "; } // end if// loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < ; (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x < ;= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don' t make a link echo " [$x] "; // if not current page... } else { // make it a link echo " $x "; } // end else } // end if } // end for// if not on last page, show forward and last page links if ($currentpage != $totalpages ) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page echo " > "; // echo forward link for lastpage echo " >> "; } // end if /****** end build pagination links ******/ ? > |
Let’s first look at a commonly used PHP paging class
The code is as follows | Copy the code |
< ?php $tbl_name=""; //your table name "; n"; //previous button if ($page > 1) $pagination.= "� previous"; else $pagination.= "� previous"; //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } $pagination.= "..."; $pagination.= "$lpm1"; $pagination.= "$lastpage"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "1"; $pagination.= "2"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } $pagination.= "..."; $pagination.= "$lpm1"; $pagination.= "$lastpage"; } //close to end; only hide early pages else { $pagination.= "1"; $pagination.= "2"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "$counter"; else $pagination.= "$counter"; } } } //next button if ($page < $counter - 1) $pagination.= "next �"; else $pagination.= "next �"; $pagination.= " } ?> while($row = mysql_fetch_array($result)) =$pagination?> |
Example
The code is as follows | Copy code |
class PageView{ $this->totalNum = $count;//Total number of records $this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true; }else if($this->pageNo > $this->pageCount - 4){ /*** "; if(!empty($pageList)){ if($this->pageCount >1){ if($this->hasPrePage){ $pageString = $pageString ."jsFunction . "(" . ($this->pageNo-1) . ")">上一页"; } foreach ($pageList as $k=>$p){ if($this->pageNo == $p){ $pageString = $pageString ."" . $this->pageNo . ""; continue; } if($p == -1){ $pageString = $pageString ."..."; continue; } $pageString = $pageString ."jsFunction . "(" . $p . ")">" . $p . ""; } if($this->hasNextPage){ $pageString = $pageString ."jsFunction . "(" . ($this->pageNo+1) . ")">下一页"; } } } $pageString = $pageString .(" return $pageString; } } ?> |
css代码
代码如下
|
复制代码
|
||||
|