如何在Codeigniter中创建分页?

时间:2014-04-16 06:20:24

标签: php html mysql codeigniter pagination

我是codeigniter的新手,我正在尝试为我从数据库中提取的记录创建分页。我写了下面的代码,它显示了我的分页,但它没有影响结果集,我仍然在同一页面上有所有记录,当我点击Page2时它显示no page found

请指导我如何制作分页?

MODEL

public function students_list()
{


    $this->db->select('*');
    $this->db->from('student'); 

    $this->db->limit($limit,$start);                

    $query= $this->db->get();

    return $query->result();

    }

CONTROLLER

public function all_students()
{

    //Pagination    
            $this->load->library('pagination');

            $config['base_url'] = base_url().'mycontroller/all_students/';
            $config['total_rows'] = 200;
            $config['per_page'] = 5;

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

            echo $this->pagination->create_links(); 

    //Pagination

    $this->load->model('loginmodel');
    $data['students_data']= $this->loginmodel->students_list();
    $data['dropdown']=      $this->loginmodel->get_degree_names();

    $this->load->view('all_students', $data);

}

查看

<?php
include 'header.php';
include 'sidebar.php';


$this->pagination->create_links();
?>
<?php 

foreach($students_data as $teachers_records)
{
    ...
     ....

4 个答案:

答案 0 :(得分:2)

控制器:

public function all_students() 
 {      $this->load->model('loginmodel');
        $config['anchor_class'] = '';
        $config['show_count'] = true;
        $config['base_url'] = site_url('controllername/all_students');
        $config['total_rows'] = sizeof($this->loginmodel->students_list());

        $data['students_data']= $this->loginmodel->students_list();
        $data['dropdown']=      $this->loginmodel->get_degree_names();
        $config['per_page'] = 10;
        $this->jquery_pagination->initialize($config);
        $data['links'] = $this->jquery_pagination->create_links();
        $data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['details'] = $this->loginmodel->students_list($config['per_page'], $data['page']);
        $this->load->view('all_students', $data);
    }

模型

public function students_list($limit=NULL,$start=NULL)
{


    $this->db->select('*');
    $this->db->from('student'); 

    $this->db->limit($limit,$start);                

    $query= $this->db->get();

    return $query->result();

 }

答案 1 :(得分:1)

你需要一些数据来替换这个和$config['total_rows']的分页使用查询,所以它会显示5个记录的总结果分页

在我的情况下我使用$this->Account->search_users($where,false, false,false,true,false);是模型函数将返回结果数组

return $query->result_array();

控制器: -

      $this->load->library('pagination');   
    $config['total_rows'] = $this->Account->search_users($where,false, false,false,true,false);
    $config['per_page']= 5;
    $config['uri_segment'] = 3;
    $config['base_url']= base_url().'/app/index'; 
    $config['suffix'] = '?'.http_build_query($_GET, '', "&"); 
    $this->pagination->initialize($config);
    $this->data['paginglinks'] = $this->pagination->create_links();     
    $this->data['per_page'] = $this->uri->segment(3);       
    $this->data['offset'] = $offset ;

视图: -

<div class="pagination" style="float:right;"> <?php echo $paginglinks; ?></div>

有关详情: - http://ellislab.com/codeigniter/user-guide/libraries/pagination.html

我的模特功能是: -

public function search_users($where = false, $num = false, $offset = false, $order_by = false, $count = false, $select = false , $table=false){

    if(!$select){$select = 'U.*';}

        if(isset($where['where']) && !empty($where['where'])) {
            $this->db->where($where['where']);
        }

        if($order_by){
            $this->db->order_by($order_by[0], $order_by[1]);
        }else{
            $this->db->order_by('U.u_id', 'DESC');
        }

        $query = $this->db->get($table . ' AS U');
        //print_r($this->db->last_query()).'<hr>';
        $recount = $query->num_rows;    
        if($count) {
            return $recount;    
        }

        if($recount > 0){
            return $query->result_array();
        }
        else{ 
            return false;
        }
    }

答案 2 :(得分:1)

为了更好地使用分页,请在助手中创建一个函数。这对您将来的使用非常有用。你不需要加载库和其他值。 在你的助手中修复这个功能。

if( ! function_exists('createPaginationForm')) {
    function createPaginationForm($url, $totalRows, $perPage, $segment, $queryString = '', $config = array()) {
        $CI =& get_instance();
        $CI->load->library('pagination');
        $config['base_url'] = base_url().$url;
        $config['total_rows'] = $totalRows;
        $config['per_page'] = $perPage;
        $config['uri_segment'] = $segment;
        $CI->pagination->initialize($config);
        return $CI->pagination->create_links($queryString);
    }
}

并在您的控制器中使用。

var $perPage = '10';
var $segment = '4';
$total_result = "50";
$pagination = createPaginationForm('admin/news/index/',$total_result ,$this->perPage, $this->segment);
$this->data['pagination'] = $pagination;

并在您的视图中打印。

echo $pagination

答案 3 :(得分:1)

首先,我建议创建一个帮助函数来配置这样的分页。你必须从codeigniter文档中找到config

中的选项目的
if (!function_exists('getPaginationConfig')) {
    function getPaginationConfig($url,$total_row,$per_page,$num_links=3){
            $config = array();
            $config['use_page_numbers'] = TRUE;
            $config['uri_segment'] = 3;// depends on how you passing your page number
            $config['base_url'] = $url;
            $config['total_rows'] = $total_row;
            $config['per_page'] = $per_page; 
            $config['full_tag_open'] = '<ul>';
            $config['full_tag_close'] = '</ul>';
            $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>';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['first_link'] = '&lt;&lt;';
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['last_link'] = '&gt;&gt;';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';
            $config['num_links'] = $num_links;

     return $config;        
   }
}

现在在控制器上..你需要从数据库中获取总行并在分页中设置perpage 并使用配置调用 这是控制器功能的一个例子

 function getMyList($page){
        $limit = YOUR_PAGE_LIMIT;//may be some global variable or input from frontend
        $starting = ($page-1)*$limit;

        $data['results']  = $this->your_model->getListOfTable($where,$limit,$starting);
        $total_rows = $this->your_model->getTotalRowsOfTable($where);// where is filter condition

        $this -> load -> helper('your_helper');// where yours getPaginationConfig is defined
        $this -> load -> library('pagination');
        $this -> pagination -> initialize(getPaginationConfig(site_url('controller/function'), $total_rows,$limit));
        $data['pagination'] = $this -> pagination -> create_links();
        $this->load->view('yourview',$data);

您的型号可能如下所示

function getListOfTable($where = array(), $limit, $starting = 0) {
        $this -> db -> select('*');
        // <-- There is never any reason to write this line!
        $this -> db -> from('your_tables');
        if (!empty($where))
           $this -> db -> where($where);
        $this -> db -> limit($limit, $starting);
        $query = $this -> db -> get();

        if ($query -> num_rows() > 0) {
            return $query -> result();
        } else {
            return FALSE;
        }
    }

function getTotalRowsOfTable($where = array()){
    if (!empty($data))
         $this -> db -> where($where);
    $this -> db -> from('your_table');
    return $this -> db -> count_all_results();

}

现在在视图中你只需要回应你喜欢的分页

  <div id="pagination"><?php echo $pagination;?></div>