【问题标题】:Codeigniter PaginationCodeigniter 分页
【发布时间】:2014-01-21 10:43:45
【问题描述】:

我想在 CodeignIter 分页中添加一个下拉菜单,以便用户可以自己选择每页的项目。

$config['per_page'] 的值没有改变。在我点击第 2 页后,会考虑默认值。

任何帮助我怎么能做到这一点。将来我还想在这个网格中添加搜索和按名称排序。所以解决方案应该是合适的。

这是我的文件。

控制器:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class products extends CI_Controller {

    public $ipp = 5;
    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct(){
       parent::__construct();
       /*if(!$this->session->userdata('user_id')){
           redirect(base_url('login'));exit;
       }*/
       $this->load->model('productmodel');
       // $this->load->model(array('usermodel','categorymodel'));
    }

    public function index()
    {
        $config['base_url'] = BASE_URL('products/index');
        $config['total_rows'] = $this->db->count_all_results('products');

        echo $this->ipp;

        if($this->input->post('sel') != ''){
            $this->ipp = $this->input->post('sel');
            $config['per_page'] = $this->input->post('sel');
            $this->pagination->initialize($config); 
        }else{
            $config['per_page'] = $this->ipp;
            $this->pagination->initialize($config); 
        }

        $start = end($this->uri->segments);
        $end = $config['per_page'];
        //$start = 0;

        if($start == '' || $start == 1){
            $start = 0;
        }

        $this->pagination->initialize($config);

        $data['product_data'] = $this->productmodel->getAllProducts($end, $start);
        //$data['categories'] = $this->cateogorymodel->getAllCategories();
        //echo '<pre>'; print_r($data);
        if($this->session->userdata('username') && $this->session->userdata('password')){
            $this->load->view('admin/products/manage', $data);
        }else{
            $this->load->view('admin/login');
        }
    }

    public function add(){

       $this->load->view('admin/products/add');

    }

    public function edit($product_id){

       $row = $this->productmodel->getProductInfo($product_id);

       //var_dump($row->product_id);

       $data['product_id']  = $row->product_id;
       $data['product_name'] = $row->product_name;
       $data['product_details'] = $row->product_details;
       $data['product_price'] = $row->product_price;

       $this->load->view('admin/products/add', $data);

    }

    public function save(){

        $this->load->library('form_validation');

        $this->form_validation->set_rules('product_name', 'Product Name', 'trim|required');
        $this->form_validation->set_rules('product_details', 'Product Details', 'trim|required');
        $this->form_validation->set_rules('product_price', 'Product Price', 'trim|required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('admin/products/add');
        }
        else
        {
            $product_id = $this->input->post('product_id');

           $data['product_name'] = $_POST['product_name'];
           $data['product_details'] = $_POST['product_details'];
           $data['product_price'] = $_POST['product_price'];

           if($product_id == 0){
              //Add
              $this->db->insert('products', $data);
           }else{
              //Edit 
              $this->db->where('product_id', $product_id);
              $this->db->update('products', $data);    
           }

           redirect(BASE_URL('products'));
        }



    }

    public function delete($product_id){
       $data['product_id'] = $product_id;
       $this->db->delete('products',$data); 
       redirect(BASE_URL('products'));
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

查看:

<?php $this->load->view('header'); ?>
<a href="<?php echo BASE_URL('user/logout'); ?>">Logout</a><br>
<a href="<?php echo BASE_URL('products/add'); ?>">Add New Product</a>

<?php
echo form_open(BASE_URL('products/index'));
            $options = array(
                            '' => 'Select',
                             '2' => '2',
                             '50' => '50',
                             '100' => '100');
            echo form_dropdown('sel',$options,'');
echo form_submit('submit','Submit');
?>

<table border="3">

<tr>
  <td>
    Product ID
  </td>
  <td>
    Product Name
  </td>
  <td>
    Product Detail
  </td>
  <td>
    Product Price
  </td>
  <td>
    Actions
  </td>
</tr>

<?php 
for($i = 0;$i<count($product_data);$i++){
?>
<tr>
  <td>
      <?php echo $product_data[$i]['product_id']; ?>
  </td>
  <td>
      <?php echo $product_data[$i]['product_name']; ?>
  </td>
  <td>
      <?php echo $product_data[$i]['product_details']; ?>
  </td>
  <td>
      <?php echo $product_data[$i]['product_price']; ?>
  </td>
  <td>
    <a href="<?php echo BASE_URL('products/edit/'.$product_data[$i]['product_id']); ?>">Edit</a>
    <a href="<?php echo BASE_URL('products/delete/'.$product_data[$i]['product_id']); ?>">Delete</a>
  </td>
</tr>
<?php
}
?>

</table>
<?php echo $this->pagination->create_links(); ?>
<?php $this->load->view('footer'); ?>

型号:

<?php 
class productmodel extends CI_Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function getAllProducts($limit = '',$start = '')
    {
        if($limit != '' && $start != '' && $start != 0){
           $this->db->limit($limit, $start);
        }else if($limit != '' && $start == 0){
           $this->db->limit($limit);
        }
        $result = $this->db->get('products');
        $data = $result->result_array();
        return $data;
    }

    function getProductInfo($product_id)
    {
        $result = $this->db->query('SELECT * FROM products WHERE product_id='.$product_id);
        $row = $result->row();
        return $row;
    }

}

?>

【问题讨论】:

  • 首先检查 $this->ipp 的值是否在选择下拉列表时是否更改,然后检查 getAllProducts 方法及其参数限制并启动
  • 我已经检查了值不要留在 $config['per_page'] 中...但是 $this->ipp 的值发生了变化...当我转到任何其他页面时会出现问题.. . 然后显示默认行数!!!
  • 我写了关于 Codeigniter 分页的教程。 cloudways.com/blog/pagination-in-codeigniter。看看并提出您的建议

标签: php codeigniter pagination


【解决方案1】:

你正在运行 $this->paggination->initialize($config) 3 次。没有必要这样做,也没有使用更改这么多变量的值。看:

if($this->input->post('sel') != ''){
        $this->ipp = $this->input->post('sel');
        $config['per_page'] = $this->input->post('sel');
        **$this->pagination->initialize($config);** 
    }else{
        $config['per_page'] = $this->ipp;
        **$this->pagination->initialize($config);** 
    }

    $start = end($this->uri->segments);
    $end = $config['per_page'];
    //$start = 0;

    if($start == '' || $start == 1){
        $start = 0;
    }

    **$this->pagination->initialize($config);**

尝试只运行:

    if(!empty($this->input->post('sel')){
        $this->ipp = $this->input->post('sel');
    }

    $config['per_page'] = $this->ipp;
    $start = end($this->uri->segments);
    $end = $config['per_page'];

    if(empty($start) || $start == 1){
        $start = 0;
    }

    $this->pagination->initialize($config);

没有必要使用两次$this-&gt;input-&gt;post('sel') 值。您的 $ipp 值已经以 5 开头。因此,如果$this-&gt;input-&gt;post('sel') 不为空,那么您的$config['per_page'] 将已经是5。现在,如果您确实在 post('sel') 中有某些内容,那么您只需更改 $ipp 的值并让代码流自行更改 $config['per_page']

【讨论】:

猜你喜欢
  • 2016-07-07
  • 2023-03-16
  • 2017-01-17
  • 2012-07-08
  • 1970-01-01
相关资源
最近更新 更多