如何在数组中添加 PHP 分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26451362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:54:36  来源:igfitidea点击:

How to add PHP pagination in array's

phparrayspagination

提问by Arqetech

I've been trying a lot of ways to add PHP pagination. I have tried searching and trying to figure other ways of implementing the pagination but none of them work.

我一直在尝试很多方法来添加 PHP 分页。我尝试搜索并尝试找出实现分页的其他方法,但没有一个有效。

Here's how I created the Index page:

这是我创建索引页面的方式:

<?php
$menuItems = array(
    "post1" => array(
        "title" => "Sample Title",
        "utime" => "M/d/Y",
        "content" => "<p>Body of the post</p>"
    ),

    "post2" => array(
        "title" => "Another Sample Title",
        "utime" => "M/d/Y",
        "content" => "<p>Content goes here...</p>"
    ),
);

foreach ($menuItems as $contItem => $item) {
?>
<li>
     <a href="dish.php?item=<?php echo $contItem; ?>">
         <h1><?php echo $item["title"]; ?></h1>
         <small><?php echo $item["utime"]; ?></small>
     </a>
</li>
<?php } ?>

I would like to know how I can paginate the the array list. Thanks!

我想知道如何对数组列表进行分页。谢谢!

回答by trzyeM-

u can use simple PHP function called array_slice()

您可以使用名为array_slice() 的简单 PHP 函数

$menuItems = array_slice( $menuItems, 0, 10 ); 

show first 10 items.

显示前 10 个项目。

$menuItems = array_slice( $menuItems, 10, 10 );

show next 10 items.

显示接下来的 10 个项目。

UPDATE:

更新:

$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $yourDataArray ); //total items in array    
$limit = 20; //per page    
$totalPages = ceil( $total/ $limit ); //calculate total pages
$page = max($page, 1); //get 1 page when $_GET['page'] <= 0
$page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
$offset = ($page - 1) * $limit;
if( $offset < 0 ) $offset = 0;

$yourDataArray = array_slice( $yourDataArray, $offset, $limit );

UPDATE#2:

更新#2:

Example of pagination:

分页示例:

$link = 'index.php?page=%d';
$pagerContainer = '<div style="width: 300px;">';   
if( $totalPages != 0 ) 
{
  if( $page == 1 ) 
  { 
    $pagerContainer .= ''; 
  } 
  else 
  { 
    $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> &#171; prev page</a>', $page - 1 ); 
  }
  $pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>'; 
  if( $page == $totalPages ) 
  { 
    $pagerContainer .= ''; 
  }
  else 
  { 
    $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> next page &#187; </a>', $page + 1 ); 
  }           
}                   
$pagerContainer .= '</div>';

echo $pagerContainer;

回答by Cranio

Another viable option is to use array_chunk():

另一个可行的选择是使用array_chunk()

$pagedArray = array_chunk($originalArray, 10, true);
$nthPage = $pagedArray[$pageNumber];