Code example for querying pages in a php + mysql database
/*
- * Php + mysql paging code
- *
- */
$ SQL _TABL = "abc"; // table name
- $ Where_name = "id> 10"; // query condition
- $ Perpagenum = 3; // The number displayed on each page
$ Total = mysql_fetch_array (mysql_query ("select count (*) AS count from $ SQL _TABL WHERE (". @ $ where_name .")"));
- $ SQL _count = $ total ['count']; // Obtain the number of returned data entries
- Unset ($ total); // deregister the variable $ total
// Calculate the number of inaccurate pages. $ page_all_num_f is an integer and $ page_all_num_t is an exact value (which may be decimal places)
- $ Page_all_num_f = round ($ page_all_num_t = $ SQL _count/$ perpagenum, 0 );
- If ($ page_all_num_f <$ page_all_num_t) // calculate the correct number of pages.
- $ Page_all_num = $ page_all_num_f + 1;
- Else
- $ Page_all_num = $ page_all_num_f;
If (is_numeric (@ $ _ GET ['P']) & amp; @ $ _ GET ['P']> 1 & amp; @ $ _ GET ['P'] <= $ page_all_num) // Obtain the GET parameter to determine the current page
- $ Page_num = $ _ GET ['P'];
- Else
- $ Page_num = 1;
$ SQL _s_num = ($ page_num-1) * $ perpagenum; // calculates the number of data entries starting
- $ SQL _p = "LIMIT". $ SQL _s_num. ",". $ perpagenum; // Generate the database query code
- $ Result = mysql_query ("select * from $ SQL _TABL WHERE (". @ $ where_name. ")". $ SQL _p); // query data
While ($ row = mysql_fetch_array ($ result) {// Main Loop
- // Output Content
- }
// Pagination button
- For ($ I = 1; $ I <= $ page_all_num; $ I ++)
- {
If (@ $ _ GET [name]! = Null) // Other GET parameters on the page
- $ P_n = "name =". @ $ _ GET [name]. "&";
- Else
- $ P_n = null;
If ($ page_num = $ I) // emphasize the number of the current page
- $ P_flag = "class = \" flag \"";
- Else
- $ P_flag = null;
Echo "$ I \ n ";
- }
- ?>
Recommended reading:
- Php and ajax without refreshing paging code
- Php article paging implementation code
- Php limit paging code
- Php paging class with multiple paging methods
- Php paging code for the previous and next pages
- Top 10 pages and top 10 pages of php paging code
- Example of simple php paging code
- A good php paging code
- One paging function: next page on the previous page
- A handy php paging class
- Php long article paging code
- A Practical php paging class
- Quick php paging
|