PHP + Mysql ---- data paging display technology
Generally, when a page loads a large amount of data, the data cannot be displayed at the same time. At this time, the more common method is the scroll bar and paging. All the children who have read e-books know that the screen of a mobile phone or pad cannot be fully displayed with so many words. Just kidding, there are millions of words in a few megabytes of books, put it on a few inches of screen. Therefore, we all slide the text to scroll or flip pages. This blog post will share with you the paging technology of php.
First, obtain the data of a table in the database, output it to the webpage, and then display it by page. I will understand it in one sentence, but please refer to the Code for details about paging.
<? Phpheader ("content-type: text/html; charset = UTF-8"); // connect to the mysql database $ conn = mysql_connect ("localhost ","",""); // select the database mysql_select_db ("test"); // set the client and connection Character set mysql_query ("set names utf8"); // The limit parameter $ length = 15; $ pagenum = @ $ _ GET ['page']? $ _ GET ['page']: 1; // total number of data rows $ sqltot = "select count (*) from t1 "; $ arrtot = mysql_fetch_row (mysql_query ($ sqltot); $ pagetot = ceil ($ arrtot [0]/$ length); // limit the number of pages if ($ pagenum> = $ pagetot) {$ pagenum = $ pagetot;} $ offset = ($ pagenum-1) * $ length; // obtain data from the database $ SQL = "select * from t1 order by id limit {$ offset}, {$ length}"; $ result = mysql_query ($ SQL ); echo "
The major technical point is the limit in mysql.
$sql="select * from table limit m,n";
The two parameters after limit are the start point and offset respectively. M can be left blank. The default value is 0 automatically. It indicates that the obtained records are restricted from m to m + n. By passing variables as parameters, You can flexibly control the number of displays on each page.
When turning pages, you need to add two hyperlinks. The $ page and $ prevpage and $ nextpage variables are added to the link address. The data displayed on each page is different through the changes of $ prevpage and $ nextpage.
However, only these two points can ensure that the page display is successful. Once the page number increments to the data range, no data will occur, but the page flip result can continue. Therefore, in order to improve the page display, you must also limit the number of pages on pages. Page number = total records/single page display records. Then, use the ceil () function to get an integer. However, before obtaining the record set, you must limit the total number of records. The recommended method here is count (*) to obtain the total number of rows, and then use mysql_fetch_row to dig out the value. The returned array. The first value is the total record value. You can view and understand the code. After the maximum number of pages is obtained, an if statement can be used to determine the number of pages. This ensures that the last page of data cannot be reached.
The last point is @
$pagenum=@$_GET['page']?$_GET['page']:1;
@ Is added before this ternary operation to ignore the php Notice. Because $ page is an uncertain index. It comes in handy when it comes to the final hyperlink of the Code. Of course, there are still many ways to ignore the warning, such as modifying the php configuration file. This depends on your personal preferences. Don't tangle with @.
The graph displayed on the last two pages (beautification and data are ignored, while the instance is used)