【问题标题】:How to Give Proper Pagination To Data Table using Pagination library in Codeigniter如何使用 Codeigniter 中的分页库对数据表进行适当的分页
【发布时间】:2015-10-14 17:07:06
【问题描述】:

我正在 CodeIgniter 中工作分页。我也看到教程和用户手册。但我找不到正确的方法,他们在控制器中调用数据库进行分页。我想要一个适当的解决方案。

这是控制器

    public function index() {

                $this->load->library('pagination');
                $config['base_url'] = 'index.php/Emp';
                $config['total_rows'] = 200;
                $config['per_page'] = 2; 
                $config['uri_segment'] = 3;

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


        $data = array(); 

        $data['showEmployeeTable']=$this->Em->selectEmployeeData(); //FOR SHOWING A EMPLOYEE DATABASE TABLE

        if($query = $this->Em->getDesignationData())  //grabs all record
        {
            $data['records'] = $query;
        }

        if($query = $this->Em->getCityRecord())  //grabs all record
        {
            $data['records_city'] = $query;
        }

        //$this->load->view('emp_update', $data);
        $this->load->view('erv', $data);

    }

这是我的模型

public function getDesignationData() {

    $query = $this->db->get('emp_desig'); //model created for get data 
    return $query->result();
}


public function getCityRecord() {

    $query = $this->db->get('city'); //model created for get data 
    return $query->result();
}

// ***************** VEDDING PLAN MODELS **********************************************************


public function selectEmployeeData() {  

     $query = $this->db->get('emp_manage');  
     return $query;  
} 

那么如何在视图页面上显示正确的分页?请逐步回答我。我是 Codeigniter 的新手。

这个在视图中。

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

【问题讨论】:

标签: php mysql codeigniter pagination database-table


【解决方案1】:

你的控制器应该是

public function index() {

    $this->load->library('pagination');
    $config = array();
    $config['base_url'] = 'index.php/Emp';
    $config['total_rows'] = 200;
    $config['per_page'] = 2;
    $config['uri_segment'] = 3;
    $this->pagination->initialize($config);


    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data['showEmployeeTable']=$this->Em->selectEmployeeData($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();


    if($query = $this->Em->getDesignationData())  //grabs all record
    {
        $data['records'] = $query;
    }

    if($query = $this->Em->getCityRecord())  //grabs all record
    {
        $data['records_city'] = $query;
    }

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

}

你的模型应该是

public function selectEmployeeData($limit, $start) {  

 $this->db->limit($limit, $start);
     $query = $this->db->get('emp_manage');  
     return $query;  
} 

并在您的意见中添加以下行

<?php echo $links; ?>

【讨论】:

    【解决方案2】:

    查看此代码并根据您的要求进行一些更改:

    你的控制者应该是:

        function list($offset=0)
       {
        $num_rows=$this->db->count_all("table_name");
        $config['base_url'] = site_url('list');
        $config['total_rows'] = $num_rows;
        $config['per_page'] = 100;
        $config['num_links'] = round($config['total_rows']/$config['per_page']);
        //$config['use_page_numbers'] = TRUE;
        $config['page_query_string']=TRUE;
        $this->pagination->initialize($config);
        $data["results"] = $this->Em->selectEmployeeData($config["per_page"], $page);
        $this->load->view('viewfile',$data);
       }
    

    你的模特:

    function selectEmployeeData($limit,$offset)//fetch all data with pagination
        {
            $data = array();
            $this->db->limit($limit, $offset);
            $Q = $this->db->get('table');
            if($Q->num_rows() > 0)
            {
                foreach ($Q->result_array() as $row)
                {
                    $data[] = $row;
                }
            }
            $Q->free_result();
            return $data;
        }
    

    在视图中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-08
      • 2018-05-25
      相关资源
      最近更新 更多