国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PHP+jquery+ajax實現(xiàn)分頁

Original 2016-12-26 16:17:05 461
abstract:這篇文章主要為大家詳細介紹了PHPjquery+ajax實現(xiàn)分頁的代碼。本文實例為大家分享了jquery ajax實現(xiàn)分頁的具體代碼,供大家參考,具體內(nèi)容如下HTML<div id="list">  <ul></ul> </div> <div id="pagecount"&g

這篇文章主要為大家詳細介紹了PHPjquery+ajax實現(xiàn)分頁的代碼。

本文實例為大家分享了jquery ajax實現(xiàn)分頁的具體代碼,供大家參考,具體內(nèi)容如下

HTML

<div id="list">
 <ul></ul>
</div>
<div id="pagecount"></div>

   

CSS

#list{width:680px; height:530px; margin:2px auto; position:relative}
#list ul li{float:left;width:220px; height:260px; margin:2px}
#list ul li img{width:220px; height:220px}
#list ul li p{line-height:22px}
#pagecount{width:500px; margin:10px auto; text-align:center}
#pagecount span{margin:4px; font-size:14px}
#list ul li#loading{width:120px; height:32px; border:1px solid #d3d3d3;
position:absolute; top:35%; left:42%; text-align:center; background:#f7f7f7
url(loading.gif) no-repeat 8px 8px;-moz-box-shadow:1px 1px 2px rgba(0,0,0,.2);
-webkit-box-shadow:1px 1px 2px rgba(0,0,0,.2); box-shadow:1px 1px 2px rgba(0,0,0,.2);}

   

jQuery

我們先聲明變量,后面的代碼要用到以下變量。

var curPage = 1; //當前頁碼
var total,pageSize,totalPage; //總記錄數(shù),每頁顯示數(shù),總頁數(shù)

   

接下來,我們自定義一個函數(shù):getData(),用來獲取當前頁數(shù)據(jù)。函數(shù)中,我們利用$.ajax()向后臺pages.php發(fā)送POST異步請求,將當前頁碼以JSON格式傳遞給后臺。

//獲取數(shù)據(jù)
function getData(page){
 $.ajax({
 type: 'POST',
 url: 'pages.php',
 data: {'pageNum':page-1},
 dataType:'json',
 beforeSend:function(){
 $("#list ul").append("<li id='loading'>loading...</li>");//顯示加載動畫
 },
 success:function(json){
 $("#list ul").empty();//清空數(shù)據(jù)區(qū)
 total = json.total; //總記錄數(shù)
 pageSize = json.pageSize; //每頁顯示條數(shù)
 curPage = page; //當前頁
 totalPage = json.totalPage; //總頁數(shù)
 var li = "";
 var list = json.list;
 $.each(list,function(index,array){ //遍歷json數(shù)據(jù)列
 li += "<li><a href='#'><img src='"+array['pic']+"'>"+array['title']
 +"</a></li>";
 });
 $("#list ul").append(li);
 },
 complete:function(){ //生成分頁條
 getPageBar();
          
            當點擊分頁條中的分頁鏈接時,調(diào)用getData(page)加載對應頁碼的數(shù)據(jù)?! ?            $("#pagecount span a").on('click',function(){
        var rel = $(this).attr("rel");
       if(rel){
      getData(rel);
       }
          });
   },
 error:function(){
 alert("數(shù)據(jù)加載失敗");
 }
 });
}

   

請求成功后并返回數(shù)據(jù),將相應的數(shù)據(jù)附給變量,并將返回的商品數(shù)據(jù)列表循環(huán)展示到對應容器#list ul中。當數(shù)據(jù)完全加載完畢后,調(diào)用分頁條函數(shù)getPageBar()生成分頁條。

//獲取分頁條
function getPageBar(){
 //頁碼大于最大頁數(shù)
 if(curPage>totalPage) curPage=totalPage;
 //頁碼小于1
 if(curPage<1) curPage=1;
 pageStr = "<span>共"+total+"條</span><span>"+curPage
 +"/"+totalPage+"</span>";
  
 //如果是第一頁
 if(curPage==1){
 pageStr += "<span>首頁</span><span>上一頁</span>";
 }else{
 pageStr += "<span><a href='javascript:void(0)' rel='1'>首頁</a></span>
 <span><a href='javascript:void(0)' rel='"+(curPage-1)+"'>上一頁</a></span>";
 }
  
 //如果是最后頁
 if(curPage>=totalPage){
 pageStr += "<span>下一頁</span><span>尾頁</span>";
 }else{
 pageStr += "<span><a href='javascript:void(0)' rel='"+(parseInt(curPage)+1)+"'>
 下一頁</a></span><span><a href='javascript:void(0)' rel='"+totalPage+"'>尾頁</a>
 </span>";
 }
  
 $("#pagecount").html(pageStr);
}

   

最后,當頁面第一次加載時,我們加載第一頁數(shù)據(jù)即getData(1),

$(function(){
 getData(1);
  
});

   

PHP

pages.php接收每次前端頁面的ajax請求,根據(jù)提交的頁碼pageNum值,從mysql數(shù)據(jù)庫中獲取數(shù)據(jù),計算總記錄數(shù)和總頁數(shù),讀取對應頁碼下的數(shù)據(jù)列表,并將最終結(jié)果以JSON格式返回給前端頁面。

include_once('connect.php'); //連接數(shù)據(jù)庫,略過,具體請下載源碼查看
 
 
$page = intval($_POST['pageNum']); //當前頁
  
$result = mysql_query("SELECT id from food");
$total = mysql_num_rows($result);//總記錄數(shù)
$pageSize = 6; //每頁顯示數(shù)
$totalPage = ceil($total/$pageSize); //總頁數(shù)
  
$startPage = $page*$pageSize; //開始記錄
//構(gòu)造數(shù)組
$arr['total'] = $total;
$arr['pageSize'] = $pageSize;
$arr['totalPage'] = $totalPage;
$query = mysql_query("SELECT id,title,pic from food order by id asc limit
$startPage,$pageSize"); //查詢分頁數(shù)據(jù)
while($row=mysql_fetch_array($query)){
 $arr['list'][] = array(
 'id' => $row['id'],
 'title' => $row['title'],
 'pic' => $row['pic'],
 );
}
echo json_encode($arr); //輸出JSON數(shù)據(jù)

   

最后,附上Mysql表結(jié)構(gòu)

CREATE TABLE IF NOT EXISTS `food` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `title` varchar(100) NOT NULL,
 `pic` varchar(255) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 更多關(guān)于PHP+jquery+ajax實現(xiàn)分頁請關(guān)注PHP中文網(wǎng)(m.miracleart.cn)其它文章!   


Release Notes

Popular Entries