duanmu1736
duanmu1736
2017-10-07 17:43

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

已采纳

i want to make the pages not go beyond 10 this the code that shows the number of pages

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

this is the output:

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

and i want to make it go like this

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

and if im on page 10 it will show like this

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

i don't have any idea how to make this work

this is the source

<?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条回答

  • duanhun3273 duanhun3273 4年前

    try the following:

    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);
    

    This will dispaly the 5 pages before and after of the selected page. Just edit the $page_span to determine how many of the pages you want to show. This uses a simple loop to loop over range of values from the minimum to maximum range.

    点赞 评论 复制链接分享

相关推荐