Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中分页以显示123…7而不是1234567?_Php_Pagination - Fatal编程技术网

在php中分页以显示123…7而不是1234567?

在php中分页以显示123…7而不是1234567?,php,pagination,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)$pe

我有一个班级分页

<?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;
    }


}

?>

下面是我如何在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> ";
        }

    }
?>

我的问题是如果有太多的页面,我希望它能输出1 2 3。。。。。。。10 而不是12345678910

提前谢谢,我真的很抱歉这样打扰你们

$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>"; 
}
如果有10页且“2”为当前页,以及

1 ... 3 4 5 ... 10

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

您尝试了什么?如果($pagination->total_pages()>4){在你的第二个脚本中,我想你可以做
。试试看,我们会帮你的!没有必要为“烦人”道歉这里的人-你不能期望什么都知道。但是Stacker确实喜欢看到人们先把事情做好-主要是因为这是学习
:)
@halfer非常感谢你的帮助,我真的很感激你。不用担心。下次如果可以的话,一定要试着写一些代码-人们会帮你改进的。
1 ... 3 4 5 ... 10