Very classic paging program Conn. php & lt ;? Phpheader (& quot; Content-Type: text/html; charset = utf-8 & quot;); & nbsp; $ conn = & quot ;; $ mysql_localho is a classic paging program
Conn. php
Header ("Content-Type: text/html; charset = utf-8 ");
$ Conn = "";
$ Mysql_localhost = "localhost ";
$ Mysql_user = "root ";
$ Mysql_pws = "sa ";
$ Mysql_data = "page ";
/**
* Connect to the database
*/
Function OpenDatabase ()
{
Global $ mysql_localhost, $ mysql_user, $ mysql_pws, $ mysql_data; // sets global variables, but the value after exiting the function is invalid.
$ Conn = @ mysql_connect ($ mysql_localhost, $ mysql_user, $ mysql_pws) or die ("prompt: database connection failed! ");
$ GLOBALS ["conn"] = $ conn; // global variable value. The value is valid after exiting the function.
Mysql_query ("set names 'utf-8'"); // sets the encoding
$ Db_selected = mysql_select_db ($ mysql_data, $ conn );
}
/**
* Shut down the database.
*/
Function CloseDatabase ()
{
Mysql_close ($ GLOBALS ["conn"]);
}
?>
Page. php
Header ("Content-Type: text/html; charset = utf-8 ");
Include ("Conn. php ");
OpenDatabase (); // open the database
// ------- Call the paging function -------
$ Pagenav = _ page_list ("test", 3 );
//------------------------
Echo $ pagenav; // Print the string returned by the page
// ------------------------ Display List -------------------
$ SQL = "SELECT * from test limit $ firstcount, $ pagesize ";
$ Result = mysql_query ($ SQL, $ conn); // query statement
While ($ row = mysql_fetch_array ($ result, MYSQL_NUM ))
{
Echo "". $ row [0]. "|". $ row [1]. "|". $ row [2];
}
Echo "";
Mysql_free_result ($ result); // release the memory space
//----------------------------------------------------
Echo $ pagenav; // Print the string returned by the page
// ================================================ ========================================================== =
/**
* Function name: _ page_list
* Parameter: $ table ---- name of the table to be queried
* $ Page_size ---- number of records on each page. by default, 20 records are displayed on one page.
* Function: displays all records in the distribution.
* Usage: $ pagenav = _ page_list ("test ");
* Print $ pagenav at the place where the paging characters are to be displayed.
*/
Function _ page_list ($ table, $ page_size = 20)
{
Global $ firstcount, $ pagenav, $ page, $ conn, $ pagesize;
$ Page = $ _ GET [page];
$ Url = _ get_url ();
$ Pagesize = $ page_size;
$ Result = mysql_query ("select * from $ table", $ conn );
$ Total = mysql_num_rows ($ result); // The total number of records.
Mysql_free_result ($ result); // release the memory space
$ Lastpg = ceil ($ total/$ pagesize); // The last page, that is, the total number of pages.
$ Firstcount = ($ page-1) * $ pagesize; // pointer to the first record on each page, starting from 0 by default
If ($ firstcount <= 0) $ firstcount = 0;
/**
* Function name: _ get_url
* Function: return the complete URL value of the page, that is, starting from the root directory, excluding the last part of the URL # fragment
*/
Function _ get_url ()
{
Global $ page;
$ Url = $ _ SERVER ["REQUEST_URI"]; // Obtain the URL address
$ Url_path = parse_url ($ url, PHP_URL_PATH); // get the file path
$ Url_query = parse_url ($ url, PHP_URL_QUERY); // get the URL? Number, which is equivalent to a query string.
Echo $ url;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service