简单php分页,作为备忘录。

 /**
     * 获取所有数据(带分页)
     * @param  string   $table  表名
     * @param  int  $pageNow    显示第几页,默认1
     * @param  string   $where where条件
     * @param  int  $pageSize    每页几条记录,默认5
     * @param bool  $restriction    是否显示分页越界默认限制
     * @author bx
     */
    public function getPageData($table, $pageNow = 1, $where = '', $pageSize = 10,$restriction = true) {
        if(!empty($where)) {
            $where = "where $where";
        }
        $sqlCount = "SELECT count(1) countnum FROM `{$table}` {$where}";
        //获取总页数
        $countData = $this->db->query_first($sqlCount);
        //获取可分页数
        $pageCount = ceil($countData['countnum'] / $pageSize);
        if ($restriction) {
            //限制分页越界
            $pageNow = min($pageCount,max(1,$pageNow));
        }
        $offset = ($pageNow - 1) * $pageSize;
        $sql = "SELECT * FROM `{$table}` {$where} limit {$offset},{$pageSize}";
        return array(
            'pageNow'   => $pageNow,//当前页码
            'pageCount' => $pageCount, //一共多少页
            'pageData'  => $this->db->dataArray($sql),
            'pagePre'   => $pageNow - 1, //上一页
            'pageNex'   => $pageNow + 1, //下一页
        );
    }

 

相关文章:

  • 2021-08-12
  • 2021-09-23
  • 2021-06-22
  • 2021-10-16
猜你喜欢
  • 2021-12-05
  • 2022-12-23
  • 2021-12-02
  • 2022-02-19
相关资源
相似解决方案