2014-04-16 53 views
1

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

请指导我如何创建分页?

模型

public function students_list() 
{ 


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

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

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

    return $query->result(); 

    } 

控制器

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); 

} 

VIEW

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


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

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

很多关于分页的堆栈已经在堆栈中 –

+0

我有c hecked他们大多数,但仍然不明白为什么它不在这里工作 – user3480644

+0

我只是举一个例子,并根据您的答案转换 –

回答

1
你需要一些数据来disp已

在分页使用的查询,这和$config['total_rows'] LAT所以它会显示总的结果分页有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

控制器:

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(); 

} 
+0

感谢lot..I已经取代了我的代码与您的代码,现在,它给我下面的错误消息我'Model' '一个PHP错误遇到 严重性:注意 消息:未定义的变量:开始 文件名:型号/ loginmodel.php 行号:582' – user3480644

+0

我已经在我的问题添加的型号代码...请检查 – user3480644

+0

还是有的.. '一个PHP错误遇到错误 严重性:警告 消息:缺少loginmodel :: students_list()的参数1,在F:\ xampp \ htdocs \ Uni中调用versity \程序\控制器\ mycontroller.php上线613和定义 文件名:型号/ loginmodel.php 行号:在模型函数 – user3480644

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 
1

首先,我建议建立一个辅助函数来配置你的分页这样。您必须从配码文件中找到配置中的选项目的

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;   
    } 
} 

Now On Controller ..你需要从数据库中获取的总行并设置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>