【问题标题】:Pagination with array items in php在php中使用数组项进行分页
【发布时间】:2015-11-06 05:56:52
【问题描述】:

如果可能的话,我想要一些帮助。

我有一个包含数组项列表的数组(将其视为帖子列表),如下所示:

$array = array(
            array(
                'title' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
                'date'  => 'Tuesday 28th July 2015',
                'img_src'   => '1130x280&text=FooBar1',
            ), 
            array(
                'title' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
                'date'  => 'Friday 17th July 2015',
                'img_src'   => '350x150&text=FooBar',
            ),
            array(
                'title' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
                'date'  => 'Thursday 9th July 2015',
                'img_src'   => '350x150&text=FooBar',
            ),
);

所以基本上我想要做的是尝试正确分页这个数组。我的代码的基本部分是这样的:

$per_page = 3;
$total = count($array);
        if ($total > $per_page) {

            $this->load->library('pagination');

            $config['uri_segment'] = 3;
            $config['per_page'] = $per_page;
            $config['total_rows'] = $total;
            $config['base_url'] = site_url('resources/blog/');

            $offset = $this->uri->segment(3);
            $this->pagination->initialize($config);
            $this->data['pagination'] = $this->pagination->create_links();
        } else {
            $offset = 0;
            $this->data['pagination'] = '';
        }
// this is the tricky part of my script
// where I need to slice the array properly each time 
if ($offset == 0) {
            $data['array'] = array_slice($array, 0, $per_page); 
        } else {
// this is probably where my mistake is **
            $this->data['array'] = array_slice($array, $per_page, $offset); 
        }

所以基本上我想做的是

  • 当我在分页的第一页时(即资源/博客) 获取我的数组的前 3 个元素,

  • 然后当我移动到下一个分页页面(即资源/博客/3) 获取接下来的 3 个项目,

  • 当我单击下一页(即资源/博客/6)时,接下来的 3 项 等等等等。

相反,在第一页上我得到了前 3 个数组项,如预期的那样,在第二页上,我得到了接下来的 3 个数组项,如预期的那样,但在下一页我仍然得到与第二页相同的项目(不是预期的),所以我想我在数组中进行切片的方式有问题(请检查我在 ** 中的代码)。

任何想法如何解决这个问题?提前致谢。

【问题讨论】:

  • 最后一行代码中的 $per_page 值始终为 3,因此该数组将始终从第一页之后的第 3 个开始返回结果。您需要根据您所在的页面更新 $per_page,尽管我认为不同的 $value 会更好,因为 $per_page 用于说明每页显示多少结果。

标签: php arrays codeigniter pagination


【解决方案1】:

假设您的 $offset 会相应地更改页码,它应该是

$this->data['array'] = array_slice($array, $offset, $per_page); 

因为array_slice 第二个参数是偏移量,第三个是长度。

【讨论】:

  • 是的!这似乎工作正常!非常感谢您的帮助! :-)
【解决方案2】:

假设$current_page = 1$max_results = 10

$offset = $max_results * ($current_page - 1);
$results = array_slice($results, $offset, $max_results);

$offset 是数组切片的开始,$max_results 是切片的长度。
这将为我们提供一个由$results[0]$results[9] 组成的新数组。

【讨论】:

    【解决方案3】:

    这应该可以帮助您理解逻辑:

    $per_page = 3;
    $total_rows = count($array);
    $pages = ceil($total_rows / $per_page);
    $current_page = isset($_GET['i']) ? $_GET['i'] : 1;
    $current_page = ($total_rows > 0) ? min($pages, $current_page) : 1;
    $start = $current_page * $per_page - $per_page;
    
    $slice = array_slice($array, $start, $per_page);
    foreach ($slice as $k => $v) {
        echo '<pre>' . print_r($v, true) . '</pre>';
    }
    
    if ($pages > 0) {
        // Display Pagination
    }
    

    【讨论】:

      【解决方案4】:

      我认为问题出在:

      } else {
      // this is probably where my mistake is **
      $this->data['array'] = array_slice($array, $per_page, $offset); 
      }
      

      函数array_slice():array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] ),所以正确的做法是:$this-&gt;data['array'] = array_slice($array, $offset, $per_page);

      【讨论】:

        【解决方案5】:

        如下修改你的代码。

            $per_page = 3;
           $total = count($array);
            if ($total > $per_page) {
        
                $this->load->library('pagination');
        
                $config['uri_segment'] = 3;
                $config['per_page'] = $per_page;
                $config['total_rows'] = $total;
                $config['base_url'] = site_url('resources/blog/');
        
                $offset = $this->uri->segment(3);
                $this->pagination->initialize($config);
                $this->data['pagination'] = $this->pagination->create_links();
            } else {
                $offset = 0;
                $this->data['pagination'] = '';
            }
          // this is the tricky part of my script
          // where I need to slice the array properly each time 
         if ($offset == 0) {
                $data['array'] = array_slice($array, 0, $per_page); 
            } else {
            // this is probably where my mistake is **
                $startoffset=$offset - 1;
                $this->data['array'] = array_slice($array,$startoffset, $per_page); 
            }
        

        它会起作用,并且会按页面显示数据。

        【讨论】:

          猜你喜欢
          • 2019-02-11
          • 2015-02-27
          • 1970-01-01
          • 2013-11-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-22
          • 1970-01-01
          相关资源
          最近更新 更多