codeigniter中的分页 - 第二页

时间:2015-11-20 06:38:12

标签: php codeigniter

我尝试在codeigniter中进行分页。它在第一页中工作正常。但是当我点击第二页时,页面似乎是空白的。数据未加载。

我的控制器:

<?php
class Generate_report extends CI_Controller {

    public $newdata;
    public $layout = 'default';
    public $html = '';
    public $title = 'Title';

    function __construct() {
        parent::__construct();
        if ($this->router->method == 'index') {
            $this->config->set_item('enable_hooks', FALSE);
        }

        $this->load->helper('form');
        $this->load->helper(array('url', 'left_menu'));
         $this->load->library('pagination');
        $this->load->library(array('form_validation', 'session'));
        $this->load->model('getReportModel');
        $this->left_menu = display_menu($this->session->userdata('role'));
    }
       public function generate_employeereport($offSet = NULL){

        //$this->config->set_item('enable_hooks', FALSE);
            if ($this->session->userdata('role') == "Admin" ||$this->session->userdata('role') == "HR Manager") {

               if(isset($_GET['submit'])) { 

                $session_data = $this->session->all_userdata();
                $category = $this->input->get('category');
                $report_join_date = date("Y-m-d", strtotime($this->input->get('report_join_date')));
                $designation = $this->input->get('designation');
                $purpose = $this->input->get('purpose');
                $data['category']= $category;
                $data['report_join_date']= $this->input->get('report_join_date');
                $data['designation']= $designation;

                $where= array();
                if($category == "Less than"){
                if($report_join_date!="")
                {
                 $where+= array("primary_details.date_join <" => $report_join_date);   
                }
                }
                else if($category == "Less than or Equal to"){
                if($report_join_date!="")
                {
                 $where+= array("primary_details.date_join <=" => $report_join_date);   
                }
                }
                else if($category == "Greater than"){
                if($report_join_date!="")
                {
                 $where+= array("primary_details.date_join >" => $report_join_date);   
                }
                }
                else if($category == "Greater than or Equal to"){
                if($report_join_date!="")
                {
                 $where+= array("primary_details.date_join >=" => $report_join_date);   
                }
                }
                else {
                if($report_join_date!="")
                {
                 $where+= array("primary_details.date_join" => $report_join_date);   
                }
                }
                if($designation!="")
                {
                 $where+= array("ta_designations.designations" => $designation);   
                }
              $config["base_url"] = base_url() . "generate_report/generate_employeereport/";
              $config["total_rows"] = $this->getReportModel->getEmployees_count($where); 
              $config["per_page"] = 50;
              $config["uri_segment"] = 3; //part of the URL which contain the page section
              $config['use_page_numbers'] = FALSE;
              $config['reuse_query_string']=TRUE;
              $config['full_tag_open'] = '<ul>';
              $config['full_tag_close'] = '</ul>';
              $config['num_tag_open'] = '<li>';
              $config['num_tag_close'] = '</li>';
              $config['cur_tag_open'] = '<li><a class="current" href="#">';
              $config['cur_tag_close'] = '</a></li>';
              $config['prev_tag_open'] = '<li>';
              $config['prev_tag_close'] = '</li>';
              $config['prev_link'] = '←';
              $config['next_link'] = '→';
              $config['next_tag_open'] = '<li>';
              $config['next_tag_close'] = '</li>';
              $config['first_link'] = 'First';
              $config['first_tag_open'] = '<li>';
              $config['first_tag_close'] = '</li>';
              $config['last_link'] = 'Last';
              $config['last_tag_open'] = '<li>';
              $config['last_tag_close'] = '</li>';
              $data['offSet'] = $offSet;
              $this->pagination->initialize($config);
              $data['employees'] = $this->getReportModel->getEmployees($where, $config["per_page"], $offSet);
              $this->html= $this->load->view('report_generation/employee_report',$data,TRUE);  
            }  
        }
}

型号:

<?php

class GetReportModel extends CI_Model {

    function __construct() {
        parent::__construct();

        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->database();
    }
        public function getEmployees($where,$limit = 0, $offSet = 0) {

            $result ="";
          //$category= $data['category'];
          //$report_join_date= $where['report_join_date'];
          //$designation= $where['designation'];
          //$purpose= $data['purpose'];
          $this->db->select('primary_details.name,primary_details.date_birth,primary_details.date_join,ta_designations.designations');
          $this->db->from('primary_details');
          $this->db->join('ta_designations', 'primary_details.designation =  ta_designations.id');
          $this->db->join('joining_details', 'primary_details.p_id =  joining_details.empl_id');
          $this->db->where('joining_details.active', 1);
          if($where!=""){     

          $this->db->where($where);
           }
          $this->db->limit($limit, $offSet);

          $query = $this->db->get();
          //echo $this->db->last_query();
          $result = $query->result_array();
          return $result;
      }

查看:

<div class="inner">
    <div class="inner_content">
        <?php
        $attributes = array(
            'name' => 'remove_employee',
            'onsubmit' => "return checking()",
            'id' => 'employee_report',
             'method' => 'GET');
        echo form_open_multipart('generate_report/generate_employeereport', $attributes);
        ?>

        <ul id="apply_leave_ul">
            <li>
                <fieldset>
                    <?php

                    $selreq_type="";
                   // echo $category;
                    if(isset($category)) {
                         $selreq_type=$category;


                         }  
                        ?> 
                    <label>Date of Joining</label>
                    <div class="area_input">
                        <select name="category" id="category" tabindex="1" class="select_box">
                            <option value="" >Select</option>
                            <option value="Less than" <?php  if( $selreq_type=="Less than"){ echo "selected='selected'";}?>>Less than</option>
                            <option value="Less than or Equal to" <?php  if( $selreq_type=="Less than or Equal to"){ echo "selected='selected'";}?>>Less than or Equal to</option>
                            <option value="Equal to" <?php  if( $selreq_type=="Equal to"){ echo "selected='selected'";}?>>Equal to</option>
                            <option value="Greater than" <?php  if( $selreq_type=="Greater than"){ echo "selected='selected'";}?>>Greater than</option>
                            <option value="Greater than or Equal to" <?php  if( $selreq_type=="Greater than or Equal to"){ echo "selected='selected'";}?>>Greater than or Equal to</option>
                        </select>
                    </div>
                </fieldset>
                <fieldset>

                    <div class="area_input">
                        <input type="text" name="report_join_date" id="report_join_date" value="<?php if(isset($report_join_date)) {echo $report_join_date;} else { echo date('d-M-Y'); }?>" name="report_join_date"  class="width180 date select_duratn">
                    </div>
                </fieldset>


                <fieldset>
                    <?php

                    $selreq_type="";
                    if(isset($designation)) {
                         $selreq_type=$designation;

                         }  
                        ?> 
                    <label>Designation</label>
                    <div class="area_input">
                        <select name="designation" id="designation" tabindex="1" class="select_box">
                            <option value="">Select</option>
                            <option value="CEO" <?php  if( $selreq_type=="CEO"){ echo "selected='selected'";}?>>CEO</option>
                            <option value="CTO" <?php  if( $selreq_type=="CTO"){ echo "selected='selected'";}?>>CTO</option>
                            <option value="Delivery Head" <?php  if( $selreq_type=="Delivery Head"){ echo "selected='selected'";}?>>Delivery Head</option>
                            <option value="Associate Project Manager" <?php  if( $selreq_type=="Associate Project Manager"){ echo "selected='selected'";}?>>Associate Project Manager</option>
                            <option value="Senior Graphics Designer" <?php  if( $selreq_type=="Senior Graphics Designer"){ echo "selected='selected'";}?>>Senior Graphics Designer</option>
                            <option value="Team Lead" <?php  if( $selreq_type=="Team Lead"){ echo "selected='selected'";}?>>Team Lead</option>
                            <option value="Senior Software Engineer" <?php  if( $selreq_type=="Senior Software Engineer"){ echo "selected='selected'";}?>>Senior Software Engineer</option>
                            <option value="Software Engineer" <?php  if( $selreq_type=="Software Engineer"){ echo "selected='selected'";}?>>Software Engineer</option>
                            <option value="Executive HR" <?php  if( $selreq_type=="Executive HR"){ echo "selected='selected'";}?>>Executive HR</option>
                            <option value="Senior Executive- Business Development" <?php  if( $selreq_type=="Senior Executive- Business Development"){ echo "selected='selected'";}?>>Senior Executive- Business Development</option>
                            <option value="Senior Engineer- QA" <?php  if( $selreq_type=="Senior Engineer- QA"){ echo "selected='selected'";}?>>Senior Engineer- QA</option>
                            <option value="Graphics Design Associate" <?php  if( $selreq_type=="Graphics Design Associate"){ echo "selected='selected'";}?>>Graphics Design Associate</option>
                            <option value="Associate Finance" <?php  if( $selreq_type=="Associate Finance"){ echo "selected='selected'";}?>>Associate Finance</option>
                            <option value="Engineer Testing" <?php  if( $selreq_type=="Engineer Testing"){ echo "selected='selected'";}?>>Engineer Testing</option>
                            <option value="Software Engineer (Trainee)" <?php  if( $selreq_type=="Software Engineer (Trainee)"){ echo "selected='selected'";}?>>Software Engineer (Trainee)</option>  
                            <option value="Engineer Testing (Trainee)" <?php  if( $selreq_type=="Engineer Testing (Trainee)"){ echo "selected='selected'";}?>>Engineer Testing(Trainee)</option>
                            <option value="Senior Software Engineer" <?php  if( $selreq_type=="Senior Software Engineer"){ echo "selected='selected'";}?>>Senior Software Engineer</option>
                            <option value="System Administrator" <?php  if( $selreq_type=="System Administrator"){ echo "selected='selected'";}?>>System Administrator</option>
                            <option value="Senior Executive HR" <?php  if( $selreq_type=="Senior Executive HR"){ echo "selected='selected'";}?>>Senior Executive HR</option>
                            <option value="Content Development Associate" <?php  if( $selreq_type=="Content Development Associate"){ echo "selected='selected'";}?>>Content Development Associate</option>
                            <option value="Business Analyst" <?php  if( $selreq_type=="Business Analyst"){ echo "selected='selected'";}?>>Business Analyst</option>
                            <option value="Engineer QA (Trainee)" <?php  if( $selreq_type=="Engineer QA (Trainee)"){ echo "selected='selected'";}?>>Engineer QA(Trainee)</option>

                        </select>
                    </div>
                </fieldset>
                 <fieldset>
                     <?php

                    $selreq_type="";
                    if(isset($purpose)) {
                         $selreq_type=$purpose;

                         }  
                        ?> 
                    <label>Purpose</label>
                    <div class="area_input">
                        <select name="purpose" id="purpose" tabindex="1" class="select_box">

                            <option value="Insurance" <?php  if( $selreq_type=="Insurance"){ echo "selected='selected'";}   ?>>Insurance</option>

                        </select>
                    </div>
                </fieldset>
            </li>
        </ul>
        <?php ?>


        <div class="footer_btn_area">
            <?php
            $data = array(
                'style' => 'float: left;margin-right:4px;',
                'class' => 'btn right',
                'value' => 'Generate',
                'name' => 'submit',
            );
            echo form_submit($data);
            ?>
            <?php echo form_close(); ?>

        </div>


   <?php 
        $i = 0;
        $j = 1;
        //print_r($employees);
        if (isset($employees) && !empty($employees)) {

        ?>  

        <div class="inner" style="padding:0px; border-bottom:none;">
                <div class="inner_content">
                    <table width="100%" border="0" id="tble_employee">
                    <tr>
                            <th scope="col">No</th>
                            <th scope="col">Name</th>
                            <th scope="col">Designation</th>
                            <th scope="col">Date of Birth</th>
                            <th scope="col">Age</th>
                            <th scope="col">Date of Joining</th>
                            <th scope="col" colspan="2">Years of Service</th>

                        <?php
                        foreach ($employees as $row) {

                             ?>
                            <tr>
                                <td><?php echo $j; ?></td>
                                <td><?php echo $row['name']; ?></td>
                                <td><?php echo $row['designations']; ?></td>
                                <td><?php echo $row['date_birth']; ?></td>
                                <?php
                                $date= new DateTime('00:00:00');
                                $bday=new DateTime($row['date_birth']);
                                $age = $date->diff($bday);
                                ?>
                                <td><?php echo $age->y; ?></td>
                                <td><?php echo $row['date_join']; ?></td>
                                <?php
                                $date1= new DateTime('00:00:00');
                                $join=new DateTime($row['date_join']);
                                $service = $date1->diff($join);
                                ?>
                                <td><?php echo $service->y; ?>
                                    <?php echo "years";?>
                                <?php echo $service->m; ?>
                                     <?php echo "months";?></td>
                               </tr>
                            <?php
                            $i++;
                            $j++;
                        }
                        ?>
                    </table>
                </div>
            </div><?php } 

?>
    <div>
    <nav class="pagination" style="text-align: right;width: 97%;"><?php echo $this->pagination->create_links(); ?></nav>
    </div>
</div>
</div>

Plz建议解决方案

2 个答案:

答案 0 :(得分:0)

试试这个

在控制器中

$config["base_url"] = base_url() . "generate_report/generate_employeereport/";
$config["total_rows"] = $this->getReportModel->getEmployees_count($where); 
$config["per_page"] = 50;
$config["uri_segment"] = 2; # Changed
$limit = $config['per_page']; # added

<!-- Rest of your code -->

//$data['offSet'] = $offSet; remove this
$this->pagination->initialize($config);

$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0; # added


$data['employees'] = $this->getReportModel->getEmployees($where, $page, $limit);
$this->html= $this->load->view('report_generation/employee_report',$data,TRUE);  

在模型中

public function getEmployees($where, $page, $limit) {

    $result ="";
    $this->db->select('primary_details.name,primary_details.date_birth,primary_details.date_join,ta_designations.designations');
    $this->db->from('primary_details');
    $this->db->join('ta_designations', 'primary_details.designation =  ta_designations.id');
    $this->db->join('joining_details', 'primary_details.p_id =  joining_details.empl_id');
    $this->db->where('joining_details.active', 1);
    if($where!=""){     

        $this->db->where($where);
    }
    $this->db->limit($page, $limit);
    $query = $this->db->get();
    $result = $query->result_array();
    return $result;
}

答案 1 :(得分:0)

控制器

上启用$config['page_query_string']
$this->load->library('pagination'); 
$config["base_url"] = base_url() . "generate_report/generate_employeereport/";
$config["total_rows"] = $this->getReportModel->getEmployees_count($where); 
$config["per_page"] = 50;
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
$data['employees'] = $this->getReportModel->getEmployees($where, $config["per_page"]); # Here passed only two parameters

在您的模型上使用此更改您的查询 -

public function getEmployees($where,$limit) {

        $offSet=$this->input->get('per_page');
        if(!$offSet){

            $offSet=0;
        }
      $result ="";

      $this->db->select('primary_details.name,primary_details.date_birth,primary_details.date_join,ta_designations.designations');
      $this->db->from('primary_details');
      $this->db->join('ta_designations', 'primary_details.designation =  ta_designations.id');
      $this->db->join('joining_details', 'primary_details.p_id =  joining_details.empl_id');
      $this->db->where('joining_details.active', 1);
      if($where!=""){     

      $this->db->where($where);
       }
      $this->db->limit($limit, $offSet);

      $query = $this->db->get();
      //echo $this->db->last_query();
      $result = $query->result_array();
      return $result;
    }