zoukankan      html  css  js  c++  java
  • php+pdo实现分页类代码

    <?php
    class SepPage{
    var $db; //pdo实例
    var $sqlStr; //数据库查询语句
    var $nowPg; //当前页
    var $pageSize; //每页显示的记录数

    function ShowPage($db, $sqlStr, $pageSize, $nowPg){
    $this->db = $db;
    $this->sqlStr = $sqlStr;
    $this->pageSize = $pageSize;
    if (!isset($nowPg) || ($nowPg == '') || ($nowPg == 0)){
    $nowPg = 1;
    }
    $this->nowPg = $nowPg;
    $start = $pageSize * ($nowPg - 1);
    $sqlStr .= " limit ".$start." , ".$pageSize;
    $rst = $db->query($sqlStr);
    if ($db->errorCode() != '00000'){
    print_r($db->errorInfo());
    exit();
    }
    $rstArr = $rst->fetchAll();
    return $rstArr;
    }

    function PageNav($name, $unit, $class){
    $rst = $this->db->query($this->sqlStr);
    if ($this->db->errorCode() != '00000'){
    print_r($this->db->errorInfo());
    exit();
    }
    $rstArr = $rst->fetchAll();
    $recordCount = count($rstArr); //总的记录数
    $lastPg = ceil($recordCount/$this->pageSize); //尾页,即总的页数
    $lastPg = $lastPg ? $lastPg : 1; //没有显示条目,置最后页为1
    $prePg = $this->nowPg - 1;
    $nextPg = (($this->nowPg == $lastPg) ? 0 : ($this->nowPg + 1));

    if ($lastPg <= 1){
    return false;
    }

    $str = "共有".$name."&nbsp;".$recordCount."&nbsp;".$unit.",每页显示&nbsp;".$this->pageSize."&nbsp;".$unit;
    $str .= ",第<select name='toPg' onchange='window.location=\"?page=\"+this.value'>\n";
    for ($i=1; $i<=$lastPg; $i++){
    if ($i == $this->nowPg){
    $str .= "<option value='$i' selected>$i</option>\n";
    }else{
    $str .= "<option value='$i'>$i</option>\n";
    }
    }
    $str .= "</select>页/共&nbsp;".$lastPg."&nbsp;页";

    $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";

    if ($prePg){
    $str .= "<a href=".$_SERVER['PHP_SELF']."?page=1"." class=".$class.">首页</a>";
    $str .= "&nbsp;";
    $str .= "<a href=".$_SERVER['PHP_SELF']."?page=".$prePg." class=".$class.">上一页</a>";
    $str .= "&nbsp;";
    }else{
    $str .= "首页";
    $str .= "&nbsp;";
    $str .= "上一页";
    $str .= "&nbsp;";
    }

    if ($nextPg){
    $str .= "<a href=".$_SERVER['PHP_SELF']."?page=".$nextPg." class=".$class.">下一页</a>";
    $str .= "&nbsp;";
    $str .= "<a href=".$_SERVER['PHP_SELF']."?page=".$lastPg." class=".$class.">尾页</a>";
    $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";
    }else{
    $str .= "下一页";
    $str .= "&nbsp;";
    $str .= "尾页";
    $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";
    }
    return $str;
    }
    }
    ?>

    PDO:

    $dsn = 'mysql:host=localhost;dbname=db_wym';
    $user = 'root';
    $pwd = 'root';
    try{
    $db = new PDO($dsn, $user, $pwd, array(PDO::ATTR_PERSISTENT => true));
    $db->exec('set names utf8');
    }catch (PDOException $e){
    echo "Error message:".$e->getMessage();
    die();
    }

  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    C 语言编程 — 使用 assert 断言进行程序设计
    C 语言编程 — uint8_t / uint16_t / uint32_t /uint64_t
    五月数据库技术通讯丨Oracle 12c因新特性引发异常Library Cache Lock等待
    4场直播丨站撸Oracle、MySQL、医疗、航空
    asyncio
    python 多线程 多进程
    一文详解被阿里腾讯视作核心机密的大数据平台架构
  • 原文地址:https://www.cnblogs.com/hasayaki/p/2888446.html
Copyright © 2011-2022 走看看