php學習筆記(三十二)ajax結合pageView類實現頁面無重新整理請求

來源:互聯網
上載者:User
<html><head><title>Ajax無重新整理</title><script type="text/javascript" src="ajax.js"></script></head><body><div id="fpage">資料載入中……</div><hr><script type="text/javascript">var cache=new Array();function setPage(url){var obj = document.getElementById("fpage");if('undefined'==typeof(cache[url])){var ajax = Ajax();ajax.get(url,function(data){obj.innerHTML=data;cache[url]=data;});}else{obj.innerHTML=cache[url];}}setPage("demo.php?page=1");</script><script type="text/javascript">document.write(new Date()+"<br>");</script></body></html>


demo.php

<?phpheader("Content-Type:text/html;charset=utf-8");require 'page.class.php';$mysqli = new mysqli("localhost","root","root","hibernate");$result = $mysqli->query("select id from users");$page = new Page($result->num_rows,10);$sql = "select id,name from users order by id {$page->limit}";$result = $mysqli->query($sql);echo '<table align="center" border="1" width=400>';echo '<tr><th>id</th><th>name</th></tr>';while ($row = $result->fetch_assoc()){echo "<tr>";foreach ($row as $col){echo "<td>".$col."</td>";}echo "</tr>";}echo '<tr><td align="right" colspan="2">'.$page->fpage().'</td></tr>';echo '</table>';?>

page.class.php

<?phpclass Page {private $total; //資料表中總記錄數private $listRows; //每頁顯示行數private $limit;private $uri;private $pageNum; //頁數private $config=array('header'=>"records", "prev"=>"prev", "next"=>"next", "first"=>"first", "last"=>"last");private $listNum=8;/* * $total  * $listRows */public function __construct($total, $listRows=10, $pa=""){$this->total=$total;$this->listRows=$listRows;$this->uri=$this->getUri($pa);$this->page=!empty($_GET["page"]) ? $_GET["page"] : 1;$this->pageNum=ceil($this->total/$this->listRows);$this->limit=$this->setLimit();}private function setLimit(){return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}";}private function getUri($pa){$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pa;$parse=parse_url($url);if(isset($parse["query"])){parse_str($parse['query'],$params);unset($params["page"]);$url=$parse['path'].'?'.http_build_query($params);}return $url;}function __get($args){if($args=="limit")return $this->limit;elsereturn null;}private function start(){if($this->total==0)return 0;elsereturn ($this->page-1)*$this->listRows+1;}private function end(){return min($this->page*$this->listRows,$this->total);}private function first(){if($this->page==1)@$html.='';else@$html.="  <a href='javascript:setPage(\"{$this->uri}&page=1\")'>{$this->config["first"]}</a>  ";return $html;}private function prev(){if($this->page==1)@$html.='';else@$html.="  <a href='javascript:setPage(\"{$this->uri}&page=".($this->page-1)."\")'>{$this->config["prev"]}</a>  ";return $html;}private function pageList(){$linkPage="";$inum=floor($this->listNum/2);for($i=$inum; $i>=1; $i--){$page=$this->page-$i;if($page<1)continue;$linkPage.=" <a href='javascript:setPage(\"{$this->uri}&page={$page}\")'>{$page}</a> ";}$linkPage.=" {$this->page} ";for($i=1; $i<=$inum; $i++){$page=$this->page+$i;if($page<=$this->pageNum)$linkPage.=" <a href='javascript:setPage(\"{$this->uri}&page={$page}\")'>{$page}</a> ";elsebreak;}return $linkPage;}private function next(){if($this->page==$this->pageNum)@$html.='';else@$html.="  <a href='javascript:setPage(\"{$this->uri}&page=".($this->page+1)."\")'>{$this->config["next"]}</a>  ";return $html;}private function last(){if($this->page==$this->pageNum)@$html.='';else@$html.="  <a href='javascript:setPage(\"{$this->uri}&page=".($this->pageNum)."\")'>{$this->config["last"]}</a>  ";return $html;}private function goPage(){return '  <input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;setPage(\''.$this->uri.'&page=\'+page+\'\')}" value="'.$this->page.'" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;setPage(\''.$this->uri.'&page=\'+page+\'\')">  ';}function fpage($display=array(0,1,2,3,4,5,6,7,8)){$html[0]="  total:<b>{$this->total}</b>{$this->config["header"]}  ";$html[1]="  pageSize:<b>".($this->end()-$this->start()+1)."</b>,currentSize:<b>{$this->start()}-{$this->end()}</b>  ";$html[2]="  <b>{$this->page}/{$this->pageNum}</b>  ";$html[3]=$this->first();$html[4]=$this->prev();$html[5]=$this->pageList();$html[6]=$this->next();$html[7]=$this->last();$html[8]=$this->goPage();$fpage='';foreach($display as $index){$fpage.=$html[$index];}return $fpage;}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.