Home  >  Article  >  Backend Development  >  PHP Pagination Navigation Ajax PHP Pagination Demonstration

PHP Pagination Navigation Ajax PHP Pagination Demonstration

WBOY
WBOYOriginal
2016-07-29 08:36:001121browse

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:


Copy the code

The code is as follows:

header("Content-type: text/html;charset=GBK");//输出编码,避免中文乱码
?> 
 
 
ajax分页演示 
 
 
 

 
$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.

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