Very classic Paging program, how to handle

Source: Internet
Author: User
It's a classic page-out 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 Database
*/
function OpenDatabase ()
{
Global $mysql _localhost, $mysql _user, $mysql _PWS, $mysql _data; Set global variable, but invalid value after exiting function
$conn = @mysql_connect ($mysql _localhost, $mysql _user, $mysql _pws) or Die ("Hint: Database connection failed! ");
$GLOBALS ["conn"] = $conn; Global variable value, value valid after exiting function
mysql_query ("Set names ' Utf-8 '"); Set encoding
$db _selected = mysql_select_db ($mysql _data, $conn);
}
/**
* Close Database
*/
function CloseDatabase ()
{
Mysql_close ($GLOBALS ["Conn"]);
}
?>
page.php
Header ("Content-type:text/html;charset=utf-8");
Include ("conn.php");
OpenDatabase (); Open Database
-------Call the paging function-------
$pagenav = _page_list ("Test", 3);
//------------------------
Echo $pagenav; Print out the string returned by pagination
------------------------Display List-------------------
$sql = "SELECT * from Test limit $firstcount, $pagesize";
$result = mysql_query ($sql, $conn); Query statements
while ($row = Mysql_fetch_array ($result, Mysql_num))
{
echo "". $row [0]. "|". $row [1]. "| ". $row [2];
}
echo "";
Mysql_free_result ($result); Free up memory space
//----------------------------------------------------
Echo $pagenav; Print out the string returned by pagination


//================================================================================
/**
* Function Name: _page_list
* Parameters: $table----The name of the table to be queried
* $page _size----The number of records per page, the default page shows 20
* Function: Distribution shows all records
* How to use: $pagenav = _page_list ("test");
* Then print out the $pagenav where you want the page character to appear
*/
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); Total Record Count
Mysql_free_result ($result); Free up memory space
$LASTPG = Ceil ($total/$pagesize); Last page, which is the total number of pages

$page = min ($lastpg, $page); Returns the smallest number of them
$PREPG = ($page > 1)? $page-1: $page; Previous page
$NEXTPG = ($page < $LASTPG)? $page + 1: $page; Next page

$firstcount = ($page-1) * $pagesize; A pointer to the first record on each page, starting from 0 by default
if ($firstcount <= 0) $firstcount = 0;

$pagenav = "Total". $lastpg. "Page";
$pagenav. = "Home";
if ($page <= 1)
{
$pagenav. = "Previous page";
}
Else
{
$pagenav. = "Previous page";
}

if ($page >= $LASTPG)
{
$pagenav. = "Next page";
}
Else
{
$pagenav. = "Next page";
}
$pagenav. = "Last";
$pagenav. = "Current section". $page. "Page";
$pagenav. = "
";
return $pagenav;
}

/**
* Function Name: _get_url
* Function: Returns the full URL value of the page, that is, from the root directory, does not contain the URL at the back of the #fragment
*/
function _get_url ()
{
Global $page;
$url = $_server["Request_uri"]; Get URL Address
$url _path = Parse_url ($url, Php_url_path); Get file path
$url _query = Parse_url ($url, php_url_query); Get the URL? Information, which is equivalent to the query string
echo $url;
  • Contact Us

    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

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.