如何在php中限制分页中的页面

时间:2017-10-07 17:43:15

标签: php pagination

我想让网页不超过10 这个显示页数的代码

for ($page=1;$page<=$number_of_pages;$page++) {
 echo '<a href="m.search.php?page=' . $page . '">' . $page . '</a> ';
}`

这是输出:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

我想让它像这样

1 2 3 4 5 6 7 8 9 10 ...

如果我在第10页就会显示如下

1 .. 10 11 12 13 14 15 16 17 18 19 20 ... 25

我不知道如何做这项工作

这是来源

<?php
if(isset($_POST['search']))
{
unset($_SESSION['keyword']);
$KeyWord = $_POST['keyword'];
$_SESSION['keyword'] = $KeyWord;
}
$KW = $_SESSION['keyword'];
$results_per_page = 1;
$sql="SELECT * FROM books_data WHERE Book_Title LIKE '%$KW%' OR Book_Author         
LIKE '%$KW%' ORDER BY Book_Title";
$result = mysqli_query($connect, $sql);
$number_of_results = mysqli_num_rows($result);

$number_of_pages = ceil($number_of_results/$results_per_page);

if (!isset($_GET['page'])) {
  $page = 1;
} else {
  $page = $_GET['page'];
}

$this_page_first_result = ($page-1)*$results_per_page;
$sql="SELECT * FROM books_data WHERE Book_Title LIKE '%$KW%' OR Book_Author LIKE '%$KW%' ORDER BY Book_Title LIMIT $this_page_first_result,         $results_per_page";
$result = mysqli_query($connect, $sql);
$query = "SELECT * FROM books_data WHERE Book_Title LIKE '%$KW%' OR  Book_Author LIKE '%$KW%' ORDER BY Book_Title ASC";
$sql = mysqli_query($connect, $query);
$nurows = mysqli_num_rows($sql);
$numrows = $connect->query($query);
if($nurows == 0 OR $nurows == 1)
{
$restext = "result";
}
else
{
$restext = "results";
}
if(isset($_POST['search']))
{
$num = 0;
$desc = "result. Keyword is blank... Please try again.";
}
else
{
$num = '';
$desc = '';
}
?>
<p><strong><?php $num ?> </strong><?php $desc ?> </p>
<?php 
?> <p><strong><?php echo $nurows; ?> </strong> <?php echo $restext;?> for '<?php echo $KW; ?>'</p>
<?php
while($row = mysqli_fetch_array($result)) {

$bookid = $row['Book_ID'];
$booktitle = $row['Book_Title'];
$bookauthor = $row['Book_Author'];
$bookimg = $row['Book_IMG_Name'];

echo "<hr><a href=\"../../pass.php?id=$bookid&kw=$KW\"><img src='../../images/bookimg/$bookimg' class='bookimg'></a><h3><a href=\"../../pass.php?id=$bookid&kw=$KW\">$booktitle</a></hr><p><font style='font-size: 15px;'><font style='font-style: italic;'>by " . $bookauthor . '</p></font><br />';

}

for ($page=1;$page<=$number_of_pages;$page++) {
  echo '<a href="m.search.php?page=' . $page . '">' . $page . '</a> ';
}

1 个答案:

答案 0 :(得分:2)

尝试以下方法:

function pageList($pages, $current_page)
{
    $output = '';
    $previous_page = 0;
    foreach($pages as $page)
    {
        if ($page - $previous_page > 2)
        {
            $output.= '... ';
        }
        else
        if ($page - $previous_page === 2)
        {
            $output.= '... ';
        }

        $output.= "<a href=\"m.search.php?page=$page\">$page</a> ";
        $previous_page = $page;
    }

    return $output;
}

function createPagination($current_page, $number_of_pages)
{
    $page_span = 5;
    $middle_min = max($current_page - $page_span, 1);
    $middle_max = min($current_page + $page_span, $number_of_pages);
    $pages = range($middle_min, $middle_max);
    if ($middle_min !== 1)
    {
        array_unshift($pages, 1);
    }

    if ($middle_max !== $number_of_pages)
    {
        $pages[] = $number_of_pages;
    }

    return pageList($pages, $current_page);
}

$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
$number_of_pages = 25;
echo createPagination($current_page, $number_of_pages);

这将显示所选页面之前和之后的5页。只需编辑$page_span即可确定要显示的页数。这使用一个简单的循环来循环从最小范围到最大范围的值范围。