PHP custom paging function_PHP tutorial
PHP custom paging function
PHP custom paging function
Every time you write a list page, you almost have to write a paging program. Recently, Xiaoyang finally decided to do it once and for all - to customize a paging function and save it in the "pageft.php" file. If you want to use paging, just include the "pageft.php" file directly and call this function. When Xiaoyang wrote this function, he tried his best to pay attention to the portability and ease of use of the program. The following is the function written by Xiaoyang:
//In order to avoid errors caused by repeated inclusion of files, a condition for determining whether the function exists:
if(!function_exists(pageft)){
//Define the function pageft(), the meaning of the three parameters is:
//$totle: total number of messages;
//$displaypg: The number of information displayed on each page, the default setting here is 20;
//$url: The link in the paging navigation is the same as this URL except for adding different query information "page".
// 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: (database) starting item of query;
//$pagenav: page navigation bar code, it is not output inside the function;
//$_SERVER: necessary to read the URL "$_SERVER["REQUEST_URI"]" of this page.
global $page,$firstcount,$pagenav,$_SERVER;
//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 the assigned value is 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 in order to add new page number information.
//Regular expressions are used here, please refer to "Regular Expressions in PHP" (http://www.pconline.com.cn/pcedu/empolder/wz/php/10111/15058.html)
$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 ".($totle?($firstcount+1):0)."-".min($firstcount+$displaypg,$totle)." records, a total of $totle records
";
//If there is only one page, jump out of the function:
if($lastpg
$pagenav.=" Home ";
if($prepg) $pagenav.=" previous page "; 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.="To n"; for($i=1;$i
}
}
?>

Okay, the paging function has been written and saved as "pageft.php". When you want to paginate, include it and call the pageft() function. However, it does not output anything, but generates several global variables for use: $firstcount, $displaypg, $pagenav. The following examples illustrate its usage:
// (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 (this parameter can be omitted when using the default 20), and use the URL of this page (default, so omit it).
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)
?>


TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
