Ajax+smarty技術實現無重新整理分頁_AJAX相關

來源:互聯網
上載者:User

這裡運用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:<?phpheader('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技術實現無重新整理分頁,希望對大家有所協助!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.