Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用数组项分页_Php_Arrays_Codeigniter_Pagination - Fatal编程技术网

在php中使用数组项分页

在php中使用数组项分页,php,arrays,codeigniter,pagination,Php,Arrays,Codeigniter,Pagination,如果可能的话,我想要一些帮助 我有一个数组,其中包含一个数组项列表(将其视为一个帖子列表),如下所示: $array = array( array( 'title' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit', 'date' => 'Tuesday 28th July 2015', '

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

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

$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个元素

  • 然后当我移动到下一个分页页面(即参考资料/blog/3)时 要获取接下来的3个项目

  • 当我单击下一页(即resources/blog/6)时,接下来的3个项目 等等
取而代之的是,在第一页,我得到了前3个数组项,正如预期的那样,在第二页,我得到了下3个数组项,正如预期的那样,但在下一页,我仍然得到了与第二页相同的项(不是预期的),所以我猜我在数组中进行切片的方式有问题(请在**中检查我的代码)


有没有办法解决这个问题?提前感谢。

假设您的
$offset
会根据页码进行相应更改,则应为

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

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

这应该有助于您理解逻辑:

$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
}
$per_page=3;
$total_rows=计数($array);
$pages=ceil($total_rows/$per_page);
$current\u page=isset($\u GET['i'])$_GET['i']:1;
$current\u page=($total\u rows>0)?最小($pages,$current_page):1;
$start=$当前页面*$每页-$每页;
$slice=array\u slice($array,$start,$per\u page);
foreach($k=>v){
回显“”。打印($v,true)。“”;
}
如果($pages>0){
//显示分页
}

我认为问题在于:

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

函数array\u slice():
array\u slice(array$array,int$offset[,int$length=NULL[,bool$preserve\u keys=false])
,因此正确的方法是:
$this->data['array']=array\u slice($array,$offset,$per\u page)

如下所示修改代码

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

它将工作并按页面显示数据。

假设
$current\u page=1
$max\u results=10

$offset
是数组切片的起点,
$max\u results
是切片的长度。

这将为我们提供一个新数组,该数组由
$results[0]
$results[9]

组成,最后一行代码中的每页$值始终为3,因此该数组将始终从第一页之后的第三页开始返回结果。您需要根据您所处的页面更新$per_page,尽管我认为不同的$value会更好,因为$per_page用于说明每页显示多少结果。是的!这似乎工作正常!非常感谢您的帮助!:-)
$offset = $max_results * ($current_page - 1);
$results = array_slice($results, $offset, $max_results);