php分页功能实现(分页的实现)

发布时间:2022-11-12

本文目录一览:

  1. php怎么实现无刷新分页效果
  2. PHP中通过什么字句实现分页功能
  3. php怎么实现分页
  4. PHP如何实现分页显示?
  5. php 长文章怎么在显示时实现分页

php怎么实现无刷新分页效果

php+ajax实现无刷新分页实现方法具体如下:

  • limit 偏移量,长度;
  • limit 0,7; 第一页
  • limit 7,7; 第二页
  • limit 14,7; 第三页
  • 每页信息条数:7
  • 信息总条数:select count(*) from table
  • 信息总页数:ceil 向上取整(总条数 / 每页条数)

1、分页类具体使用

class Pagination {
    private $total; // 数据表中总记录数
    private $listRows; // 每页显示行数
    private $limit; // mysql 数据库的limit
    private $uri; // 分页信息前面的uri地址
    private $pageNum; // 页数
    private $config = array('header' => "个记录", "prev" => "【上一页】", "next" => "【下一页】", "first" => "【首 页】", "last" => "【尾 页】");
    private $listNum = 8;
    public function __construct($total, $listRows = 10, $pa = "") {
        $this->total = $total;
        $this->listRows = $listRows;
        $this->uri = $this->getUri($pa);
        $this->page = !empty($_GET["page"]) ? $_GET["page"] : 1;
        $this->pageNum = ceil($this->total / $this->listRows);
        $this->limit = $this->setLimit();
    }
    private function setLimit() {
        return "Limit " . ($this->page - 1) * $this->listRows . ", {$this->listRows}";
    }
    private function getUri($pa) {
        $url = $_SERVER["REQUEST_URI"] . (strpos($_SERVER["REQUEST_URI"], '?') ? '' : "?") . $pa;
        $parse = parse_url($url);
        if (isset($parse["query"])) {
            parse_str($parse['query'], $params);
            unset($params["page"]);
            $url = $parse['path'] . '?' . http_build_query($params);
        }
        return $url;
    }
    public function __get($args) {
        if ($args == "limit")
            return $this->limit;
        else
            return null;
    }
    private function start() {
        if ($this->total == 0)
            return 0;
        else
            return ($this->page - 1) * $this->listRows + 1;
    }
    private function end() {
        return min($this->page * $this->listRows, $this->total);
    }
    private function first() {
        $html = "";
        if ($this->page == 1)
            $html .= ' ' . $this->config["first"] . ' ';
        else
            $html .= " <a href='javascript:void(0)' onclick='showPage(\"{$this->uri}page=1\")'>{$this->config["first"]}</a> ";
        return $html;
    }
    private function prev() {
        $html = "";
        if ($this->page == 1)
            $html .= ' ' . $this->config["prev"] . ' ';
        else
            $html .= " <a href='javascript:void(0)' onclick='showPage(\"{$this->uri}page=" . ($this->page - 1) . "\")'>{$this->config["prev"]}</a> ";
        return $html;
    }
    private function pageList() {
        $linkPage = "";
        $inum = floor($this->listNum / 2);
        for ($i = $inum; $i >= 1; $i--) {
            $page = $this->page - $i;
            if ($page < 1)
                continue;
            $linkPage .= " <a href='javascript:void(0)' onclick='showPage(\"{$this->uri}page={$page}\")'>{$page}</a> ";
        }
        $linkPage .= " {$this->page} ";
        for ($i = 1; $i <= $inum; $i++) {
            $page = $this->page + $i;
            if ($page <= $this->pageNum)
                $linkPage .= " <a href='javascript:void(0)' onclick='showPage(\"{$this->uri}page={$page}\")'>{$page}</a> ";
            else
                break;
        }
        return $linkPage;
    }
    private function next() {
        $html = "";
        if ($this->page == $this->pageNum)
            $html .= ' ' . $this->config["next"] . ' ';
        else
            $html .= " <a href='javascript:void(0)' onclick='showPage(\"{$this->uri}page=" . ($this->page + 1) . "\")'>{$this->config["next"]}</a> ";
        return $html;
    }
    private function last() {
        $html = "";
        if ($this->page == $this->pageNum)
            $html .= ' ' . $this->config["last"] . ' ';
        else
            $html .= " <a href='javascript:void(0)' onclick='showPage(\"{$this->uri}page=" . ($this->pageNum) . "\")'>{$this->config["last"]}</a> ";
        return $html;
    }
    private function goPage() {
        return '
        <input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;showPage(\'' . $this->uri . 'page=\'+page+\'\')}" value="' . $this->page . '" style="width:25px"/>
        <input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;showPage(\'' . $this->uri . 'page=\'+page+\'\')" />';
    }
    function fpage($display = array(0, 1, 2, 3, 4, 5, 6, 7, 8)) {
        $html[0] = " 共有<b>{$this->total}</b>{$this->config["header"]} ";
        $html[1] = " 每页显示<b>" . ($this->end() - $this->start() + 1) . "</b>条,本页<b>{$this->start()}-{$this->end()}</b>条 ";
        $html[2] = " <b>{$this->page}/{$this->pageNum}</b>页 ";
        $html[3] = $this->first();
        $html[4] = $this->prev();
        $html[5] = $this->pageList();
        $html[6] = $this->next();
        $html[7] = $this->last();
        $html[8] = $this->goPage();
        $fpage = '';
        foreach ($display as $index) {
            $fpage .= $html[$index];
        }
        return $fpage;
    }
}

2、数据显示

header("content-type:text/html;charset=utf-8");
$link = mysql_connect('localhost','root','111111');
mysql_select_db('shop', $link);
mysql_query("set names utf8");
$css = <<<eof
<style type="text/css">
table {border:1px solid black; width:700px; margin:auto; border-collapse:collapse;}
td {border:1px solid black; }
</style>
eof;
echo $css;
echo "<table><tr><td>序号</td><td>名称</td><td>数量</td><td>价格</td><td>时间</td></tr>";
include "./Pagination.php";
$sql = "select * from sw_goods";
$qry = mysql_query($sql);
$total = mysql_num_rows($qry);
$per = 7;
$page_obj = new Pagination($total,$per);
$sqla = "select * from sw_goods ".$page_obj->limit;
$qrya = mysql_query($sqla);
$pagelist = $page_obj->fpage(array(3,4,5,6,7,8));
$i=1;
while($rsta = mysql_fetch_assoc($qrya)){
    echo "<tr>";
    echo "<td>".$i++."</td>";
    echo "<td>".$rsta['goods_name']."</td>";
    echo "<td>".$rsta['goods_number']."</td>";
    echo "<td>".$rsta['goods_price']."</td>";
    echo "<td>".date("Y-m-d H:i:s",$rsta['goods_create_time'])."</td>";
    echo "</tr>";
}
echo "<tr><td colspan=5>".$pagelist."</td></tr>";
echo "</table>";

3、ajax无刷新分页实现

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html>
<head>
    <title>新建网页</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <script type="text/javascript">
        function showPage(myurl){
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function(){
                if(xhr.readyState==4){
                    var rst = document.getElementById("result");
                    rst.innerHTML = xhr.responseText;
                }
            }
            xhr.open("get",myurl);
            xhr.send(null);
        }
        window.onload = function(){
            showPage("./data1.php");
        }
    </script>
    <style type="text/css">
    </style>
</head>
<body>
    <h2>ajax无刷新分页效果</h2>
    <div id="result"></div>
</body>
</html>

PHP中通过什么字句实现分页功能

写法有很多种,比如: 在 HTML 页面中使用 JavaScript:

$.get("1.php",{参数},function(data){
    $("#shi").html(data);
});

即当点击上一页或下一页时触发一个函数,执行上面的代码,把返回的内容放在指定位置。 data 就是 PHP 文件返回的内容;那个 SQL 语句应该是这样写的:

select * from table order by id desc limit ($page-1)*每页个数,($page)*每页个数

这里用到了 jQuery。 另一种思路是当第一次加载时,把所有的内容都从数据库里读出来,然后在浏览器用 JS 处理分页,在 PHP 发送数据时最好用 JSON 格式,这样更好处理。

php怎么实现分页

/**
 * 获取分页的HTML内容
 * @param integer $page 当前页
 * @param integer $pages 总页数
 * @param string $url 跳转url地址 最后的页数以 'page=x' 追加在url后面
 * @return string HTML内容;
 */
public static function getPageHtml($page, $pages, $url){
    //最多显示多少个页码
    $_pageNum = 5;
    //当前页面小于1 则为1
    $page = $page < 1 ? 1 : $page;
    //当前页大于总页数 则为总页数
    $page = $page > $pages ? $pages : $page;
    //页数小当前页 则为当前页
    $pages = $pages < $page ? $page : $pages;
    //计算开始页
    $_start = $page - floor($_pageNum/2);
    $_start = $_start < 1 ? 1 : $_start;
    //计算结束页
    $_end = $page + floor($_pageNum/2);
    $_end = $_end > $pages ? $pages : $_end;
    //当前显示的页码个数不够最大页码数,在进行左右调整
    $_curPageNum = $_end - $_start + 1;
    //左调整
    if($_curPageNum < $_pageNum && $_start > 1){
        $_start = $_start - ($_pageNum - $_curPageNum);
        $_start = $_start < 1 ? 1 : $_start;
        $_curPageNum = $_end - $_start + 1;
    }
    //右边调整
    if($_curPageNum < $_pageNum && $_end < $pages){
        $_end = $_end + ($_pageNum - $_curPageNum);
        $_end = $_end > $pages ? $pages : $_end;
    }
    $_pageHtml = '<ul class="pagination">';
    if($page > 1){
        $_pageHtml .= '<li><a title="上一页" href="'.$url.'page='.($page-1).'">«</a></li>';
    }
    for ($i = $_start; $i <= $_end; $i++) {
        if($i == $page){
            $_pageHtml .= '<li class="active"><a>'.$i.'</a></li>';
        }else{
            $_pageHtml .= '<li><a href="'.$url.'page='.$i.'">'.$i.'</a></li>';
        }
    }
    if($page < $_end){
        $_pageHtml .= '<li><a title="下一页" href="'.$url.'page='.($page+1).'">»</a></li>';
    }
    $_pageHtml .= '</ul>';
    echo $_pageHtml;
}

PHP如何实现分页显示?

class c_mysql_page {
    var $I_pagesize = 10; // 每页记录数
    var $C_width = '80%'; // 表格宽度
    function c_mysql_page ($I_pagesize=10, $C_width='80%') {
        if (isset($I_pagesize)) {
            $this->I_pagesize = $I_pagesize;
        }
        if (isset($C_width)) {
            $this->C_width = $C_width;
        }
    }
    function page_standard($I_sumrecord,$C_page,$C_url="self",$C_otherpara="",$bgcolor="") {
        global $PHP_SELF,$$C_page;
        if ((!$I_sumrecord) || (!$C_page)) {
            AlertExit("参数不全!");
        }
        if($this->I_pagesize < 1) {
            AlertExit("请设定每页的记录数!");
        }
        if($I_sumrecord < 1) {
            return false;
        }
        if($C_url == "self") {
            $C_url = $PHP_SELF;
        }
        $I_page = $$C_page;
        $I_maxpage = $this->getmaxpage($I_sumrecord);
        $I_page = $this->checkPage($I_maxpage,$I_page);
        echo "";
        echo "";
        echo "共" . $I_sumrecord. "条主题&nbsp;当前第" . $I_page . "/". $I_maxpage. "页";
        if ($I_maxpage > 1) {
            echo "";
            if($I_page > 1 && $I_page < $I_maxpage) {
                echo "首页";
                $pre = $I_page - 1;
                echo "上页";
                $next = $I_page + 1;
                echo "下页";
                echo "末页";
            } elseif($I_page == 1) {
                $next = $I_page + 1;
                echo "下页";
                echo "末页";
            } elseif($I_page == $I_maxpage) {
                echo "首页";
                $pre = $I_page - 1;
                echo "上页";
            }
            echo "转到";
            echo "<select onchange=\"window.location.href=this.value\">";
            for($i=1;$i<=$I_maxpage;$i++) {
                $selected = ($i == $I_page) ? "selected" : "";
                echo "<option value=\"{$C_url}?{$C_otherpara}page={$i}\" {$selected}>第{$i}页</option>";
            }
            echo "</select>";
        }
    }
    function getmaxpage($I_sumrecord) {
        if (!$I_sumrecord) {
            AlertExit("参数不全!");
        }
        return ceil($I_sumrecord / $this->I_pagesize);
    }
    function checkPage($I_maxpage,$I_page) {
        if ($I_page < 1) {
            $I_page = 1;
        } elseif ($I_page > $I_maxpage) {
            $I_page = $I_maxpage;
        }
        return $I_page;
    }
    function GetRecordStartEnd($I_sumrecord,$C_page,$C_url="self",$C_otherpara="") {
        $I_maxpage = $this->getmaxpage($I_sumrecord);
        $I_page = $this->checkPage($I_maxpage,$I_page);
        $start = ($I_page - 1) * $this->I_pagesize;
        $end = $start + $this->I_pagesize;
        return array($start, $end);
    }
}

php 长文章怎么在显示时实现分页

有种偷懒的做法。就是文章太长,显示一段,隐藏另外段落。 文章在后台发布肯定是通过 WEB 编辑器进行的,对吧!你可以在编辑器上增加文章分页显示功能。具体的原理是这样的,你在想要分页的地方插入你自己定义的标记符号比如 #page#,插入到数据库中的 HTML 代码中就会有这样的 #page# 的代码。在前台看的时候,通过程序处理,把文字 HTML 中有 #page# 的东西进行下翻译成文章分页即可!有几个 #page# 就可以判断在当初在后台设置过分几页显示。生成一段 JS 代码,控制 DIV 的显示隐藏就可以实现分页效果。