我已正确地将CI分页实施到系统中。我已经设置了每页限制25个结果。但我想让用户改变限制,如50,75,100,如果他们愿意。如果用户选择查看50个结果,系统在第一页完美运行,但当用户点击下一页链接或排序表列时,它将返回到限制25。
这是我的代码。
视图:
<?php
if(isset($_POST['num']))
{
$selected_option = $_POST['num'] ;
}else{
$selected_option ='';
}
$options = array(25,50,75,100);
?>
<form id="myForm" method="post" action = "">
Show <select name="num" onchange="document.getElementById('myForm').submit()">
<?php
$selected_option = $_POST['num'];
foreach($options as $v){
if($v == $selected_option){
$selected = 'selected = "selected"';
}else{
$selected = '';
}
echo "<option value='$v' $selected>$v</option>";
}
?>
</select> entries
</form>
<table style="width:100%">
<thread>
<?php foreach ($fields as $field_name => $field_display): ?>
<th <?php if($sort_by == $field_name) echo "class=\"sort_$sort_order\"" ?>>
<?php echo anchor("rps/index/$field_name/".
(($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc'), $field_display); ?>
</th>
<?php endforeach; ?>
</thread>
<tbody>
<?php foreach ($record as $key): ?>
<tr>
<?php foreach ($fields as $field_name => $field_display): ?>
<td><?php echo $key->$field_name; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if (strlen($pagination)): ?>
Pages: <?php echo $pagination; ?>
<?php endif; ?>
控制器:
public function index($sort_by='player_item', $sort_order='asc',$offset=0)
{
if (isset($_POST['num']) && !empty($_POST['num']))
$limit = $this->input->post('num');
else
$limit= 25;
$data7['fields'] = array(
'id' => 'ID',
'time' => 'Time',
'player_item' => 'Player Choose',
'comp_item'=> 'Computer Choose',
'result'=> 'Result'
);
$this->load->model('rps_result');
$result = $this->rps_result->history($limit,$offset,$sort_by,$sort_order);
$data7['record'] = $result['rows'];
$data7['num_record'] = $result['number_rows'];
//pagination
$this->load->library('pagination');
$this->load->helper('url');
$config['base_url'] = "http://localhost/xampp/CodeIgniter/index.php/rps/index/$sort_by/$sort_order";
$config['total_rows'] = $data7['num_record'];
$config['per_page'] = $limit;
$config['uri_segment'] = 5;
$this->pagination->initialize($config);
$data7['pagination'] = $this->pagination->create_links();
$data7['sort_by'] = $sort_by;
$data7['sort_order'] = $sort_order;
$this->load->view('rps_result', $data7);
}
模型:
function history($limit,$offset,$sort_by,$sort_order)
{
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array('id','player_item','comp_item','result','time');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'time';
//result query
$query = $this->db->select('id,player_item,comp_item,result,time')
->from('rps')
->limit($limit,$offset)
->order_by($sort_by,$sort_order);
$ret['rows'] = $query->get()->result();
//count query
$query1 = $this->db->select('count(*) as count', FALSE)
->from('rps');
$tmp = $query1->get()->result();
$ret['number_rows'] = $tmp[0]->count;
return $ret;
}
任何帮助都将深受赞赏。
答案 0 :(得分:0)
您应该从页面传递'num'。当您选择该值时,它将获取提交的表单并重新加载页面。但是它在下一页上没有使用$ _POST ['num'],这个值没有通过。最简单的解决方法是将值存储在cookie中。在您的控制器而不是$limit = $this->input->post('num');
做
if($this->input->post('num')){
setcookie('pagination_limit',$this->input->post('num'));
$limit = $this->input->post('num');
}elseif($this->input->cookie('pagination_limit')){
$limit = $this->input->cookie('pagination_limit', true);
}else{$limit = 25;}
在您看来,您还应该使用$_POST['num']
$this->input->cookie('pagination_limit', true);