class分页类连接数据库类

custom.class.inc.php

<?php
// 连接数据库的类
class ConnDB
{
  var $dbtype;
  var $host;
  var $usr;
  var $pwd;
  var $dbname;
  var $conn;
  // 构造方法
  function ConnDB($dbtype,$host,$user,$pwd,$dbname)
  {
    $this->dbtype = $dbtype;
    $this->host = $host;
    $this->pwd = $pwd;
    $this->dbname = $dbname;
    $this->user = $user;
  }
  function GetConn()
  {
   $this->conn = mysql_connect($this->host,$this->user,$this->pwd) or die("数据库服务器连接错误".mysql_error()); 
   mysql_select_db($this->dbname,$this->conn) or die("数据库访问错误".mysql_error());
   mysql_query("set names 'utf8'");
   return $this->conn;
  }
  function __destruct()
  {
   $this->CloseDB(); 
  }
  function CloseDB()
  {
   mysql_close($this->conn);
  }
  
}
// 数据库操作类
class OperateDB
{
 function Execsql($sql,$conn)
 {
   $sqltype = strtolower(substr(trim($sql),0,6));// 截取sql语句中的前6个字符串,并转换成小写
   $result = mysql_query($sql,$conn);// 执行sql语句
   $calback_arrary = array();// 定义二维数组
   if ("select" == $sqltype)// 判断执行的是select语句
   {
    
    if (false == $result)
    {
     return false; 
    }
    else if (0 == mysql_num_rows($result))
    {
     return false;
    }
    else
    {
     while($result_array = mysql_fetch_array($result))
     {
     array_push($calback_arrary, $result_array);
     }
     return $calback_arrary;// 成功返回查询结果的数组 
    }
   }
   else if ("update" == $sqltype || "insert" == $sqltype || "delete" == $sqltype)
   {
     if ($result)
     {
      return true;
     }
     else
     {
      return false;
     }
   }
 } 
}

// 数据分页类
class PageAlg
{
 var $pagesize;// 每页显示的条数
 var $nowpage;// 当前页
 var $conn;// 数据库连接
 var $sqlstr;// sql语句
 var $pagetotal;// 获取当前页显示的记录数
 var $pagecount;// 总的条数
 var $result;// 执行查询语句的结果
 var $arrays;// 返回查询到的数据二维数组
 public function GetData($sqlstr,$conn,$pagesize,$nowpage)
 {
  $this->arrays = array();// 定义数组返回当前页的数据
  if (!isset($nowpage) || $nowpage=="" || 0==$nowpage)// 如果当前页的变量值为空或0
  {
   $this->nowpage = 1;// 则判断当前页为第一页
  }
  else
  {
   $this->nowpage = $nowpage;// 否则当前页为传进的值
  }
  $this->pagesize = $pagesize;// 定义每页显示数据条数
  $this->conn = $conn;
  $startIndex = $this->pagesize*($this->nowpage-1);
  $this->sqlstr = $sqlstr." limit $startIndex,$this->pagesize";
  $this->result = mysql_query($this->sqlstr,$conn);
  $this->sqlstr = $sqlstr;
  if (false == $this->result)// 查询数据库失败!!
  {
   return false;
  }
  $this->pagetotal = mysql_num_rows($this->result);// 获取查到的条数,不一定都是pagesize这么多条
  if (0 == $this->pagetotal)// 查询到的为0条说明数据库中不存在数据
  {
   return false;
  }
  else if ($this->pagetotal <= $this->pagesize)
  {
   
  }
  while($result_array = mysql_fetch_array($this->result))
  {
   array_push($this->arrays,$result_array);// 把数据增加到二维数组中 
  }
  // 返回二维数组
  return $this->arrays;
 }
 public function Showpage($contentname,$unit,$anotersearchstr,$class)
 {
  $allrs = mysql_query($this->sqlstr,$this->conn);// 查询数据库总改表的所有数据
  $record = mysql_num_rows($allrs);// 获得总的记录数
  $pagecount = ceil($record/$this->pagesize);//计算总的页数.2.22取3
        $str.= "共有".$contentname."&nbsp;".$record."&nbsp;".$unit."&nbsp;每页显示&nbsp;".$this->pagesize."&nbsp;".$unit."&nbsp;第&nbsp;".$this->nowpage."&nbsp;页/共&nbsp;".$pagecount."&nbsp;页"; 
  $str.="&nbsp;&nbsp;&nbsp;&nbsp;";
  $str.="<a href=".'"'.$_SERVER["PHP_SELF"]."?page=1".$anothersearchstr.'"'."class=".$class.">首页</a>";
  $str.="&nbsp;";
  if (($this->nowpage-1)<=0)
  {
   $str.="<a href=".'"'.$_SERVER['PHP_SELF']."?page=1".$anothersearchstr.'"'."class=".$class.">上一页</a>";
  }
  else
  {
   $str.="<a href=".'"'.$_SERVER['PHP_SELF']."?page=".($this->nowpage-1).$anothersearchstr.'"'."class=".$class.">上一页</a>";
   
  }
  $str.="&nbsp;";
  if (($this->nowpage+1)>=$pagecount)
  {
    $str.="<a href=".'"'.$_SERVER['PHP_SELF']."?page=".$pagecount.$anothersearchstr.'"'."class=".$class.">下一页</a>"; 
  }
  else
  {
   
    $str.="<a href=".'"'.$_SERVER['PHP_SELF']."?page=".($this->nowpage+1).'"'.$anothersearchstr."class=".$class.">下一页</a>"; 
  }
  
  $str.="&nbsp;";
  $str.="<a href=".'"'.$_SERVER['PHP_SELF']."?page=".$pagecount.$anothersearchstr.'"'."class=".$class.">尾页</a>"; 
  if (count($this->arrays)==0 || $this->result==false)
  {
   return "";
  }
  else
  {
   return $str;
  }


 }
}

?>

引用:

conndb.inc.php

<?php

require("custom.class.inc.php");
$ccon = new ConnDB("mysql","localhost","root","root","think_cms");
$operatedb = new OperateDB();
$conn = $ccon->GetConn();
$page = new PageAlg();

?>

<?php

require("conndb.inc.php");

$sqlstr = "select * from cms_post";
$result = $page->GetData($sqlstr,$conn,2,$_GET['page']);
if ($result)
{
foreach($result as $show)
{
 echo $show['post_body']."<br>";
}
 echo $page->ShowPage("公告","条","","a1");
}
else
 echo '数据库没有数据!!!';

 

?>


 

你可能感兴趣的:(class分页类连接数据库类)