PHP + MYSQL implementation pagination display on data database

xiaoxiao2021-03-06  113

Title: PHP MySQL For Database Data Data Implementation Page Display Author: Stangly Wrong

A lot of PHP's beginners, are more confused for the page of the web page, and the principle of paging display is very simple. Today, someone asked me on QQ. Because of his hand, this small note is written, it is convenient for brothers and friends.

Question: My database structure is as follows

Create Database Book_List (ID INT UNSIGNED NOT NULL AUTO_INCREMENT, NAME VARCHAR (50) Not Null, Author Varchar (50) Not Null, Primary Key (ID))

ID: For book ID words, primary key Name: Book name Author: book book author

This example data sheet is relatively simple, mainly convenient to explain.

Now we need to display these data such as this page, require: 11 records in the data table, 5 items per page, and multiple page display.

Analyze now,

The first page shows 0 - 4 records second page Show 5 - 9 records page 3 Show 10 - 14 records

In fact, you can draw the following conclusions, each time you need to display from the $ POSI section, the number is $ PAGESize, there is such a SQL statement in MySQL:

Select * from book_listwhere 1Limit 0, 5

It means that 5 records are read from the 0th record. We can build our functions in the characteristics of this statement. In fact, every time you display, you can determine $ POSION. The implementation code is as follows:

$ PAGENO = 3; // Soled to display the page 3 $ POSI = ($ PAGENO - 1) * $ pageize; // Calculate from the proprietary record reading

/ / Then build the following SQL statement $ sql = "SELECT * home_LIST WHERE 1 LIMIT $ POSI, $ PAGESIZE";

That is, you can directly read all the data to display.

Then the following is given to a simplified version of the page class, you can display any data tables. In order to throw the jade.

PHP: Class Page {var $ tablename; // Operation Table Var $ pagesize; // Page size, default is 10 pages VAR $ PAGENO; // The current page number is the page number VAR $ PAGENUM; // Page statistics Var $ ORDER_FIELD; // Sort field var $ order_type; // Sort method // Configuration function, implement additional function // $ T is a table name Function Page ($ T) {IF ($ T == '" ) {Echo "Table Name is IncorRect!"; Return false;} $ this-> TableName = $ T; $ this-> PageSize = 10; $ this-> Pageno = 1; $ this-> Pagenum = NULL; $ THIS -> ORDER_FIELD = '1'; $ this-> ORDER_TYPE = 'dec'} // set page size Function set_pagesize ($ P = 10) {$ this-> Pagesize = $ P; Return True;} // Set current page number Function set_pageno ($ p) {$ this-> Pageno = $ P; return true;} // get the total number of pages // $ T is the specific table function get_pagenum () {global $ db; // Reference a global $ DB Operation Class $ SQL = "SELECT Count (*) AS Num from $ this-> Tablename "; $ result = $ db-> query ($ sql); $ obj = $ db-> fetchobject ($ result); return $ this-> Pagenum = $ OBJ-> Num;} / / Get the current page number function get_pageno () {return $ this-> Pageno;} // Get the current page size Function Get_pagesize () {Return $ this-> Pagesize;} // Get the page number Function Get_posion of the page you need to display () {RETURN $ POSI = ($ this-> Pageno - 1) * $ this-> pagesize;

} // read data // $ f for the list of fields to read // $ C for the SELECT statement // / / The result will return to an array // / $ data [0] = array (// FieldName => value //. //. // Function get_page ($ f = '*', $ c = 1) {global $ db // Reference a global $ db action class Object // Get a line number $ POSI = $ this-> get_posion (); $ sql = "SELECT $ f from $ this-> Tablename Where $ C Order by $ this-> Order_field $ this-> Order_type limit $ POSI $ this-> pagesize; $ result = $ db-> query ($ sql); $ data = array (); $ i = 1; while ($ arr = $ db-> fetcharray ($ result) {foreach ($ ARR AS $ Key => $ VAR) $ DATA [$ I] [$ Key] = $ VAR; $ I ;} Return $ DATA;}}

PHP: $ Page = New Page ('Book_List'); // Set page size is 15 pages $ page-> set_pagesize (15); // Set the displayed page as page 2 $ Page-> set_pageno (2) ; // Read the page data // Read the book name and the author two fields // Condition is the author must be Stangly's record $ data = $ page-> get_page ("name, author", "author = 'Stangly ' "); echo" currently displayed page data "; foreach ($ data as $ key => $ var) {echo" title "$ page-> Get_PageNo ()..": $ key "; echo" OF: $ VAR ";

Remarks: This program has no detailed test, there may be a bug, but also look at it.

转载请注明原文地址:https://www.9cbs.com/read-100276.html

New Post(0)