So I was wondering if it's possible to use Codeigniter's pagination when using a Web Service? We're using REST and since we're not connecting to a database per se, I was wondering how I would handle the total rows call required in the pagination setup?
So this is how I managed to implement it:
//My array to hold the envelope data
$displayArray = $data['response']['envelopes'];
// The pagination controller code
$quantity = 15;
$start = $this->uri->segment(3);
$data['displayArray'] = array_slice($displayArray,$start,$quantity);
$config['base_url'] = 'http://mysite.com/lcb/status/index/';
$config['total_rows'] = count($displayArray);
$config['per_page'] = $quantity;
$this->pagination->initialize($config);
$this->load->view('envelope_status', $data);
// Code for view
<?php $this->load->view('includes/header'); ?>
<div id="container">
<h1>History</h1>
<?php echo $this->pagination->create_links(); ?>
<table>
<tr><th>Envelope ID</th><th>Status</th><th>Date</th></tr>
<?php
foreach ($displayArray as $envelope) { ?>
<tr cellpadding="2"><td style="text-transform: uppercase;"><?php echo $envelope["envelopeId"]; ?></td><td><?php echo $envelope["status"]; ?></td><td><?php echo $envelope["statusChangedDateTime"]; ?></td></tr>
<?php } ?>
</table>
</div>
<?php $this->load->view('includes/footer'); ?>
So there you have it. I still need to see about getting it to display them in newest-to-oldest order, instead of the reverse, but it works!
As long as you can get total number of item you need to fetch, and some items in specific index (eg. id=5 to id=10), You can use codeigniter pagination library
Below is example (in your controller):
<?php
public function some_page($current_page)
{
$per_page = 5; // example
$current_index = ($current_page - 1) * $per_page;
$your_item_list = $this->YOUR_API->get_item_list($current_index, $per_page);
$config['total_rows'] = $this->YOUR_API->get_total_rows();
$config['per_page'] = $item_per_page;
$this->load->library('pagination', $config);
}
?>
Or you could use a trick, fetch all data then use count()
and array_slice()
<?php
$all_items = $this->YOUR_API->get_all_items();
$item_list = array_slice($all_item, $current_index, $per_page);
$config['total_rows'] = count($all_items);
?>