rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » PHP

使用PHP+PDO+SMARTY实现分页

1、添加一个smarty_inc.php的文件用来配置smarty

<?php

include_once("Smarty/Smarty.class.php"); //包含smarty类文件

$smarty = new Smarty(); //建立smarty实例对象$smarty

$smarty->config_dir="Smarty/Config_File.class.php";  // 目录变量

$smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存

$smarty->template_dir = "./templates"; //设置模板目录

$smarty->compile_dir = "./templates_c"; //设置编译目录

$smarty->cache_dir = "./cache"; //缓存文件夹

$smarty->cache_lifetime = 60;
//----------------------------------------------------

//左右边界符,默认为{},但实际应用当中容易与JavaScript相冲突

//----------------------------------------------------

$smarty->left_delimiter = "{";

$smarty->right_delimiter = "}";

?>

2、添加一个DB_conn.php的文件,并在此文件中创建一个类用来连接数据库

<?php
class DB_conn{
 /* pdo数据源 */
    protected $db_driver = "";
    protected $db_host = "";
    protected $db_user = "";
    protected $db_password = "";
    protected $db_name = "";
    protected $db_table = "";
    protected $db_ut = "";
    protected $db = "";        //数据库连接句柄

  /* 连接数据库 begin */
    public function db_conn(){
     try{
            $db = new PDO("$this->db_driver:host=$this->db_host;dbname=$this->db_name","$this->db_user","$this->db_password");
            $db->query("set names '$this->db_ut'");
            $this->db = $db;
        } catch(pdoexception $e) {
            die($e->getmessage());
        }
    }
}
?>

3、添加一个Cls_Pagination.php的文件,并创建一个类用于分页

<?php
include("DB_conn.php");

class Cls_Pagination extends DB_conn{//继承DB_conn类

 /* 分页显示参数设置 */
    private $page_size = 2;        //每页显示的记录数目
    private $firstcount = 0;
    private $currentpage = 1;        //页码
    private $records = 0;        //表中记录总数
    private $page_count = 0;    //总页数
    private $pagestring = "";    //前后分页链接字符串
    private $pagecontect;

 function __construct($db_driver,$db_host,$db_name,$db_table,$db_user,$db_password,$db_ut){
  $this->db_driver = $db_driver;
     $this->db_host = $db_host;
     $this->db_name = $db_name;
     $this->db_table = $db_table;
     $this->db_user = $db_user;
     $this->db_password = $db_password;
     $this->db_ut = $db_ut;
     $this->db_conn();
    }

 /* 总记录*/
 private function set_totle(){
  $sr = $this->db->query("select * from `$this->db_table`");
  $this->records = $sr->rowCount();
 }

 /* 总页数*/
 private function set_totle_page(){
  $this->page_count = ceil($this->records / $this->page_size);
 }

 /* 页码处理 begin */
    private function set_page(){
     $page = $_REQUEST[page];
     if (!isset($page) || $page == NULL || preg_match('/[a-zA-Z]/',$page) || $page < 1) {
   $this->currentpage = 1;
     } else {
    if ($page > $this->page_count) {
         $this ->currentpage = $this->page_count;
       } else {
         $this ->currentpage = $page;
       }
     }
    }

 private function getFirstcount(){
  $this->firstcount = ($this->currentpage - 1) * $this->page_size;
 }

 private function set_pagecontect(){
  $sr = $this->db->query("SELECT * FROM `$this->db_table` order by id desc limit $this->firstcount,$this->page_size");
  while($row = $sr->fetch()) {
   $this->pagecontect[] = "$row[0] | $row[1] |$row[2] |$row[3] |$row[4]<br />";
  }
 }

 private function set_pagestring(){
  if ($this->currentpage == 1) {
      $this->pagestring .= "首页&nbsp;上一页&nbsp;";
     } else {
      $prepg = $this -> currentpage - 1; //上一页
      $this->pagestring .= "<a href='pagination.php?page=1'>首页</a>&nbsp;<a href='pagination.php?page=$prepg'>上一页</a>&nbsp;";
     }
     if ($this -> currentpage == $this -> page_count) {
      $this -> pagestring .= "下一页&nbsp;尾页";
     } else {
      $nextpg = $this -> currentpage + 1; //下一页
      $this -> pagestring .= "<a href='pagination.php?page=$nextpg'>下一页</a>&nbsp;<a href='pagination.php?page=$this->page_count'>尾页</a>";
     }
 }

 /* 获取分页链接数据 begin */
    public function get(){
        $page_data[0] = $this->records;        //表中记录的总数
        $page_data[1] = $this->page_count;    //总页数
        $page_data[2] = $this->currentpage;        //当前页码
        $page_data[3] = $this->pagestring;    //'首页'、'上一页'、
//                            //'下一页'、//'尾页'
//                            //--链接样式
//
        $page_data[4] = $this->pagecontect;    //[1]、[2]、[3]
                            //--链接样式
        return $page_data;
    }

 /* 建立分页 begin */
    public function create_page(){
  $this->set_totle();
  $this->set_totle_page();
  $this->set_page();
  $this->getFirstcount();
  $this->set_pagecontect();
  $this->set_pagestring();
    }
}
?>

4、添加一个pagination.php的文件

<?php
 include("smarty_inc.php");
 include("Cls_Pagination.php");
 $db = new Cls_Pagination("mysql","localhost","newdb","message","root","","GBK");
 $db->create_page();
 $r =  $db->get();
$smarty->assign("value",$r[4]);
$smarty->assign("r",$r[3]);
$smarty->display("pagination.htm");
?>

5、在templates文件夹中添加一个pagination.htm的文件

<html>
{foreach from=$value item=id}
{$id}
{/foreach}
{$r}
</html>

顶一下
(0)
踩一下
(0)