【问题标题】:php pagination script implementationphp分页脚本实现
【发布时间】:2015-08-22 13:08:48
【问题描述】:

我在 [http://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928][1] 遇到了这个 php 分页教程 我不太了解它,因此很难实现它。

这是进入 Paginator.class.php 文件的代码:

<?php
class Paginator {
    private $_conn;
    private $_limit;
    private $_page;
    private $_query;
    private $_total;

    public function __construct( $conn, $query ) {
        $this->_conn = $conn;
        $this->_query = $query;
        $rs= $this->_conn->query( $this->_query );
        $this->_total = $rs->num_rows;
    }

    public function getData( $limit = 10, $page = 1 ) {
        $this->_limit   = $limit;
        $this->_page    = $page;
        if ( $this->_limit == 'all' ) {
            $query      = $this->_query;
        } else {
            $query      = $this->_query . " LIMIT " . ( ( $this->_page - 1 ) * $this->_limit ) . ", $this->_limit";
        }
        $rs             = $this->_conn->query( $query );
        while ( $row = $rs->fetch_assoc() ) {
            $results[]  = $row;
        }
        $result         = new stdClass();
        $result->page   = $this->_page;
        $result->limit  = $this->_limit;
        $result->total  = $this->_total;
        $result->data   = $results;
        return $result;
    }

    public function createLinks( $links, $list_class ) {
        if ( $this->_limit == 'all' ) {
        return '';
    }

    $last       = ceil( $this->_total / $this->_limit );
    $start      = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links : 1;
    $end        = ( ( $this->_page + $links ) < $last ) ? $this->_page + $links : $last;
    $html       = '<ul class="' . $list_class . '">';
    $class      = ( $this->_page == 1 ) ? "disabled" : "";
    $html       .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">&laquo;</a></li>';
    if ( $start > 1 ) {
        $html   .= '<li><a href="?limit=' . $this->_limit . '&page=1">1</a></li>';
        $html   .= '<li class="disabled"><span>...</span></li>';
    }
    for ( $i = $start ; $i <= $end; $i++ ) {
        $class  = ( $this->_page == $i ) ? "active" : "";
        $html   .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . $i . '">' . $i . '</a></li>';
    }
    if ( $end < $last ) {
        $html   .= '<li class="disabled"><span>...</span></li>';
        $html   .= '<li><a href="?limit=' . $this->_limit . '&page=' . $last . '">' . $last . '</a></li>';
    }
    $class      = ( $this->_page == $last ) ? "disabled" : "";
    $html       .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page + 1 ) . '">&raquo;</a></li>';

    $html       .= '</ul>';
    return $html;
    }
}
?>

这是我放在 index.php 文件中的:

<?php
    require_once 'Paginator.class.php';
    $conn       = new mysqli( 'host', 'user', 'password', 'database' );
    $limit      = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 25;
    $page       = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
    $links      = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;
    $query      = "select DATE_FORMAT(GDate,'%d/%m/%Y') as dd,name,comment,location from mytable where !GAllow=0 order by GDate desc";
    $Paginator  = new Paginator( $conn, $query );
    $results    = $Paginator->getData( $page, $limit );
?>

<!DOCTYPE html>
    <head>
        <title>PHP Pagination</title>
        <link rel="stylesheet" href="css/bootstrap.min.css">
    </head>
    <body>
        <div class="container">
                <div class="col-md-10 col-md-offset-1">
                <h1>PHP Pagination</h1>
                <table class="table table-striped table-condensed table-bordered table-rounded">
                        <thead>
                               <tr>
                                <th>Name</th>
                                <th width="20%">Message</th>
                                <th width="20%">City</th>
                                <th width="25%">Date</th>
                        </tr>
                        </thead>
                        <tbody>

                        <?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?>
        <tr>
                <td><?php echo $results->data[$i]['name']; ?></td>
                <td><?php echo $results->data[$i]['comment']; ?></td>
                <td><?php echo $results->data[$i]['location']; ?></td>
                <td><?php echo $results->data[$i]['dd']; ?></td>
        </tr>
<?php endfor; ?>

                        </tbody>
                </table>
                <?php echo $Paginator->createLinks( $links, 'pagination pagination-sm' ); ?> 

                </div>
        </div>
        </body>
</html>

当页面运行时,第 25 页也只显示了一行。单击下一页将单行添加到表中。怎么了?

【问题讨论】:

  • 看来是作者搞乱了传递给函数的参数顺序。

标签: php pagination


【解决方案1】:

是的。 __construct( $conn, $query )getData( $limit = 10, $page = 1 )createLinks( $links, $list_class )class Paginator 的方法。

因此,您的 Paginator 类最终将如下所示:

class Paginator {

    private $_conn;
    private $_limit;
    private $_page;
    private $_query;
    private $_total;

    public function __construct( $conn, $query ) { ... }
    public function getData( $limit = 10, $page = 1 ) { ... }
    public function createLinks( $links, $list_class ) { ... }

}

【讨论】:

  • Berriel,我确实做到了……但是,我得到了同样的错误。在索引页面中,我已经包含了 require_once 'Paginator.class.php';......
  • Berriel,我已经按照你的要求显示了代码。
  • 您的查询似乎失败了。在分配$this-&gt;_total = $rs-&gt;num_rows; 之前检查$rs 是否为假。你测试过你的查询吗?
  • 是的,查询中有错误。该脚本是否设计为每页显示一行?当我点击下一页时,只会增加一个。
  • 教程中好像有错误,你应该把public function getData( $limit = 10, $page = 1 )切换到这个public function getData( $page = 1, $limit = 10 )...如果答案对你有帮助,别忘了标记为已接受答案
猜你喜欢
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 2011-06-24
  • 2011-04-11
相关资源
最近更新 更多