<?php /* * 文件名:datagridclass.php * 作者:感染源 * 时间:2007-07-25 * 描述:分页类 */error_reporting(0);class datagridclass{ private $conn; private $result; private $resultArr = array(); public $beginrecord = 0; public $totalrecords = 0; public $totalpages = 0; public $currentpage = 0; public $maxline = 0; public $maxlist = 0; function __construct($pHost, $pName, $pPwd, $pDbName, $pMaxLine = 15, $pMaxList = 8) { $this->conn = mysql_connect($pHost, $pName, $pPwd) or die('DataBase connecting false...'); mysql_select_db($pDbName, $this->conn) or die('Choice database false...'); $this->maxline = $pMaxLine; $this->maxlist = $pMaxList; }//function __construct(); function __set($property_name, $value) { $this->$property_name = $value; }//function __set(); function __destruct() { mysql_free_result($this->result); mysql_close($this->conn); }//function __destruct(); function readdata($pSql) { $this->result = mysql_query($pSql, $this->conn) or die('Select false...'); $this->totalrecords = mysql_num_rows($this->result); if (!$this->totalrecords) { return false; } else { $this->totalpages = ceil($this->totalrecords / $this->maxline); $pSql .= ' LIMIT '.$this->beginrecord.','.$this->maxline; $this->result = mysql_query($pSql, $this->conn) or die('Select false...'); $i = 0; while ($row = mysql_fetch_array($this->result)) { $this->resultArr[$i] = $row; $i++; }//while }//if return $this->resultArr; }//function readdata(); function navigate() { if ($this->totalrecords) { $this->currentpage = ($this->beginrecord / $this->maxline) + 1; $firstpage = 0; $prevpage = $this->beginrecord - $this->maxline; $nextpage = $this->beginrecord + $this->maxline; $lastpage = ($this->totalpages - 1)*$this->maxline; if ($this->beginrecord == 0) { echo '[首页] [上一页] '; } else { echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$firstpage'>[首页]</a> "; echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$prevpage'>[上一页]</a> "; } if ($this->currentpage <= $this->maxlist) { for ($i=1; $i<=$this->maxlist; $i++) { $pagerecord = ($i - 1)*$this->maxline; if ($this->currentpage == $i) { echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$pagerecord'>[$i]</a> "; } else { echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$pagerecord'>$i</a> "; }//if }//for } elseif ($this->currentpage > ($this->totalpages-$this->maxlist)) { for ($i=($this->maxlist); $i>0; $i--) { $pagerecord = ($this->totalpages - $i)*$this->maxline; if ($this->currentpage == ($this->totalpages-$i+1)) { echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$pagerecord'>[".($this->totalpages - $i +1)."]</a> "; } else { echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$pagerecord'>".($this->totalpages - $i+1)."</a> "; }//if }//for } else { $j = ceil($this->maxlist / 2); for ($i = $j; $i>=0; $i--) { if ($this->currentpage == ($this->currentpage-$i)) { echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=".(($this->currentpage-$i-1)*$this->maxline)."'>[".($this->currentpage-$i)."]</a> "; } else { echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=".(($this->currentpage-$i-1)*$this->maxline)."'>".($this->currentpage-$i)."</a> "; } } for ($i = 1; $i<=$j; $i++) { echo "<a href='".$_SERVER['PHP_SELF'].'?beginrecord='.(($this->currentpage+$i-1)*$this->maxline)."'>".($this->currentpage+$i)."</a> "; } } if ($this->beginrecord == $lastpage) { echo '[下一页] [末页]'; } else { echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$nextpage'>[下一页]</a> "; echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$lastpage'>[末页]</a>"; } //echo "$this->currentpage / $this->totalpages,共有记录数:$this->totalrecords"; } }}/*/test$dg = new datagridclass('localhost','root','root','test',2,5);if (isset($_GET['beginrecord'])){ $dg->__set('beginrecord', $_GET['beginrecord']);}else { $dg->__set('beginrecord', 0);}$rs = $dg->readdata('select * from testuser');if(!$rs){ echo '暂无数据……';}else { $i=0; echo "<table width='40%' border='1'>"; echo "<tr><td>id</td><td>name</td></tr>"; while($i<count($rs)) { echo "<tr><td>"; echo $rs[$i]['id']; echo "</td><td>".$rs[$i]['uname']; echo "</td></tr>"; $i++; } echo "</tale>";}$dg->navigate();$dg = NULL;*/?>