PHP Pagination Navigation Ajax PHP Pagination Demonstration
Today I was watching Momo’s explanation of paging. It seemed like no one had posted in the original area for a long time, so I just expanded Momo’s one and gave a PHP+AJAX paging demonstration. Okay, that’s it. First of all, we are still basic. AJAX development framework:
Copy code The code is as follows:
var http_request=false;
function send_request(url){//Initialization, specify processing function, function to send request
http_request=false;
//Start Initialize the XMLHttpRequest object
if(window. rideMimeType("text/xml");
}
}
else if(window.ActiveXObject){//IE browser
try{
http_request=new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
http_request=new ActiveXobject(" Microsoft.XMLHttp");
}catch(e){}
}
}
if(!http_request){//Exception, failed to create object instance
window.alert("Failed to create XMLHttp object!");
return false ;
}
http_request.
//Determine the request method, URL, and whether to execute the next code synchronously
http_request.open("GET",url,true);
http_request.send(null);
}
//Processing Function that returns information
function processrequest(){
if(http_request.readyState==4){//Determine the object status
if(http_request.status==200){//The information has been returned successfully, start processing the information
document. getElementById(reobj).innerHTML=http_request.responseText;
} using. ");
}
}
}
function dopage(obj,url){
document.getElementById(obj).innerHTML="Reading data...";
send_request(url);
reobj=obj;
}
I display the content in a div. When the page turning action occurs, I use AJAX to update the DIV to achieve the page turning effect. This is the content display page code:
The code is as follows:
header("Content-type: text/html;charset=GBK");//输出编码,避免中文乱码
?>
$page=isset($_GET['page'])?intval($_GET['page']):1; //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1。
$num=10; //每页显示10条数据
$db=mysql_connect("localhost","root","7529639"); //创建数据库连接
mysql_select_db("cr_download"); //选择要操作的数据库
/*
首先咱们要获取数据库中到底有多少数据,才能判断具体要分多少页,具体的公式就是
总数据库除以每页显示的条数,有余进一。
也就是说10/3=3.3333=4 有余数就要进一。
*/
$result=mysql_query("select * from cr_userinfo");
$total=mysql_num_rows($result); //查询所有的数据
$url='test.php';//获取本页URL
//页码计算
$pagenum=ceil($total/$num); //获得总页数,也是最后一页
$page=min($pagenum,$page);//获得首页
$prepg=$page-1;//上一页
$nextpg=($page==$pagenum ? 0 : $page+1);//下一页
$offset=($page-1)*$num; //获取limit的第一个参数的值,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。
//开始分页导航条代码:
$pagenav="显示第 ".($total?($offset+1):0)."-".min($offset+10,$total)." 条记录,共 $total 条记录 ";
//如果只有一页则跳出函数:
if($pagenum<=1) return false;
$pagenav.=" 首页 ";
if($prepg) $pagenav.=" 前页 "; else $pagenav.=" 前页 ";
if($nextpg) $pagenav.=" 后页 "; else $pagenav.=" 后页 ";
$pagenav.=" 尾页 ";
$pagenav.=" 页,共 $pagenum 页";
//假如传入的页数参数大于总页数,则显示错误信息
If($page>$pagenum){
Echo "Error : Can Not Found The page ".$page;
Exit;
}
$info=mysql_query("select * from cr_userinfo limit $offset,$num"); //获取相应页数所需要显示的数据
While($it=mysql_fetch_array($info)){
Echo $it['username'];
echo "
";
} //显示数据
echo"
";
echo $pagenav;//输出分页导航
?>
The key to turning pages is to call the dopage() function when turning pages, and then use the callback information to update the content in the div. Server-side core code:
Copy code The code is as follows:
header("Content-type: text/html;charset=GBK");//Output encoding to avoid Chinese garbled characters
$ page=isset($_GET['page'])?intval($_GET['page']):1; //This sentence is to get the value of page in page=18. If page does not exist, then the number of pages is 1.
$num=10; elect_db("cr_download"); //Select to Database of operation
/*
First of all, we need to get how much data there is in the database to determine how many pages it needs to be divided into. The specific formula is
The total database is divided by the number of items displayed on each page, and the remainder is rounded up to one.
That is to say, 10/3=3.3333=4 If there is a remainder, we must round it up by one.
*/
$result=mysql_query("select * from cr_userinfo");
$total=mysql_num_rows($result); //Query all data
$url='test.php';//Get the URL of this page
// Page code calculation p $ Pagenum = CEIL ($ Total/$ Num); // Get the total page number, which is the last page
$ page = min ($ pagenum, $ page); // gets homepage
$ prepg = $ page-1;//Previous page
$nextpg=($page==$pagenum ? 0 : $page+1);//Next page
$offset=($page-1)*$num; Get the value of the first parameter of limit. If the first page is (1-1)*10=0, the second page is (2-1)*10=10.
//Start paging navigation bar code:
$pagenav="Display page ".($total?($offset+1):0)."-".min ($offset+10,$total)." records, total $total records ";
//If there is only one page, jump out of the function:
if($pagenum<=1) return false;
$pagenav.=" Homepage ";
if($prepg) $pagenav.=" Previous page "; else $pagenav.=" Previous page ";
if($nextpg) $pagenav.= " Next page "; else $pagenav.=" Next page ";
$pagenav.=" Last page ";
$pagenav.=" Pages, total $pagenum pages ";
//If the page number parameter passed in is greater than the total number of pages, an error message will be displayed
If($page>$pagenum){
"Echo "Error: Can Not Found The page ".$page;
Exit;
}
$info=mysql_query("select * from cr_userinfo limit $offset,$num"); //Get the data to be displayed for the corresponding page number
While($it=mysql_fetch_array($info)){
Echo $it[ 'username'];
echo "
";
} echo"
echo $pagenav;//Output paging navigation
?>
The above has introduced the PHP paging navigation Ajax PHP paging demonstration, including the content of PHP paging navigation. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.
