<?PHP
class fenyeController extends baseController{
public function showAction(){
$goodsModel = new goodsModel('localhost','xuefei','root','123');
$rows = $goodsModel ->getAll();
$this ->smarty->assign('list',$rows);
$this ->smarty->display('fenye.tpl');
}
public function processAction(){
$goodsModel = new goodsModel('localhost','123');
//每页显示记录数
$pagesize = 2;
//求出总的记录数
$total_rows = $goodsModel ->getTotal();
//求总共的页码数
$pages = ceil($total_rows[0]/$pagesize);
//当前第几页
$page = $_POST['page'];
$str = "当前第".$page."页"."总共".$pages."页"."共".$total_rows[0]."记录";
//var_dump($str);
//接下来,我要根据当前点击的页码求出对应的数据
$offset = $pagesize*($page-1);
$pageInfo = $goodsModel ->getPage($offset,$pagesize);
//var_dump($pageInfo);
//将获得数据链接,后返回
$first = 1;
$prev = $page-1;
$next = $page+1;
$last = $pages;
$first_a = "<a onclick='viewpage(".$first.")' href='#'>首页</a>";
if($page>1){
$prev_a = "<a onclick='viewpage(".$prev.")' href='#'>上一页</a>";
}
if($page<$pages){
$next_a = "<a onclick='viewpage(".$next.")' href='#'>下一页</a>";
}
$last_a = "<a onclick='viewpage(".$last.")' href='#'>尾页</a>";
$str = $str.$first_a.$prev_a.$next_a.$last_a;
//var_dump($str);
$info = array('str'=>$str,'info'=>$pageInfo);
echo json_encode($info);
//命令视图层显示数据
}
}
?>
<?PHP
//短路算法
defined("APP_NAME") || die("非法访问");
class goodsModel extends baseModel{
public function getAll(){
$sql = "select * from book";
$result = MysqL_query($sql);
$rows = array();
while($row = MysqL_fetch_assoc($result)){
$rows[] = $row;
}
return $rows;
}
public function getTotal(){
$sql = "select count(*) as total_rows from book";
$result = MysqL_query($sql);
$rows = MysqL_fetch_array($result);
//file_put_contents("d://row.txt",$rows,FILE_APPEND);
return $rows;
}
public function getPage($offset,$pagesize){
$sql = "select * from book limit $offset,$pagesize";
$result = MysqL_query($sql);
$rows = array();
while($row = MysqL_fetch_assoc($result)){
$rows[] = $row;
}
return $rows;
}
}
fenye.tpl文件
<html> <head> <@R_404_338@ charset="utf-8"> <script> function viewpage(page){ var xhr; if(window.ActiveXObject){ xhr = new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ xhr = new XMLHttpRequest(); } var url = "index.PHP?c=fenye&a=process"; xhr.open("POST",url,true); xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xhr.onreadystatechange = callback; xhr.send("page="+page); function callback(){ if(xhr.readyState==4){ if(xhr.status==200){ //alert(xhr.responseText); var json = eval("("+xhr.responseText+")"); var str = json.str; document.getElementById("para").innerHTML = str; for(var i=0;i<json.info.length;i++){ var info = json.info[i].id+json.info[i].name+json.info[i].author+json.info[i].public+json.info[i].isbn+json.info[i].type+json.info[i].price; document.getElementById('div').innerHTML = info; } //alert(json.str); //alert(json.str); /*for(var i=0;i<json.info.length;i++){ alert(json.info[i].name); } */ } } } } </script> </head> <body onLoad="viewpage(1)"> <h3 align="center">数据分页显示</h3> <table style="margin-bottom:20px;margin:auto;border:1px solid;border-spacing:1"> <tr> <th>ID</th><th>名称</th><th>作者</th><th>出版社</th><th>ISBN号</th><th>类型</th><th>价格</th> </tr> <{foreach from=$list item="value"}> <tr> <td style="padding-top:5px;"><{$value.id}></td> <td style="padding-top:5px;"><{$value.name}></td> <td style="padding-top:5px;"><{$value.author}></td> <td style="padding-top:5px;"><{$value.public}></td> <td style="padding-top:5px;"><{$value.isbn}></td> <td style="padding-top:5px;"><{$value.type}></td> <td style="padding-top:5px;"><{$value.price}></td> </tr> <{/foreach}> </table> <table align="center" style="margin-top:15px"> <tr> <td style="colspan:3;height:20"> <p id="para"></p> </td> </tr> </table> <div id="div"></div> </body> </html>
原文链接:https://www.f2er.com/ajax/166660.html