如何添加分页PHP在数组 [英] How to add PHP pagination in array's

查看:115
本文介绍了如何添加分页PHP在数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想了很多办法来添加分页PHP这里阵列的动态PHP数组的一个例子: HTTP ://arqetech.net/arrayTest/ 现在如果内容的增长高达500个或更多的页面将加载速度较慢,所以我对分页据我已经尝试了很多办法,但他们没有工作,就像我希望它的工作。

I've been trying a lot of ways to add PHP pagination in arrays here's an example of the dynamic php array: http://arqetech.net/arrayTest/ Now If the content grows upto 500 or more the page would load slower, So I to paginate It I have tried a lot of ways but none of them work like I want it to work.

下面就是我创建的索引页:

Here's how I created the Index page:

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

在这里我把数据或数组的文件:

the file where I put the data or array's :

<?php

$menuItems = array(

"hello-world" => array(

    "title" => "Hello World, This Is My First POST!!!",
    "utime" => "June 13, 2014",
    "content" => "<p>You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man. </p>"

),

"my-second-post-weeheeeee" => array(

    "title" => "one+1",
    "utime" => "There was an ERROR showing some data!",
    "content" => "<p>You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. </p>"

),

);
?>

我该如何向他们展示在网页中:

And how I show them in the page:

<?php

function strip_bad_chars( $input ) {
    $output = preg_replace( "/[^a-zA-Z0-9_-]/", "", $input );
    return $output;
}

if (isset($_GET['item'])) {

    $menuItem = strip_bad_chars( $_GET['item'] );
    $dish = $menuItems[$menuItem];

}

?>
<body>
<ul>
    <li>
        <h1><?php echo $dish["title"]; ?></h1>
        <small><?php echo $dish["utime"]; ?></small>
        <?php echo $dish["content"]; ?>
    </li>
</ul>
</body>

能否请你告诉我怎么分页的邮局在索引页。谢谢!

Can you please show me how to paginate those post's in the Index page. Thank You!

推荐答案

ü可以使用简单的称为PHP函数的 array_slice()

u can use simple PHP function called array_slice()

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

显示前10个项目。

show first 10 items.

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

显示未来10项。

show next 10 items.

更新:

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

更新#2:

分页的例子:

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

这篇关于如何添加分页PHP在数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆