Home >Backend Development >PHP Tutorial >A very good PHP page turning class_PHP tutorial

A very good PHP page turning class_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:46:431048browse

Copy code The code is as follows:

/*
* Created on 2007-6-8
* Programmer: Alan , Msn - haowubai@hotmail.com
* bkJia.com Develop a project PHP - MySQL - Apache
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
//In order to avoid errors caused by repeated inclusion of files, a condition is added to determine whether the function exists:
if(!function_exists(pageft)){
//Define the function pageft(), the meanings of the three parameters are: :
//$totle: the total number of information;
//$displaypg: the number of information displayed on each page, the default setting here is 20;
//$url: the link in the paging navigation, in addition to adding different The query information other than "page" is the same as this URL.
// The default value should be set to the URL of this page (i.e. $_SERVER["REQUEST_URI"]), but the right side of the default value can only be a constant, so the default value is set to an empty string, and then inside the function Set as the URL of this page.
function pageft($totle,$displaypg=20,$url=''){

//Define several global variables:
//$page: current page number;
/ /$firstcount: The starting item of the (database) query;
//$pagenav: The page navigation bar code, which is not output inside the function;
//$_SERVER: Read the URL of this page "$_SERVER ["REQUEST_URI"]" required.
global $page,$firstcount,$pagenav,$_SERVER;

//In order to make "$displaypg" here accessible from outside the function, set it as a global variable. Note that after a variable is redefined as a global variable, the original value is overwritten, so it is reassigned here.
$GLOBALS["displaypg"]=$displaypg;

if(!$page) $page=1;

//If $url uses the default value, which is empty, then Assign the value to the URL of this page:
if(!$url){ $url=$_SERVER["REQUEST_URI"];}

//URL analysis:
$parse_url=parse_url($url) ;
$url_query=$parse_url["query"]; //Get the query string of the URL separately
if($url_query){
//Because the URL may contain page number information, we need to Remove it to add new page number information.
//Regular expressions are used here, please refer to "Regular Expressions in PHP"
$url_query=ereg_replace("(^|&)page=$page","",$url_query);

//Replace the query string of the processed URL with the query string of the original URL:
$url=str_replace($parse_url["query"],$url_query,$url);

//Add page query information after the URL, but wait for assignment:
if($url_query) $url.="&page"; else $url.="page";
}else {
$url.="?page";
}

//Page number calculation:
$lastpg=ceil($totle/$displaypg); //The last page is also the total number of pages
$page=min($lastpg,$page);
$prepg=$page-1; //Previous page
$nextpg=($page==$lastpg ? 0 : $page+ 1); //Next page
$firstcount=($page-1)*$displaypg;

//Start paging navigation bar code:
$pagenav="Display the first -".min($firstcount+$displaypg,$totle)." records, Total $totle records";

//If there is only one page, jump out of the function:
if($lastpg<=1) return false;

$pagenav.=" < a href='$url=1'>Homepage ";
if($prepg) $pagenav.=" Previous page< /a> "; else $pagenav.=" Previous page";
if($nextpg) $pagenav.="
Next page " ; else $pagenav.=" Next page";
$pagenav.=" Last page ";

// Pull down the jump list and list all page numbers in a loop:
$pagenav.=" Go to page of $lastpg";
}
}
?>


/*
//(The previous procedure is omitted)

include("pageft.php"); //Include the "pageft.php" file
//Get the total number of information
$result=mysql_query( "select * from mytable");
$total=mysql_num_rows($result);
//Call pageft() to display 10 pieces of information per page (when using the default 20, this parameter can be omitted), use URL of this page (default, so omitted).
pageft($total,10);
//The global variables generated now come in handy:
$result=mysql_query("select * from mytable limit $firstcount,$displaypg ");
while($row=mysql_fetch_array($result)){
//(list content omitted)
}

//Output paging navigation bar code:
echo $pagenav;

//(The following procedures are omitted)
*/
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320072.htmlTechArticleCopy the code as follows: ?php /* * Created on 2007-6-8 * Programmer: Alan , Msn - haowubai@hotmail.com * PHP100.com Develop a project PHP - MySQL - Apache * Window - Preferences...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn