Paging is often used in PHP, where the current end requests a number of data on the page, and it is possible to return a record of the specified number of bars by using an SQL statement to control the offset.
If you do not consider the front-end display effect, the bottom line is to control the SQL statement in the limit m, N of M and N of the numerical size, to avoid some bugs.
The two core variables for paging are: number of pages, how many pieces of data per page. Special case on the last page, there may be no data for the specified number of bars. For example, you need the Count bar data for Page one now, for table data, there is a (page-1) Xcount record in front of this page, and the limit offset in the SQL statement is * * * * (page-1) *count, Count. The statement is this, but the actual data that may be found does not have count bars, so the front-end display needs to be displayed in terms of the actual number of bars.
To get the number of pages, you need to know the values of these two variables, you can use Get or post to pass, when the first load or give the value of the page is not reasonable, you can assign a default value. These two variables can be obtained directly with the super-global array, of course, can also be obtained with the Filter_input function, you can also do some validation and filtering data.
//parameter acquisition, using GET or post functionGetParam ($name,$type=input_request) { if($type==input_request) {$val= Filter_input (Input_get,$name);//simple filtering, you can set a more detailed filter method if($val==NULL||$val===false) {$val= Filter_input (Input_post,$name); return($val==NULL||$val===false) ?NULL:$val; }Else{return $val; } } Else{ $val= Filter_input ($type,$name); return($val==NULL||$val===false) ?NULL:$val; } }
After you get the page and count, the query gets the data
//Get Data functionGetData () {Try{$dsn= ' mysql:dbname= '. Db_name. '; Host= '.HOST; $pdo=NewPDO ($dsn, USER,PASS); $count= GetParam (' count ');//number per page $page= GetParam (' page ');//The first few pages $count= ($count==NULL||$count<= 0)? 10:$count; $page= ($page==NULL||$page<= 0)? 1:$page; $sql 1= ' SELECT count (*) as Total from info '; $stmt=$pdo->query ($sql 1); $ret=$stmt->fetch (PDO::FETCH_ASSOC); $total _count=$ret[' Total '];//total number of data bars in table $count=$count>$total _count?$total _count:$count;//If you specify that the number of impressions per page is greater than the total number of bars $total _page= (int)Ceil($total _count/$count);//total number of pages $page=$page>$total _page?$total _page:$page;//If the specified page is greater than the total pages $sql 2=sprintf(' select * from info limit%s,%s ', ($page-1) *$count,$count); $query=$pdo->query ($sql 2); $ret=$query->fetchall (PDO::FETCH_ASSOC); $item _num=Count($ret);//number of data bars actually found include' show.php ';//Load Data display page}Catch(pdoexception$e) {Echo' ERROR: '.$e->getmessage (). ' <br/> '; return Array(); }}
Also need to make a display data page, display, the page shows such as 1, 2, 3, ranking a few pages, they are actually links, click on the page to pass in the required variables, and then the script query to the data sent over, the main part of the code
<Body> <Divclass= "Main" > <Table> <caption><H3>Employee information</H3></caption> <TR> <th>Id</th><th>Name</th><th>Department</th><th>Age</th> </TR> <?php for ($i =0; $i < $item _num; $i + +) {if ($i%2 = = 0) { echo "<tr class= ' back ' ><td>{$ret [$i] [' ID ']}</td><td>{$ret [$i] [' Name ']}</TD><TD >{$ret [$i] [dep ']}</td><td>{$ret [$i] [' age ']}</td></tr>]; } else{echo "<tr><td>{$ret [$i] [' ID ']}</td><td>{$r et[$i] [' name ']}</td><td>{$ret [$i] [' DEP ']}</td><td>{$ret [$i] [' Age ']}</td></tr > "; } } ?> </Table> <Divclass= "lnk"> <ul> <Li><ahref= ' <?phpecho "Get.php?page=1&count={$count} "?>' > Home</a></Li> <Li><ahref= ' <?php$pre= $page-1;$pre= ($pre ==0)? 1: $pre;echo "Get.php?page={$pre}&count={$count} "?>' > Previous page</a></Li> <?php for ($i =1; $i <= $total _page; $i + +) {echo "<li><a href= ' get.php?page={$ i}&count={$count} ' >{$i}</a></li> ";}?> <Li><ahref= ' <?php$post= $page +1;$post= ($post = = $total _page)? $total _page: $post;echo "Get.php?page={$post}&count={$count} "?>' > Next page</a></Li> <Li><ahref= ' <?phpecho "Get.php?page={$total _page}&count={$count} "?>' > Last</a></Li> <ul> <Div> </Div> </Body>
Load Display page effects
To make a simple style, the default loading is actually accessing the http://localhost/page/get.php?page=1&count=10, giving the default value. Click Last to see the Address bar showing page and count parameters
Be aware of the insufficient number of last-page records. Of course, in some frameworks have been done in the paging class, such as CI, can be directly used, convenient, powerful and style can be defined more beautiful, but also consider that if there are thousands of data, such as the case, of course, you can not put each page of the link is placed below the table.
Little pagination.