所以我想知道在使用 Web 服务时是否可以使用 Codeigniter 的分页?我们正在使用 REST,并且由于我们本身没有连接到数据库,我想知道如何处理分页设置中所需的总行调用?
问问题
3262 次
2 回答
1
所以这就是我设法实现它的方式:
//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'); ?>
所以你有它。我仍然需要看看让它以最新到最旧的顺序显示它们,而不是相反,但它有效!
于 2013-10-15T21:12:36.223 回答
0
只要你能得到你需要获取的项目总数,以及特定索引中的一些项目(例如 id=5 到 id=10),你可以使用 codeigniter 分页库
下面是示例(在您的控制器中):
<?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);
}
?>
或者你可以使用一个技巧,获取所有数据然后使用count()
和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);
?>
于 2013-10-03T22:27:23.747 回答