CodeIgniter Forums
Array Pagination - how to page data stored in an array (e.g. from a news feed) - array paging - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Array Pagination - how to page data stored in an array (e.g. from a news feed) - array paging (/thread-19697.html)



Array Pagination - how to page data stored in an array (e.g. from a news feed) - array paging - El Forum - 06-16-2009

[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!


Array Pagination - how to page data stored in an array (e.g. from a news feed) - array paging - El Forum - 11-06-2009

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


Array Pagination - how to page data stored in an array (e.g. from a news feed) - array paging - El Forum - 11-06-2009

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


Array Pagination - how to page data stored in an array (e.g. from a news feed) - array paging - El Forum - 01-19-2010

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


Array Pagination - how to page data stored in an array (e.g. from a news feed) - array paging - El Forum - 01-19-2010

[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


Array Pagination - how to page data stored in an array (e.g. from a news feed) - array paging - El Forum - 01-20-2010

[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.