Pagination with array items in php

11,425

Solution 1

Assuming your $offset changes accordingly to page number, it should be

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

because array_slice second argument is offset and third is length.

Solution 2

Assuming that $current_page = 1 and $max_results = 10:

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

$offset is the start of the array slice and $max_results is the length of the slice.
This would give us a new array consisting of $results[0] through $results[9].

Share:
11,425
ltdev
Author by

ltdev

Updated on June 04, 2022

Comments

  • ltdev
    ltdev almost 2 years

    I'd like some help if possible.

    I'm having an array that contains a list of array items (think of it as a list of posts) like this:

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

    So basicly what I want to do is try to paginate this array properly. The basic part of my code is this:

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

    So basicly what I want to do is

    • when I'm on the first page of the pagination ( i.e. resources/blog ) to get the first 3 elements of my array,

    • then when I move to the next pagination page ( i.e resources/blog/3 ) to get the next 3 items,

    • when I click the next page ( i.e resources/blog/6 ) the next 3 items etc etc..

    What happens instead is on the first page I get the first 3 array items, as expected, on 2nd page I get the next 3 array items, as expected, BUT on next page I still get the same items like in 2nd page (not expected), so I guess there's something wrong with how I do the slicing in my array (check my code in **).

    Any ideas how to fix this? Thanks in advance.