<?
	require_once("json.php");

	$p=floor(trim($_GET["p"]));
	$p=$p<=0?1:$p;
	$q=floor(trim($_GET["q"]));
	$q=$q<=0?20:$q;

	function ShowTable($ds,$uri){
		global $p,$q;
		
		$uri=$uri."&p=$p&q=$q";
		$str="<script language='javascript'>var ds=\"$ds\";</script><script language='javascript' src='".$uri."'></script><script language='javascript' src='script.js'></script>";
		echo $str;
	}

	function ShowTable2($ds,$uri){
		global $p,$q;

		$str=file_get_contents($uri."&p=$p&q=$q");
		$json=new Services_JSON();
		$obj=$json->decode($str);
		$headers=$obj->headers;
		$lists=$obj->lists;
		$total=$obj->total;

		$row=count($headers);
		$line=count($lists);

		echo "<table id='list'>";

		echo "<tr>";
		for($i=0;$i<$row;$i++){
			echo "<th>".mb_convert_encoding($headers[$i],"GB2312","UTF-8")."</th>";
		}
		echo "</tr>";

		for($j=0;$j<$line;$j++){
			echo "<tr>";
			for($k=0;$k<$row;$k++){
				echo "<td>".$lists[$j][$k]."</td>";
			}
			echo "</tr>";
		}

		echo "<tr>";
		echo "<th id='page' colspan='".$row."'>";
		echo ShowPage($ds,$total,$p,$q);					
		echo "</th>";
		echo "</tr>";

		echo "</table>";

	}

	function ShowPage($ds,$total,$p,$q){
		$page_min=1;
		$page_pre=$p-1;
		$page_nex=$p+1;
		$page_max=ceil($total/$q);

		$page_pre=$page_pre<$page_min?$page_min:$page_pre;
		$page_nex=$page_nex>$page_max?$page_max:$page_nex;

		$page_sta=$p-5<1?1:$p-5;
		$page_end=$p+5>$page_max?$page_max:$p+5;					
		
		$page_sta=$p+5>$page_max?$page_max-10:$page_sta;
		$page_end=$p-5<0?11-$page_sta:$page_end;

		$page_sta=$page_sta<1?1:$page_sta;
		$page_end=$page_end>$page_max?$page_max:$page_end;

		$str="";
		for($i=$page_sta;$i<=$page_end;$i++){
			if($i==$p){
				$str.="&nbsp;<font color='red'><b>".$i."</b></font>&nbsp;";
			}
			else{
				$str.="&nbsp;<a href='?ds=$ds&p=$i&q=$q' target='_self'>".$i."</a>&nbsp;";
			}
		}
		$str.="|| 第 $p/$page_max 页 共 ".floor($total)." 条记录";
		return $str;	
	}

	function SendTableData($table){
		// 格式化
		$json=new Services_JSON();
		$output=$json->encode($table);

		// 输出
		ob_clean();
		$str="obj=$output;";
		exit($str);
	}
?>