2012-04-04 3 views
-1

Я хочу отобразить 2 строки в виде страницы в качестве вывода. Снова, когда я нажимаю, чтобы перейти к следующей странице, он отобразит 2 следующих ряда и так далее (все вместе у меня есть 8 строки в таблице ), но когда я запускаю следующий код, он отображает все 8 строк на странице просмотра вместе с ссылками на страницы. Я пробовал целый день, чтобы найти фактическую причину отсутствия , но моя проблема все еще не решена. искал помощь в Интернете с соответствующим запросом, но это было бесполезно. Finalyy Я здесь с моими собственными словами, чтобы объяснить проблему . Я прокомментировал почти все строки в моем коде. Я загружаю библиотеки в автозагрузку для разбивки на страницы и помощник для формы и URL. Я действительно буду благодарен, если кто-нибудь мне поможет. Спасибо заранее.pagination не работает в 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. 
       } 
      } 
     } 
     ?> 

следующий код из моей модели по имени пользователя

<?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

вам нужно определить смещение и ограничения в запросе .... –

+0

я сделал с этим также. Но это не работает. Пожалуйста, проверьте коды ниже. load-> model ('user'); \t \t $ this-> load-> library ('pagination'); $ user_list = new user(); $ total_rows = $ user_list-> count(); // $ student_list-> order_by ('name'); $ data ['user_list'] = $ user_list-> get (5, $ offset) -> all; –

+0

какая ошибка вы получаете –

ответ

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. 
      } 
     } 
    ?>