如何在codeigniter php中启动和限制每页记录

时间:2019-06-13 19:54:08

标签: php codeigniter pagination codeigniter-3

我尝试了许多方法,但没有进行分页。我已经使用了细分,但是在没有分页功能的情况下,数据之间的分页可能无法正常工作。

控制器

class Modelcategory extends CI_Controller {

  public function __construct() {
          parent:: __construct();
          $this->load->helper("url");
          $this->load->model("Db_model");
          $this->load->library("pagination");
      }
  function index(){

      $msg=$this->input->get('msg');
      $action=$this->input->get('action');
      $resultArr='';
      if(empty($action)){
        $config = array();
        $config['base_url'] = base_url('index.php/modelcategory');
        $config['total_rows'] = $this->Db_model->get_count('model_category');
        $config["per_page"] = 2;
        $config["uri_segment"] = 1;
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(1)) ? $this->uri->segment(1) : 0;
        $data["links"] = $this->pagination->create_links();
        $data['resultArr']=$this->Db_model->GetAllData('model_category',$config["per_page"], $page);
        print_r($data['resultArr']);
      }
      if($msg == 'success'){
        $msg ="Record inserted Successfully!";
      }

      $data['msg']=$msg;
      $data['action']=$action;
      $this->load->view('includes/header');
      $this->load->view('modelcat',$data);
      $this->load->view('includes/footer');

  }
}

型号

function GetAllData($Table,$limit = NULL, $start = NULL){   //Insert Data into Database

         $this->db->select("*");
       $this->db->from($Table);
         $this->db->limit($limit, $start);
       $query = $this->db->get();
         if ($query->num_rows() > 0 ){
           return $query->result_array();
         }else{
             return false;
         }

    }
    public function get_count($Table) {
        return $this->db->count_all($Table);
    }

查看

  if(!empty($resultArr)){
                $count=1;
                foreach ($resultArr as $key => $value) {
                    echo $count++." ".$value['name']."<br>";
                }
                echo $this->pagination->create_links();
            }

让我知道实际出在哪里,我正在使用Codeigniter 3.1.10版

1 个答案:

答案 0 :(得分:0)

我发现了问题所在,解决方案在'base_url()'中,并带有uri_segment

控制器功能:

function index(){
  $msg=$this->input->get('msg');
  $action=$this->input->get('action');
  $resultArr='';
  if(empty($action)){
    $config = array();
    $config['base_url'] = base_url('index.php/modelcategory/index');
    $config['total_rows'] = $this->Db_model->get_count('model_category');
    $config["per_page"] = 2;
    $config["uri_segment"] = 3;
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["links"] = $this->pagination->create_links();
    $data['resultArr']=$this->Db_model->GetAllData('model_category',$config["per_page"], $page);
    print_r($data['resultArr']);
  }
  if($msg == 'success'){
    $msg ="Record inserted Successfully!";
  }

  $data['msg']=$msg;
  $data['action']=$action;
  $this->load->view('includes/header');
  $this->load->view('modelcat',$data);
  $this->load->view('includes/footer');
}