php学习笔记(三十二)ajax结合pageView类实现页面无刷新请求

<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

<?php
	header("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

<?php
	class 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;
			else
				return null;
		}

		private function start(){
			if($this->total==0)
				return 0;
			else
				return ($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>";
				else
					break;
			}
			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;
		}
	}


你可能感兴趣的:(Ajax)