【问题标题】:How to keep number of row in sql results continue in PHP pagination ?如何保持 sql 结果中的行数在 PHP 分页中继续?
【发布时间】:2017-10-11 14:41:13
【问题描述】:

我有这些 PHP 代码可以生成结果以及分页。

对于此代码,我将其设置为每页 5 行。但问题是当我点击下一页时,数字并没有持续增加。

我想要实现的是当它到达第一页时,数字将是(1-5),第二页(5-10)。

这是我的代码:

index.php

<?php include "config.php"; ?>
    <div>
      <table>
        <thead>
          <tr>
            <th>Number</th>
            <th>name</th>
            <th>address</th>
          </tr>
        </thead>
        <tbody>
            <?php for( $i = 0; $i < count( $results->data ); $i++ ) : ?>
            <tr>
                <td><?php echo $page++; ?></td>
                <td><?php echo $results->data[$i]['name']; ?></td>
                <td><?php echo $results->data[$i]['address']; ?></td>
            </tr>
            <?php endfor; ?>
        </tbody>
      </table>
    </div>                                                    
    <?php echo $Paginator->createLinks($links); ?>

config.php

<?php
$user = "root";
$pass = "";
try {
    $connect = new PDO('mysql:host=localhost;dbname=database', $user, $pass);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
$limit = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 5;
$page  = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
$links = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;
$query = "SELECT * FROM table ORDER BY id DESC";

require_once 'Paginator.php';
$Paginator  = new Paginator($connect, $query);
$results    = $Paginator->getData($limit, $page);
?>

Paginator.php

<?php
class Paginator {
    private $konek;
    private $batas;
    private $halaman;
    private $habeit;
    private $semua;

    public function __construct($conn, $query) {

        $this->konek = $conn;
        $this->habeit = $query;

        $rs= $this->konek->prepare( $this->habeit );
        $rs->execute();
        $this->semua = $rs->rowCount();

    }

    public function getData( $limit = 10, $page = 1 ) {

        $this->batas    = $limit;
        $this->halaman  = $page;

        if ( $this->batas == 'all' ) {
            $query = $this->habeit;
        } else {
            $query = $this->habeit . " LIMIT " . ( ( $this->halaman - 1 ) * $this->batas ) . ", $this->batas";
        }
        $rs = $this->konek->prepare( $query );
        $rs->execute();
        while ( $row = $rs->fetch(PDO::FETCH_ASSOC) ) {
            $results[]  = $row;
        }

        $result         = new stdClass();
        $result->page   = $this->halaman;
        $result->limit  = $this->batas;
        $result->total  = $this->semua;
        $result->data   = $results;

        return $result;
    }

    public function createLinks( $links ) {
        if ( $this->batas == 'all' ) {
            return '';
        }
        $last       = ceil( $this->semua / $this->batas );

        $start      = ( ( $this->halaman - $links ) > 0 ) ? $this->halaman - $links : 1;
        $end        = ( ( $this->halaman + $links ) < $last ) ? $this->halaman + $links : $last;

        $html       = '<ul class="pagination">';

        $class      = ( $this->halaman == 1 ) ? "disabled" : "";
        $html       .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=1">&laquo;</a></li>';
        $html       .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=' . ( $this->halaman - 1 ) . '">&lsaquo;</a></li>';

        if ( $start > 1 ) {
            $html   .= '<li><a href="?limit=' . $this->batas . '&page=1">1</a></li>';
            $html   .= '<li><span class="titik">...</span></li>';
        }

        for ( $i = $start ; $i <= $end; $i++ ) {
            $class  = ( $this->halaman == $i ) ? "active" : "";
            $html   .= '<li class="' . $class . '"><a href="?limit=' . $this->batas . '&page=' . $i . '">' . $i . '</a></li>';
        }

        if ( $end < $last ) {
            $html   .= '<li class="disabled"><span>...</span></li>';
            $html   .= '<li><a href="?limit=' . $this->batas . '&page=' . $last . '">' . $last . '</a></li>';
        }

        $class      = ( $this->halaman == $last ) ? "disabled" : "";
        $html       .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=' . ( $this->halaman + 1 ) . '">&rsaquo;</a></li>';
        $html       .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=' . ( $last ) . '">&raquo;</a></li>';

        $html       .= '</ul>';

        return $html;
    }

}

感谢是否有人可以帮助我解决这个问题。谢谢。

【问题讨论】:

    标签: php mysql pagination


    【解决方案1】:

    代替

    <td><?php echo $page++; ?></td>
    

    使用

    <td><?php echo ($page - 1) * 5 + $i; ?></td>
    

    如果您在第一页,$page 将为 1。对于第一个循环,它将评估为:

    <td><?php echo (1 - 1) * 5 + 1 ?></td>
    

    打印1,以此类推,对于第2页,输出将从6开始

    【讨论】:

    • 它以数字 0 开头。如何解决?
    • 检查 $page 的值,根据代码它不能为 0,除非您从查询字符串中获取 0 值。您还可以将 ($page - 1) * 5 + $i 更改为 ((empty($page) ? 1 : $page) - 1) * 5 + $i.如果 $page 的值为 0,这将使用 1。
    【解决方案2】:

    在您的数字列中使用它。在 for 循环下面。

    $output = ($page - 1) * 5 + $i + 1;
    

    里面

    只需echo $output;

    <td><?php echo $output; ?></td>
    

    【讨论】:

      猜你喜欢
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 2019-01-03
      • 2017-07-27
      相关资源
      最近更新 更多