標籤:limit post data tle input rip value center php
要寫分頁,首先你得清楚,一頁你想顯示多少條資訊?如何計算總共顯示的頁數?
先說一下思路:
(1)從資料庫讀取資料,以chenai表為例,讀取所有留言資訊。並能夠實現輸入寄件者,可以查詢該寄件者的留言總數。
(2)計算分頁資訊,此處,以每頁顯示5條資訊為例。分頁列表()以當前頁為中心,向前顯示2條,向後顯示2條。
(3)給每個分頁列表添加對應的點擊事件。
(4)能夠實現分頁後,添加查詢分頁功能。
現實:
第一步:引入 jquery檔案和bootstrap檔案。(因為表格和分頁均是用的bootstrap)
<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><!--引入bootstrap的css檔案--><link type="text/css" rel="stylesheet" href="../bootstrap-3.3.7-dist/css/bootstrap.min.css" /><!--引入js包--><script src="../jquery-3.2.0.js"></script><!--引入bootstrap的js檔案--> <script src="../bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> <style type="text/css"> /*給當前頁添加特殊樣式*/ .dqy{ font-weight: bolder; } </style></head>
第二步:從資料庫讀取資料
<body><h1 style="text-align: center;">分頁查詢</h1><div><input type="text" id="send"/><input type="button" id="chaxun" value="查詢"/></div><!--用table顯示留言資訊--><table class="table table-striped"> <thead> <tr> <th>代號</th> <th>寄件者</th> <th>接收者</th> <th>時間</th> <th>留言資訊</th> </tr> </thead> <tbody id="nr"> </tbody></table><!--造一個div顯示分頁資訊--><div><ul class="pagination xinxi"> </ul></div></body></html>
第三步:寫分頁資訊
(1)造方法
<script type="text/javascript">//定義當前頁,預設為1,不能為0var page = 1;//定義每一頁顯示多少條留言資訊var num = 5;//造一個方法載入資料Load();//造一個方法載入分頁資訊
LoadFenYe();
(2)寫方法
function Load(){var send=$("#send").val();$.ajax({url:"chuli.php",data:{page:page,num:num,sender:send},type:"POST",dataType:"JSON",success:function(data){//因為要返回json資料,所以更改DB.class.php類檔案,使其返回json資料var str ="";//json遍曆方式for(var k in data)//alert(data[k].Ids);{str = str+"<tr><td>"+data[k].Ids+"</td><td>"+data[k].Sender+"</td><td>"+data[k].Receiver+"</td><td>"+data[k].Times+"</td><td>"+data[k].Comment+"</td></tr>";}$("#nr").html(str);}});} //載入分頁的方法 function LoadFenYe(){ //定義一個變數,用來放所有的分頁資訊,預設為空白 var s =""; var send=$("#send").val();//載入上一頁 s = "<span id=‘prev‘>前一頁</span>"; //載入中間的數字 var zts=0; //總的留言條數為0條 $.ajax({ async:false, //必須取消非同步,不然總條數為0,總頁數也為0 url:"tiaoshu-cl.php", data:{sender:send},type:"POST", dataType:"TEXT", success:function(data){zts=data; } }); //總頁數為總條數除以每一頁顯示的條數,如果為小數則向上取整 var zys = Math.ceil(zts/num); //已經知道總的頁數後,開始顯示分頁資訊 page = parseInt(page); //轉一下整型 // 當前頁開始,向前顯示2頁,向後顯示兩頁 for (var i = page-2;i < page+3;i++) { //注意i的範圍 if(i>0 && i<=zys){ //如果i等於當前頁,樣式會不一樣 if (i==page){ s=s+"<span ys=‘"+i+"‘ class=‘dqy‘>"+i+"</span>"; //當前頁 } else{ s=s+"<span ys=‘"+i+"‘ class=‘qty‘>"+i+"</span>"; //其他頁 } } } //載入後一頁s = s +"<span id=‘next‘>後一頁</span>";s = "<li>"+s+"</li>";$(".xinxi").html(s);
第四步:添加點擊事件
//給前一頁加點擊事件$("#prev").click(function(){page = parseInt(page);if(page>1){page--;}//重新載入資料Load();LoadFenYe();})//給中間頁面添加點擊事件$(".qty").click(function(){page=parseInt($(this).attr("ys")); //取自訂的屬性值Load();LoadFenYe();})//給後一頁加點擊事件$("#next").click(function(){page = parseInt(page);if(page<zys){page++;}//重新載入資料Load(); LoadFenYe();})
} //這是分頁方法的另一半花括弧
第五步:實現查詢分頁
$("#chaxun").click(function(){Load(); LoadFenYe();})
chuli.php頁面
<?php$page=$_POST["page"];$num = $_POST["num"];$sender = $_POST["sender"];require "../DB.class.php";$db = new DB();$tgs = ($page-1)*$num;//跳過的條數$sql = "select * from chenai where Sender like ‘%{$sender}%‘ limit {$tgs},{$num}";echo $db->jsonquery($sql);?>
tiaoshu-cl.php頁面
<?php$sender = $_POST["sender"];require "../DB.class.php";$db = new DB();$sql = "select count(*) from chenai where Sender like ‘%{$sender}%‘";echo $db->strquery($sql);?>
如何用ajax寫分頁查詢(以留言資訊為例)-----2017-05-17