Ajax+smarty技术实现无刷新分页
程序员文章站
2022-08-10 17:17:29
这里运用smarty模板,更简单
本文主要的技术:ajax,php,smarty,另外自己封装了一个很简单的类
类:
(function(){...
这里运用smarty模板,更简单
本文主要的技术:ajax,php,smarty,另外自己封装了一个很简单的类
类:
(function(){ function $(id) { return document.getelementbyid(id); } $.init=function() { try{return new xmlhttprequest();}catch(e){}; try{return new activexobject('microsoft.xmlhttp');}catch(e){} alert('请更换浏览器'); } $.get=function (url,data,callback,type) { var xhr = this.init(); url += '?' +new date().gettime(); if(data!=null){ url += '&'+data; } xhr.open('get',url); xhr.onreadystatechange = function () { if(xhr.readystate == 4 && xhr.status == 200){ if(type==null){ callback(xhr.responsetext); } if(type == 'text'){ callback(xhr.responsetext); } if(type == 'xml'){ callback(xhr.responsexml); } if(type == 'json'){ callback(eval("("+xhr.responsetext+")")); } } } xhr.send(null); } $.post = function (url,data,callback,type) { var xhr = this.init(); xhr.open('post',url); xhr.setrequestheader('content-type','application/x-www-form-urlencoded'); xhr.onreadystatechange = function () { if(xhr.readystate == 4 && xhr.status == 200){ if(type==null){ callback(xhr.responsetext); } if(type == 'text'){ callback(xhr.responsetext); } if(type == 'xml'){ callback(xhr.responsexml); } if(type == 'json'){ callback(eval("("+xhr.responsetext+")")); } } }; xhr.send(data); }
类很简单,不介绍了
html:
window.onload=function () { init(2); } function del(id,p){ $.get('del.php','id='+id,function(msg){ if(msg==1){ init(p); } }) } function init(p) { $.get('page.php','page='+p,function(msg){ $('result').innerhtml=msg; }); } php: <?php header('content-type:text/html;charset=utf-8'); mysql_connect('localhost','root','root'); mysql_select_db('db2'); mysql_query('set names utf8'); $re=mysql_query('select count(*) as num from catgory'); $hang=mysql_fetch_assoc($re); $row1=$hang['num']; $page=isset($_get['page'])? $_get['page']:1; $pagesize=2; $total=ceil($row1/$pagesize); $prev=$page-1; $next=$page+1; if($prev<1){$prev=1;} if($next>$total){$next=$total;} $offset = ($page-1)*$pagesize; $result=mysql_query("select * from catgory limit ".$offset.','.$pagesize); $ct=mysql_num_rows($result); $data=array(); for($i=0;$i<$ct;$i++){ $r=mysql_fetch_assoc($result); $data[]=$r; } include('libs/smarty.class.php'); $smarty=new smarty(); $smarty->assign('data',$data); $smarty->assign('page',$page); $smarty->assign('pagesize',$pagesize); $smarty->assign('total',$total); $smarty->assign('prev',$prev); $smarty->assign('next',$next); $smarty->assign('row1',$row1); $str=$smarty->fetch('page.html'); echo $str;
smarty:
<table border="1" rules="all" width="600"> <tr> <td>编号</td> <td>品牌</td> <td>pid</td> <td>删除</td> </tr> {foreach from=$data item='value'} <tr> <td>{$value['id']}</td> <td>{$value['name']}</td> <td>{$value['pid']}</td> <td><a href="#", onclick="del({$value['id']},{$page})">删除</td> </tr> {/foreach} <tr> <td colspan="3"> 共{$total}页 第{$page}页 <a href="#", onclick="init({$prev})">上一页</a> <a href="#", onclick="init({$next})">下一页</a> <a href="#", onclick="init(1)">第一页</a> <a href="#", onclick="init({$total})">最末页</a> 共{$row1}条数据 每页{$pagesize}条数据 </td> </tr> </table>
要引入smary模板
以上所述是小编给大家介绍的ajax+smarty技术实现无刷新分页,希望对大家有所帮助!