Add pagination for PHP search and pagination for php search
Pagination is a method for browsing large amounts of data. Beginners often have no clue about this issue. Therefore, I wrote this article to explain it in detail, we strive to make friends who have read this article understand the principle and implementation methods of paging display.
All sample code is written in php.
The so-called paging display means that the result set in the database is artificially divided into segments for display.
Please read the following code in detail and debug and run it once. You 'd better modify it once and add your own functions.
$ Wherelist = array (); $ urlist = array (); if (! Empty ($ _ GET ['title']) {$ wherelist [] = "title like '% ". $ _ GET ['title']. "% '"; $ urllist [] = "title = ". $ _ GET ['title'];} if (! Empty ($ _ GET ['keyword']) {$ wherelist [] = "keywords like '% ". $ _ GET ['keyword']. "% '"; $ urllist [] = "keywords = ". $ _ GET ['keyword'];} if (! Empty ($ _ GET ['author']) {$ wherelist [] = "author like '% ". $ _ GET ['author']. "% '"; $ urllist [] = "author = ". $ _ GET ['author'];} $ where = ""; if (count ($ wherelist)> 0) {$ where = "where ". implode ('and', $ wherelist); $ url = '&'. implode ('&', $ urllist);} // implementation principle of paging // 1. get the total number of records in the data table $ SQL = "select count (*) from news $ where"; $ result = mysql_query ($ SQL); $ totalnum = mysql_num_rows ($ result ); // number of lines displayed per page $ pagesize = 5; // total number of pages $ maxpage = ceil ($ totalnum/$ Pagesize); $ page = isset ($ _ GET ['page'])? $ _ GET ['page']: 1; if ($ page <1) {$ page = 1;} if ($ page> $ maxpage) {$ page = $ maxpage;} $ limit = "limit ". ($ page-1) * $ pagesize. ", $ pagesize"; $ sql1 = "select * from news {$ where} {$ limit }"; // $ sql1 = "select * from news {$ where} {$ limit}"; $ res = mysql_query ($ sql1);?> <Form action = "searchpage. php" method = "get"> title: <input type = "text" name = "title" value = "<? Php echo $ _ GET ['title']?> "Size =" 8 "> keyword <input type =" text "name =" keywords "value =" <? Php echo $ _ GET ['keyword']?> "Size =" 8 "> author: <input type =" text "name =" author "value =" <? Php echo $ _ GET ['author']?> "Size =" 8 "> <input type =" button "value =" view all "onclick =" window. location = 'searchpage. php '"> <input type =" submit "value =" Search "> </form> <table border =" 1 "width =" 1000 "align =" center "> <tr> <td> NO. </td> <td> title </td> <td> keywords </td> <td> author </td> <td> date </td>/ td> <td> content </td> </tr> <? Php while ($ row = mysql_fetch_assoc ($ res) {?> <Tr> <td> <? Php echo $ row ['id']?> </Td> <? Php echo $ row ['title']?> </Td> <? Php echo $ row ['keyword']?> </Td> <? Php echo $ row ['author']?> </Td> <? Php echo date ("Y-m-d H: I: s", $ row ['addtime'])?> </Td> <? Php echo $ row ['content']?> </Td> </tr> <? Php }?> <Tr> <td colspan = "6"> <? Php echo "{$ totalnum} items on the current {$ page}/{$ maxpage} page"; echo "<a href = 'searchpage. php? Page = 1 {$ url} '> homepage </a> "; echo" <a href = 'searchpage. php? Page = ". ($ page-1)." {$ url} '> previous page </a> "; echo" <a href = 'searchpage. php? Page = ". ($ page + 1)." {$ url} '> next page </a> "; echo" <a href = 'searchpage. php? Page = {$ maxpage} {$ url} '> last page </a> ";?> </Td> </tr> </table>
I hope this article will help you with PHP programming.