title: Pagination with array items in php
body: <p>I'd like some help if possible.</p>
  
  <p>I'm having an array that contains a list of array items (think of it as a list of posts) like this:</p>
  
  <pre><code>$array = array(
              array(
                  'title' =&gt; 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
                  'date'  =&gt; 'Tuesday 28th July 2015',
                  'img_src'   =&gt; '1130x280&amp;text=FooBar1',
              ), 
              array(
                  'title' =&gt; 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
                  'date'  =&gt; 'Friday 17th July 2015',
                  'img_src'   =&gt; '350x150&amp;text=FooBar',
              ),
              array(
                  'title' =&gt; 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
                  'date'  =&gt; 'Thursday 9th July 2015',
                  'img_src'   =&gt; '350x150&amp;text=FooBar',
              ),
  );
  </code></pre>
  
  <p>So basicly what I want to do is try to paginate this array properly. The basic part of my code is this:</p>
  
  <pre><code>$per_page = 3;
  $total = count($array);
          if ($total &gt; $per_page) {
  
              $this-&gt;load-&gt;library('pagination');
  
              $config['uri_segment'] = 3;
              $config['per_page'] = $per_page;
              $config['total_rows'] = $total;
              $config['base_url'] = site_url('resources/blog/');
  
              $offset = $this-&gt;uri-&gt;segment(3);
              $this-&gt;pagination-&gt;initialize($config);
              $this-&gt;data['pagination'] = $this-&gt;pagination-&gt;create_links();
          } else {
              $offset = 0;
              $this-&gt;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-&gt;data['array'] = array_slice($array, $per_page, $offset); 
          }
  </code></pre>
  
  <p>So basicly what I want to do is </p>
  
  <ul>
  <li><p>when I'm on the first page of the pagination ( i.e. resources/blog )
  to get the first 3 elements of my array,</p></li>
  <li><p>then when I move to the next pagination page ( i.e resources/blog/3 )
  to get the next 3 items,</p></li>
  <li>when I click the next page ( i.e resources/blog/6 ) the next 3 items
  etc etc..</li>
  </ul>
  
  <p>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 **).</p>
  
  <p>Any ideas how to fix this? Thanks in advance.</p>