2012-04-04 149 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. 
      } 
     } 
    ?>