2012-04-04 98 views
-1

我想在視圖頁中顯示2行作爲輸出。再次當我點擊進入下一頁時,它將顯示2下一行等等(所有我一起8但是當我運行下面的代碼時,它顯示了查看頁面中的所有8行以及分頁鏈接。我試圖找到不是 的實際原因,但是我的問題仍然沒有解決。用相關的查詢在互聯網上尋找幫助,但它沒用。最後,我在這裏用我自己的話來解釋 這個問題。我已經評論了我的代碼中幾乎所有的行。我正在加載autoload中的庫以進行分頁和幫助的形式和網址。我真的很感激,如果 有人幫我。提前致謝。分頁不工作在codeigniter :(

<?php 
     class ManageUser extends CI_Controller { // creating class for the controller 
      function index() 
      { 
       if($this->session->userdata('logged_in')) // checking users under session if he is already logged in 
       {   
        $this->load->model('admin/user');  // loading model . I am not using Datamapper. 
        $result = $this->user->view_user(); // getting response from the model and storing it to result 
        $total_rows = count($result);   // counting number of rows countered (its 8 in in my database) 
        //echo $total_rows; 


        if($result) 
        { 
         $data['users'] = $result; 
         $config['base_url'] = "http://192.168.0.102/project/index.php/admin/manageUser"; // this is the address where I am pointing the view page url 
         $config['total_rows'] = $total_rows;  // Total numbers of rows assigned to pagination-config 
         $config['per_page'] = '2';     // I want to display 2 rows in 1 page 
         $config['uri_segment'] = '2'; 
         $this->pagination->initialize($config);  // initilizing the pagination-config 
         //$data['pagination'] = $this->pagination->create_links(); 
         //$this->load->vars($data); 
         $this->load->view('admin/manageUser',$data); // Loading the page 
        } 

        //$this->load->view('admin/manageUser',$data); 
       } 
       else 
       { 
        redirect('admin/login'); // incase of faliured session user will be redirected to the login-page. 
       } 
      } 
     } 
     ?> 

下面的代碼是從我的一個名爲User

<?php 
      Class User extends CI_Model // extending the model 
      { 
       function __construct() 
       { 
        parent::__construct(); 
       } 

       function view_user()  // function which is loaded from controller 
       { 

        $query = $this -> db -> get('users'); //query to fetch all the information from the database 
        return $query->result();  // returning result to the Controller. 
       } 
      } 
     ?> 

模型最後,這是我用來顯示內容的視圖頁面。

<!-- This is the view page --> 

     <?php if(isset($users)) { ?>  <!-- Checking if user is set --> 
      <?php foreach($users as $user) { ?> <!-- running in a loop to accept all the value from the database and display it row wise --> 
       <td><?php echo $user -> us_display_name; ?></td> <!-- Displaying name --> 
       <td><?php echo $user -> us_first_name . " " . $user -> us_last_name; ?></td> <!-- Displaying first name and last name together --> 
       <td><?php echo $user -> us_email_id; ?></td> <!-- Displaying email-ID --> 
     <?php } } else { ?> 
     <tr> <td>No records found!</td> 
     <?php } ?> 
     <?php echo $this->pagination->create_links(); ?> 
+0

您需要定義偏移量和限制您的查詢.... – 2012-04-05 07:32:00

+0

我做與此同時。但它不工作。請檢查下面的代碼。 <!php if(!defined('BASEPATH'))exit('No direct script access allowed'); 函數索引($ offset = 0) { \t \t $ this-> load-> model('user'); \t \t $ this-> load-> library('pagination'); $ user_list =新用戶(); $ total_rows = $ user_list-> count(); // $ student_list-> order_by('name'); $ data ['user_list'] = $ user_list-> get(5,$ offset) - > all; – 2012-04-05 08:27:15

+0

什麼錯誤,你得到 – 2012-04-05 08:49:49

回答

1

您正在從模型中獲取所有用戶。這不是你怎麼做的。 你的控制器應該是這樣的:

<?php 
    class ManageUser extends CI_Controller { // creating class for the controller 
     function index($offset) 
     { 
      if($this->session->userdata('logged_in')) // checking users under session if he is already logged in 
      {   
       $this->load->model('admin/user');  // loading model . I am not using Datamapper. 
       $perpage = 2; 

       $result = $this->user->view_user($offset,$perpage); // getting response from the model and storing it to result 
       $total_rows = $this->db->count_all('users'); 


       if($result) 
       { 
        $data['users'] = $result; 
        $config['base_url'] = "http://192.168.0.102/project/index.php/admin/manageUser"; // this is the address where I am pointing the view page url 
        $config['total_rows'] = $total_rows;  // Total numbers of rows assigned to pagination-config 
        $config['per_page'] = $perpage;     // I want to display 2 rows in 1 page 
        $config['uri_segment'] = '2'; 
        $this->pagination->initialize($config);  // initilizing the pagination-config 
        //$data['pagination'] = $this->pagination->create_links(); 
        //$this->load->vars($data); 
        $this->load->view('admin/manageUser',$data); // Loading the page 
       } 

       //$this->load->view('admin/manageUser',$data); 
      } 
      else 
      { 
       redirect('admin/login'); // incase of faliured session user will be redirected to the login-page. 
      } 
     } 
    } 
    ?> 

和模型中需要限制的結果

<?php 
     Class User extends CI_Model // extending the model 
     { 
      function __construct() 
      { 
       parent::__construct(); 
      } 

      function view_user($offset,$limit)  // function which is loaded from controller 
      { 

       $query = $this -> db -> get('users',$perpage,$offset); //You dont fetch all the data from the database 
       return $query->result();  // returning result to the Controller. 
      } 
     } 
    ?>