Welcome Guest, Not a member yet? Register   Sign In
Array Pagination - how to page data stored in an array (e.g. from a news feed) - array paging
#1

[eluser]Flemming[/eluser]
I needed to page an array of data that was returned from Elliot Haughin's CI port of SimplePie - http://www.haughin.com/code/simplepie/

Here's what I came up with. Simple really, just using PHP's array_slice function:

Code:
$rss_items = $this->simplepie->get_items(); //just for example - $rss_items is my array

$quantity = 5 // how many per page
$start = $this->uri->segment(3); // this is auto-incremented by the pagination class
if(!$start) $start = 0; // default to zero if no $start

// slice the array and only pass the slice containing what we want (i.e. the $config['per_page'] number of records starting from $start)
$data['rss_items'] = array_slice($rss_items, $start, $quantity);

$config['base_url'] = '/page/rss_feed/';
$config['uri_segment'] = 3;
$config['total_rows'] = count($rss_items);
$config['per_page'] = $quantity;

$this->pagination->initialize($config);

$data['pagination'] = $this->pagination->create_links();

Then by the time the $data['rss_items'] arrives at my view it only contains the records that I want, so I can loop through it with a foreach:
Code:
foreach($rss_items as $item)
{
  echo $item->get_title(); // obviously this will depend on how your array is structured
}

I THINK I've explained that right but any questions just give me a shout. I know it's incredibly simple but I thought it was worth posting!


Messages In This Thread
Array Pagination - how to page data stored in an array (e.g. from a news feed) - array paging - by El Forum - 06-16-2009, 05:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB