codeigniter 分页结果不显示在下一页

codeigniter pagination-result not display at the next page

我已经在系统中正确实现了 CI 分页。我已将每页限制为 25 个结果。但我想让用户根据需要更改限制,例如 50,75,100。如果用户选择查看 50 个结果,系统在第一页运行完美,但是当用户单击下一页 link 或对 table 列进行排序时,它 return 限制为 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;

  }

任何帮助将不胜感激。

您应该逐页传递 'num'。当您 select 该值时,它会提交表单并重新加载页面。但是它在接下来的页面上没有使用 $_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);