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!
#2

[eluser]Eric Barnes[/eluser]
I know it is old but thank you. This is just what I was looking for today. Smile
#3

[eluser]Flemming[/eluser]
Pleased it was of some use!
#4

[eluser]god_module[/eluser]
Thanks for sharing! I didn't think of array_slice...
#5

[eluser]maria clara[/eluser]
@ flemming,

its interesting..can you explain more about it??? maybe i could use it for alternative ways of making pagination.



regards,
kahtrina Wink
#6

[eluser]Flemming[/eluser]
@maria clara

it's only useful for data that is stored in an array (such as rss feed) rather than coming from a database where you can use LIMIT to retrieve a specific set of results to correspond with your pagination.




Theme © iAndrew 2016 - Forum software by © MyBB