<?php
/**
分页类 
用法：
	$zpage=new ZF_pagination;
	$zpage->rows_all 	 = 456;
	$zpage->rows_perpage = 10;
	$zpage->page_now 	 = 5;  //$pg;
	$zpage->page_url	 = "../index.php";
//	$zpage->url_mode	 = "submit";

	$navi_str =$zpage->page_tbl_1("新闻列表");
	$navi_str.=$zpage->page_tbl_2("新闻列表");
	$navi_str.=$zpage->navi_tbl_1("新闻列表");

	//从数据库中读取数据,取前n条记录,并跳转至开始显示记录//2002.02.27修改
	$rs=$db->SelectLimit($q,$zpage->SelectLimit);
	$rs->Move($zpage->Moveto);

**/

class ZF_pagination {
	//page参数
    var $rows_all	 	= 0;	//所有记录数
    var $rows_perpage	= 10;	//每页显示记录数
    var $page_now		= 0;	//当前页号

	var $SelectLimit	=0;		//选择前 n 条 记录
	var $Moveto			=0;		//跳转至 n 条 记录

    var $page_first	= 0;	//第一页
    var $page_pre	= 0;	//前一页
    var $page_next	= 0;	//后一页
    var $page_last	= 0;	//最后页
	
    var $page_first_lbl	= "首页";	//第一页
    var $page_pre_lbl	= "前页";	//前一页
    var $page_next_lbl	= "后页";	//后一页
    var $page_last_lbl	= "尾页";	//最后页
	

	//navi参数
    var $pages_all	 	= 0;	//[计算] 所有pages数
    var $pages_pernavi	= 10;	//[参数] 每navi显示pages数
    var $navi_now		= 0;	//[计算] 当前navi号

    var $navi_first	= 0;	//第一页
    var $navi_pre	= 0;	//前n 页
    var $navi_next	= 0;	//后n 页
    var $navi_last	= 0;	//最后页
	
	var $navis_all	= 0;	//所有页数

	//其它参数
	var $page_url_delim		= "";	//链接与pg的分割符号
	var $page_var_name		= "";	//页的变量名
	var $page_url			= "";	//链接的地址

	var $submit_form_name	= "";	//利用form提交方式分页的 form名称
	
	var $url_mode			= "";	//转到另外一页的方法类型(link submit)

	var $has_link_style		= "";	//有连接的样式
	var $no_link_style		= "";	//没有链接的样式
	
	var $suject_color		= "";	//导航兰提示的字体颜色
	
	function ZF_pagination() {
		$this->page_var_name	= "pg";				//页的变量名
		$this->submit_form_name	= "search_form";	//利用form提交方式分页的 form名称
		
		$this->url_mode			= "link";			//转到另外一页的方法类型(link submit)

		$this->has_link_style	= "";									//有连接的样式
		$this->no_link_style	=" style=\"color:".TINT_COLOR."\" ";	//没有链接的样式

		$this->suject_color		=SUBJECT_COLOR;	//导航兰提示的字体颜色
	}
	
	//分页
	function page_exec()  {
		//rows_all,rows_perpage,page_now是需要提供的
		$this->rows_all		= intval($this->rows_all);
		$this->rows_perpage	= intval($this->rows_perpage);
		$this->page_now		= intval($this->page_now);
		
		//1.
		$this->pages_all	= ($this->rows_perpage==0)?(0):(ceil($this->rows_all/$this->rows_perpage));
		
		//2.
		if (($this->page_now<=0)or($this->page_now>$this->pages_all))
		$this->page_now		= 1;
		
		$this->page_first	= 1;
		$this->page_pre		= $this->page_now-1;
		$this->page_next	= $this->page_now+1;
		$this->page_last	= $this->pages_all;

		if ($this->page_pre == 0) {
			$this->page_first	=0;
			$this->page_pre		=0;
		}
		
		if ($this->page_next > $this->page_last) {
			$this->page_next	=0;
			$this->page_last	=0;
		}
		//3.
		$this->page_url_delim = (strpos($this->page_url,"?"))?("&"):("?");
		
		//4.
		$this->SelectLimit	=$this->page_now*$this->rows_perpage;
		$this->Moveto		=$this->rows_perpage*($this->page_now-1);
		
	}  //endof page_exec()

	//分navi
	function navi_exec()  {
		//先分页，
		$this->page_exec();

		//再分navi
		$this->pages_all	= $this->pages_all;
		$this->pages_pernavi= $this->pages_pernavi;
		$this->navi_now		= ceil($this->page_now/$this->pages_pernavi);

		$this->navi_first	= 1;
		$this->navi_pre		= $this->navi_now-1;
		$this->navi_next	= $this->navi_now+1;
		$this->navi_last	=($this->pages_pernavi==0)?(0):(ceil($this->pages_all/$this->pages_pernavi));

		$this->navis_all	= $this->navi_last;

		if ($this->navi_pre == 0) {
			$this->navi_first	=0;	
			$this->navi_pre		=0;
		}
		
		if ($this->navi_next > $this->navi_last) {
			$this->navi_next	=0;
			$this->navi_last	=0;
		}

		if ($this->navi_pre !=0) $this->navi_pre  = $this->pages_pernavi * ($this->navi_now-2)  +1 ;
		if ($this->navi_next!=0) $this->navi_next = $this->pages_pernavi * ($this->navi_now)    +1 ;
		if ($this->navi_last!=0) $this->navi_last = $this->pages_all ;
		
		if ($this->navi_now !=0) $this->navi_now  = $this->pages_pernavi * ($this->navi_now-1)  +1 ;
		
	}  //endof navi_exec()

	//form提交的javascript
	function link_style($page_no) {
		if ($page_no) {
			$ss =" ".$this->has_link_style;
			$ss.="  href=\"".$this->page_url.$this->page_url_delim.$this->page_var_name."=".$page_no."\" ";
			
			if ($this->url_mode=="submit")
				$ss.=" onclick=\"".$this->submit_form_name.".".$this->page_var_name.".value=".$page_no.";"
								  .$this->submit_form_name.".submit();return false;\"";
		} else {
			$ss =$this->no_link_style;
		}
		
		return $ss;
	}

	/*
		每页n条记录的下拉框
	*/
	function rows_perpage_select($select_name,$interval="10;20;50;100") {
		
		$interval=explode(";",$interval);
		for ($arr=array(),$i=0;$i<count($interval);$i++) $arr[$interval[$i]]=$interval[$i];
		
		//$arr[10000]="不限";
		$ss.=ctrl_select(array("opt_arr"=>$arr),$select_name,$this->rows_perpage,"class=select_num");
		
		return $ss;
	}

	/*
		第n页的下拉框
	*/
	function page_select() {
		$arr=array();
		for ($i=1;$i<=$this->pages_all;$i++) $arr[$i]=$i;
		if (count($arr)==0) $arr[0]=0;
		
		if ($this->url_mode=="submit") {
			$js_ext.="onchange=\"";
			$js_ext.=	$this->submit_form_name.".".$this->page_var_name.".value=";
			$js_ext.=	$this->page_var_name.".options[".$this->page_var_name.".selectedIndex].value;";
			$js_ext.=   $this->submit_form_name.".submit();return false;";
			$js_ext.="\"";

		} else {
			$js_ext.="onchange=\"";
			$js_ext.=	"location='".$this->page_url.$this->page_url_delim.$this->page_var_name."='+";
			$js_ext.=	$this->page_var_name.".options[".$this->page_var_name.".selectedIndex].value";
			$js_ext.="\"";
		}
		
		$ss="<style>
				.select_num {font-family: \"Verdana\",\"Arial\",\"Helvetica\",\"sans-serif\"; font-size: 7pt}
			 </style>";

		$ss.=ctrl_select(array("opt_arr"=>$arr),$this->page_var_name,$this->page_now,$js_ext." class=select_num ");

		return $ss;
	}

	//page显示 1
	function page_tbl_1($msg_note="") {
		$this->page_exec();
		
		$navi_str ="\n";
		$navi_str.="<table border=0 cellspacing=0 cellpadding=0 width=".WIDTH_PERCENT." align=center height=20>\n";
		$navi_str.="  <tr>\n";
		$navi_str.="    <td valign=bottom align=left >&nbsp;&nbsp;<font color=\"".$this->suject_color."\">".$msg_note."</font></td>\n";
		$navi_str.="    <td valign=bottom align=right>\n";
		$navi_str.="      <a ".$this->link_style($this->page_first).">".$this->page_first_lbl."</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->page_pre)  .">".$this->page_pre_lbl  ."</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->page_next) .">".$this->page_next_lbl ."</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->page_last) .">".$this->page_last_lbl ."</a>\n";
		$navi_str.="       &nbsp;&nbsp;"; 
		$navi_str.="       页次：<b>".$this->page_now."/".$this->pages_all."</b>页&nbsp;\n";
		$navi_str.="       共<b>".$this->rows_all."</b>条记录 <b>\n";
		$navi_str.="    </td>\n";
		$navi_str.="  </tr>\n";
		$navi_str.="</table>\n\n";
		
		return $navi_str;
	}//endof navi_tbl_1
	
	//page显示 英文
	function page_tbl_en1($msg_note="") {
		$this->page_exec();
		
		$navi_str ="\n";
		$navi_str.="<table border=0 cellspacing=0 cellpadding=0 width=".WIDTH_PERCENT." align=center height=20>\n";
		$navi_str.="  <tr>\n";
		$navi_str.="    <td valign=bottom align=left >&nbsp;&nbsp;<font color=\"".$this->suject_color."\">".$msg_note."</font></td>\n";
		$navi_str.="    <td valign=bottom align=right>\n";
		$navi_str.="      <a ".$this->link_style($this->page_first).">First</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->page_pre)  .">Previous</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->page_next) .">Next</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->page_last) .">Last</a>\n";
		$navi_str.="       &nbsp;&nbsp;"; 
		$navi_str.="       Page:<b>".$this->page_now."/".$this->pages_all."</b>&nbsp;\n";
		$navi_str.="       Total of <b>".$this->rows_all."</b>records <b>\n";
		$navi_str.="    </td>\n";
		$navi_str.="  </tr>\n";
		$navi_str.="</table>\n\n";
		
		return $navi_str;
	}//endof navi_tbl_1

	//page显示 英文
	function page_tbl_en($msg_note="") {
		$this->page_exec();
		
		$navi_str ="\n";
		$navi_str.="<table border=0 cellspacing=0 cellpadding=0 width=100% align=center height=20>\n";
		$navi_str.="  <tr>\n";
		$navi_str.="    <td valign=bottom align=left >&nbsp;&nbsp;<font color=\"".$this->suject_color."\">".$msg_note."</font></td>\n";
		$navi_str.="    <td valign=bottom align=right class=\"main\">\n";
		$navi_str.="       ".$this->page_now."/".$this->pages_all."&nbsp;\n";
		$navi_str.="      <a ".$this->link_style($this->page_pre)  ."><img src=\"../images/previous_arrow.gif\" border=no alt=\"previous\" align=\"absmiddle\"></a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->page_next) ."><img src=\"../images/next_arrow.gif\" border=no alt=\"next\" align=\"absmiddle\"></a>\n"; 
		$navi_str.="       &nbsp;&nbsp;"; 
		$navi_str.="    </td>\n";
		$navi_str.="  </tr>\n";
		$navi_str.="</table>\n\n";
		
		return $navi_str;
	}//endof navi_tbl_1

	//page显示 2
	function page_tbl_2($msg_note="") {
		$this->page_exec();
		
		$navi_str ="\n";
		$navi_str.="<table border=0 cellspacing=0 cellpadding=0 width=".WIDTH_PERCENT." align=center height=20>\n";
		$navi_str.="  <tr>\n";
		$navi_str.="    <td valign=bottom align=left >&nbsp;&nbsp;<font color=\"".$this->suject_color."\">".$msg_note."</font></td>\n";
		$navi_str.="    <td valign=bottom align=right>\n";
		$navi_str.="       ".$this->rows_perpage_select("disp_rows")."条/页&nbsp;\n";
		$navi_str.="       第".$this->page_select()."页&nbsp;\n"; 
		$navi_str.="       共<b>".$this->rows_all."</b>条记录 <b>\n";
		$navi_str.="    </td>\n";
		$navi_str.="  </tr>\n";
		$navi_str.="</table>\n\n";
		
		return $navi_str;
	}//endof navi_tbl_1

	//navi显示 1
	function navi_tbl_1($msg_note="") {
		$this->navi_exec();
		
		$navi_str ="\n";
		$navi_str.="<table border=0 cellspacing=0 cellpadding=0 width=".WIDTH_PERCENT." align=center height=20>\n";
		$navi_str.="  <tr>\n";
		$navi_str.="    <td valign=bottom align=left >&nbsp;&nbsp;<font color=\"".$this->suject_color."\">".$msg_note."</font></td>\n";
		$navi_str.="    <td valign=bottom align=right>\n";
/*
		$navi_str.="      <a ".$this->link_style($this->navi_first).">&lt;&lt;</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->navi_pre)  .">&lt;</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->navi_next) .">&gt;</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->navi_last) .">&gt;&gt;</a>\n"; 
*/
		if ($this->navi_first)
		$navi_str.="      <a ".$this->link_style($this->navi_first).">[第1页]</a>"; 
		if ($this->navi_pre)
		$navi_str.=      "<a ".$this->link_style($this->navi_pre)  .">[前".$this->pages_pernavi."页]</a>\n"; 

		for ($i=$this->navi_now;$i<=min(($this->navi_now+$this->pages_pernavi-1),$this->pages_all);$i++)
			if ($this->page_now==$i) 
				 $navi_str.="      <a style=\"color:".$this->suject_color."\">[<b>".$i."</b>]</a>\n";
			else $navi_str.="      <a ".$this->link_style($i).">".$i."</a>\n";

		if ($this->navi_next)
		$navi_str.="      <a ".$this->link_style($this->navi_next) .">[后".$this->pages_pernavi."页]</a>"; 
		if ($this->navi_last)
		$navi_str.=      "<a ".$this->link_style($this->navi_last) .">[最末页]</a>\n"; 
		
//		$navi_str.="       &nbsp;&nbsp;"; 
//		$navi_str.="       页次：<b>".$this->page_now."/".$this->pages_all."</b>页&nbsp;\n";
		$navi_str.="       共<b>".$this->rows_all."</b>条记录 <b>\n";
		$navi_str.="    </td>\n";
		$navi_str.="  </tr>\n";
		$navi_str.="</table>\n\n";
		
		return $navi_str;
	}//endof navi_tbl_1


	//定义自己的显示样式
	function page_tbl_3($msg_note="") {
		$this->page_exec();
		
		$navi_str ="\n";
		$navi_str.="<table border=0 cellspacing=0 cellpadding=0 width=".WIDTH_PERCENT." align=center height=20>\n";
		$navi_str.="  <tr>\n";
		$navi_str.="    <td valign=bottom align=left >&nbsp;&nbsp;<font color=\"".$this->suject_color."\">".$msg_note."</font></td>\n";
		$navi_str.="    <td valign=bottom align=right>\n";
		$navi_str.="      <a ".$this->link_style($this->page_first).">".$this->page_first_lbl."</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->page_pre)  .">".$this->page_pre_lbl  ."</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->page_next) .">".$this->page_next_lbl ."</a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->page_last) .">".$this->page_last_lbl ."</a>\n";
		$navi_str.="       &nbsp;&nbsp;"; 
		$navi_str.="       页次：<b>".$this->page_now."/".$this->pages_all."</b>页&nbsp;\n";
		$navi_str.="       第".$this->page_select()."页&nbsp;\n"; 
		$navi_str.="       共<b>".$this->rows_all."</b>条记录 <b>\n";
		$navi_str.="    </td>\n";
		$navi_str.="  </tr>\n";
		$navi_str.="</table>\n\n";
		
		return $navi_str;
	}//endof navi_tbl_3

	//page显示 中文
	function page_tbl_cn($msg_note="") {
		$this->page_exec();
		
		$navi_str ="\n";
		$navi_str.="<table border=0 cellspacing=0 cellpadding=0 width=100% align=center height=20>\n";
		$navi_str.="  <tr>\n";
		$navi_str.="    <td valign=bottom align=left >&nbsp;&nbsp;<font color=\"".$this->suject_color."\" >".$msg_note."</font></td>\n";
		$navi_str.="    <td valign=bottom align=right class=\"main\">\n";
		$navi_str.="       ".$this->page_now."/".$this->pages_all."&nbsp;\n";
		$navi_str.="      <a ".$this->link_style($this->page_pre)  ."><img src=\"../images/previous_arrow_cn.gif\" border=no alt=\"前页\" align=\"absmiddle\"></a>\n"; 
		$navi_str.="      <a ".$this->link_style($this->page_next) ."><img src=\"../images/next_arrow_cn.gif\" border=no alt=\"后页\" align=\"absmiddle\"></a>\n"; 
		$navi_str.="       &nbsp;&nbsp;"; 
		$navi_str.="    </td>\n";
		$navi_str.="  </tr>\n";
		$navi_str.="</table>\n\n";
		
		return $navi_str;
	}//endof navi_tbl_1
}// endof class

?>