codeigniter中的多个分页

时间:2017-04-28 06:04:16

标签: php codeigniter

先谢谢。

在我的项目中,我在单页中使用了2个分页。在我的控制器中,我创建了2个分页。但它不适用于视图。

这是我的控制器代码

第一次分页

 $id = $this->session->userdata('user_id');
       $config['base_url'] = base_url()."my_admin";
        $config['total_rows'] = $this->m_user->get_results_posts_a_count($id);
        $config['uri_segment'] = 2;
        $config['per_page'] = 3;
        $config['use_page_numbers'] = TRUE;
        $val=$this->uri->segment(2);
        $from=$config['per_page'];
        $to=isset($val)?$val:0;
        $config['full_tag_open'] = "<ul class='pagination'>";
        $config['full_tag_close'] ="</ul>";
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
        $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
        $config['next_tag_open'] = "<li>";
        $config['next_tagl_close'] = "</li>";
        $config['prev_tag_open'] = "<li>";
        $config['prev_tagl_close'] = "</li>";
        $config['first_tag_open'] = "<li>";
        $config['first_tagl_close'] = "</li>";
        $config['last_tag_open'] = "<li>";
        $config['last_tagl_close'] = "</li>";


        $this->pagination->initialize($config);
         $this->pagination->create_links();
        $data['a_post'] = $this->post_model->getuser_a_post($id,$from,$to);

第二次分页

$config['base_url'] = base_url()."my_admin";
    $config['total_rows'] = $this->m_user->get_results_posts_count($id);
    $config['uri_segment'] = 2;
    $config['per_page'] = 3;
    $config['use_page_numbers'] = TRUE;
    $val=$this->uri->segment(2);
    $from=$config['per_page'];
    $to=isset($val)?$val:0;
    $config['full_tag_open'] = "<ul class='pagination'>";
    $config['full_tag_close'] ="</ul>";
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
    $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
    $config['next_tag_open'] = "<li>";
    $config['next_tagl_close'] = "</li>";
    $config['prev_tag_open'] = "<li>";
    $config['prev_tagl_close'] = "</li>";
    $config['first_tag_open'] = "<li>";
    $config['first_tagl_close'] = "</li>";
    $config['last_tag_open'] = "<li>";
    $config['last_tagl_close'] = "</li>";


    $this->pagination->initialize($config);
     $this->pagination->create_links();
    $data['a_post'] = $this->post_model->getuser_a_post($id,$from,$to);

如何在此div中的视图文件中使用这两个分页

<!--  code for first pagination -->

<div class="pagination pull-right"><?php echo $this->pagination->create_links();?></div>
                               </div>

<!--  code for second pagination -->



     <div class="pagination pull-right"><?php echo $this->pagination->create_links();?></div>
                                   </div>

2 个答案:

答案 0 :(得分:0)

我发现有多个分页的最好方法是通过ajax现在当我没有使用ajax进行多重分页时我遇到链接问题。

在my_admin的视图

<script type="text/javascript">
$(document).ready(function() {
    function load_questions_data(page){
        $.ajax({
        url:"<?php echo base_url(); ?>admin/example/" + page,
        method:"GET",
        dataType:"json",
        success:function(data) {
            list = '<table class="table table-striped table-bordered">';
            list += '<tbody>';
            list += data.table;
            list += '</tbody>';
            list += '</table>';
            $('.question_list').html(list);
            $('.question_pagination_links').html(data.pagination);
        }
        });
    }

    load_questions_data(1);

    // pag-1 class is created in the $config of pagination on controller
    $(document).on("click", ".pag-1 li a", function(e){
        e.preventDefault();
        e.stopPropagation();

        var page = $(this).data("ci-pagination-page");
        load_questions_data(page);
    });
});
</script>

<script type="text/javascript">
$(document).ready(function() {
    function load_questions_data(page){
        $.ajax({
        url:"<?php echo base_url(); ?>admin/to_another_function/" + page,
        method:"GET",
        dataType:"json",
        success:function(data) {
            list = '<table class="table table-striped table-bordered">';
            list += '<tbody>';
            list += data.table;
            list += '</tbody>';
            list += '</table>';
            $('.question_list').html(list);
            $('.question_pagination_links').html(data.pagination);
        }
        });
    }

    load_questions_data(1);

    // pag-2 class is created in the $config of pagination on controller
    $(document).on("click", ".pag-2 li a", function(e){
        e.preventDefault();
        e.stopPropagation();

        var page = $(this).data("ci-pagination-page");
        load_questions_data(page);
    });
});
</script>
  

注意您可能需要设置一些uri路线https://www.codeigniter.com/user_guide/general/routing.html#examples

$route['admin'] = 'admin/index';
$route['admin/example/(:any)'] = 'admin/example/$1';
$route['admin/(:any)'] = 'admin/index/$1';

控制器上的功能你可以根据自己的喜好命名

<?php 

class Admin extends CI_Controller {

public function __construct() {
  parent::__construct();
  $this->load->model('example_model');
}

public function index() {
    // admin views in here.
}

public function example() {
    $config = array();

    $config["base_url"] = '#';
    $config["total_rows"] = $this->example_model->total_items();
    $config["per_page"] = 2;
    $config["uri_segment"] = 3;
    $config["use_page_numbers"] = TRUE;
    $config["num_links"] = 10;

    $config["full_tag_open"] = '<ul class="pagination pag-1">';
    $config["full_tag_close"] = '</ul>';
    $config["first_tag_open"] = '<li>';
    $config["first_tag_close"] = '</li>';
    $config["last_tag_open"] = '<li>';
    $config["last_tag_close"] = '</li>';
    $config['next_link'] = '&gt;';
    $config["next_tag_open"] = '<li>';
    $config["next_tag_close"] = '</li>';
    $config["prev_link"] = "&lt;";
    $config["prev_tag_open"] = "<li>";
    $config["prev_tag_close"] = "</li>";
    $config["cur_tag_open"] = "<li class='active'><a href='#'>";
    $config["cur_tag_close"] = "</a></li>";
    $config["num_tag_open"] = "<li>";
    $config["num_tag_close"] = "</li>";

    $this->pagination->initialize($config);
    $page = $this->uri->segment(3);

    $start = ($page - 1) * $config["per_page"];

    $output = array(
       'pagination'  => $this->pagination->create_links(),
       'table'   => $this->example_model->get_list($config["per_page"], $start)
    );

    echo json_encode($output);
}

public function to_another_function() {
    $config = array();

    $config["base_url"] = '#';
    $config["total_rows"] = $this->example_model->total_items();
    $config["per_page"] = 2;
    $config["uri_segment"] = 3;
    $config["use_page_numbers"] = TRUE;
    $config["num_links"] = 10;

    $config["full_tag_open"] = '<ul class="pagination pag-2">';
    $config["full_tag_close"] = '</ul>';
    $config["first_tag_open"] = '<li>';
    $config["first_tag_close"] = '</li>';
    $config["last_tag_open"] = '<li>';
    $config["last_tag_close"] = '</li>';
    $config['next_link'] = '&gt;';
    $config["next_tag_open"] = '<li>';
    $config["next_tag_close"] = '</li>';
    $config["prev_link"] = "&lt;";
    $config["prev_tag_open"] = "<li>";
    $config["prev_tag_close"] = "</li>";
    $config["cur_tag_open"] = "<li class='active'><a href='#'>";
    $config["cur_tag_close"] = "</a></li>";
    $config["num_tag_open"] = "<li>";
    $config["num_tag_close"] = "</li>";

    $this->pagination->initialize($config);
    $page = $this->uri->segment(3);

   $start = ($page - 1) * $config["per_page"];

    $output = array(
       'pagination'  => $this->pagination->create_links(),
       'table'   => $this->example_model->get_list($config["per_page"], $start)
    );

    echo json_encode($output);
}

}

模型

<?php

class Example_model extends CI_Model {

    public function get_list($limit, $offset) {
        $this->db->limit($limit, $offset);
        $query = $this->db->get('questions');

        $table = '';

        foreach ($query->result_array() as $result) {
            $table .= '<tr>';
            $table .= '<td>' . $result['title'] . '</td>';
            $table .= '</tr>';
        }

        return $table;
    }

    public function total_items() {
        return $this->db->count_all_results('questions');
    }
}

答案 1 :(得分:0)

尝试使用变量捕获$ this-&gt; pagination-&gt; create_links()的结果,如下所示:

(分页代码1)

$ pagination1 = $ this-&gt; pagination-&gt; create_links();

...

(分页代码2)

$ pagination2 = $ this-&gt; pagination-&gt; create_links();

...

然后,在您的视图中,只需回显您想要相应分页的每个变量。