php新分页代码

<?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 '[首页]&nbsp;[上一页]&nbsp;';
            }
            
else 
            {
                
echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$firstpage'>[首页]</a>&nbsp;";
                
echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$prevpage'>[上一页]</a>&nbsp;";
            }
            
            
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>&nbsp;";
                    }
                    
else 
                    {
                        
echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$pagerecord'>$i</a>&nbsp;";
                    }
//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>&nbsp;";
                    }
                    
else 
                    {
                        
echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$pagerecord'>".($this->totalpages - $i+1)."</a>&nbsp;";
                    }
//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>&nbsp;";
                    }
                    
else 
                    {
                        
echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=".(($this->currentpage-$i-1)*$this->maxline)."'>".($this->currentpage-$i)."</a>&nbsp;";
                    }
                }
                
for ($i = 1$i<=$j$i++)
                {
                    
echo "<a href='".$_SERVER['PHP_SELF'].'?beginrecord='.(($this->currentpage+$i-1)*$this->maxline)."'>".($this->currentpage+$i)."</a>&nbsp;";
                }
            }
            
            
if ($this->beginrecord == $lastpage)
            {
                
echo '[下一页]&nbsp;[末页]';
            }
            
else 
            {
                
echo "<a href='".$_SERVER['PHP_SELF']."?beginrecord=$nextpage'>[下一页]</a>&nbsp;";
                
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;
*/
?>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值