简体   繁体   中英

how to works start and limit per page records in codeigniter php

I tried many ways but not working pagination. I have used segment but not working may be in this pagination between data working without pagination perfect.

Controller

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

  }
}

Model

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

VIEW

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

Let me know where is actually issue, I am using Codeigniter version 3.1.10

I figure out the issue, solution is in the 'base_url()' and with uri_segment .

Controller function :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM