php 中的分页显示 123....7 而不是 1234567?

Moh*_*san 1 php pagination

好吧,我有一个类分页

<?php
class Pagination {

  public $current_page;
  public $per_page;
  public $total_count;

  public function __construct($page=1, $per_page=20, $total_count=0){
    $this->current_page = (int)$page;
    $this->per_page = (int)$per_page;
    $this->total_count = (int)$total_count;
  }

  public function offset() {
    return ($this->current_page - 1) * $this->per_page;
  }

  public function total_pages() {
    return ceil($this->total_count/$this->per_page);
    }

  public function previous_page() {
    return $this->current_page - 1;
  }

  public function next_page() {
    return $this->current_page + 1;
  }

    public function has_previous_page() {
        return $this->previous_page() >= 1 ? true : false;
    }

    public function has_next_page() {
        return $this->next_page() <= $this->total_pages() ? true : false;
    }


}

?>
Run Code Online (Sandbox Code Playgroud)

这是我在 index.php 中使用它的方法

<?php

$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = 4;
$total_count = class::count_all();

$pagination = new Pagination($page, $per_page, $total_count);

$sql = "SELECT * FROM tablename ";
$sql .= "LIMIT {$per_page} ";
$sql .= "OFFSET {$pagination -> offset()}";

?>

<?php foreach($variables as $variable):
?>

<?php endforeach;?>

    <?php
    if ($pagination -> total_pages() > 1) {

        if ($pagination -> has_previous_page()) {
            echo "<a href=\"index.php?page=";
            echo $pagination -> previous_page();
            echo "\">&laquo; Previous</a> ";
        }

        for ($i = 1; $i <= $pagination -> total_pages(); $i++) {
            if ($i == $page) {
                echo " <span class=\"selected\">{$i}</span> ";
            } else {
                echo " <a href=\"index.php?page={$i}\">{$i}</a> ";
            }
        }

        if ($pagination -> has_next_page()) {
            echo " <a href=\"index.php?page=";
            echo $pagination -> next_page();
            echo "\">Next &raquo;</a> ";
        }

    }
?>
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果有很多页面,我希望它输出 1 2 3 ....... 10 而不是 1 2 3 4 5 6 7 8 9 10

预先感谢,我真的很抱歉这样打扰你们所有人

小智 5

$delta = 1; // +/- 1 page from current
$start = max(1, $page - $delta);
$end = min($pagination -> total_pages(), $page + $delta);

if ($start > 1) {
    // place first page link
    echo " <a href=\"index.php?page=1\">1</a> "; 
    if ($start > 2) {
        // place "..." if $start is not next to "1"
        echo " ... ";
    }
}
for ($i = $start; $i <= $end; $i++) {
    if ($i == $page) {
        echo " <span class=\"selected\">{$i}</span> ";
    } else {
        echo " <a href=\"index.php?page={$i}\">{$i}</a> ";
    }
}
if ($end < $pagination -> total_pages()) {
    if ($end < $pagination -> total_pages() - 1) {
        // place "..." if $end is not prev to "1"
        echo " ... ";
    }
    // place last page link
    echo " <a href=\"index.php?page={$pagination -> total_pages()}\">{$pagination -> total_pages()}</a>"; 
}
Run Code Online (Sandbox Code Playgroud)

这将生成:

1 2 3 ... 10
Run Code Online (Sandbox Code Playgroud)

如果有 10 页且“2”为当前页,并且

1 ... 3 4 5 ... 10
Run Code Online (Sandbox Code Playgroud)

如果当前页面为“4”,等等。