2013-11-14 2 views
1

привет, мое разбиение на страницы не работает.CodeIgniter Pagination не отображает правильные строки на странице

Это мой класс контроллера. customer_controller.php

 public function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper('url'); 
     $this->load->model('customer_model'); 
     $this->load->library('pagination'); 


    } 
    public function index() 
    { 


     $data['title']= 'Customer'; 
     $data['records'] = $this->customer_model->getAll(); 
     $data['groups'] = $this->customer_model->getAllCustomerGroups(); 
     $data['groupcodes'] = $this->customer_model->getAllCustomerGroups(); 

     $config['base_url'] = 'customers'; 
     $config['total_rows'] = $this->customer_model->record_count(); 
     $config['per_page'] = 1; 

     $config["uri_segment"] = 3; 

     $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; 
     $data["customers"] = $this->customer_model->fetch_customers($config["per_page"], $page); 
     $data["links"] = $this->pagination->create_links(); 


     //$this->pagination->initialize($config); 
     //echo $this->pagination->create_links(); 

     $this->load->view('include/header',$data); 
     $this->load->view('include/navbar',$data); 
     $this->load->view('customer_view', $data); 
     $this->load->view('include/sidebar',$data); 
     $this->load->view('include/footer',$data); 

    } 

Customer_model.php

public function fetch_customers($limit, $start) { 
      $this->db->limit($limit, $start); 
      $query = $this->db->query('SELECT customercode, customername, customergroup, customertype, customeraddress, website FROM customercard'); 

      if ($query->num_rows() > 0) { 
       foreach ($query->result() as $row) { 
        $data[] = $row; 
       } 
       return $data; 
      } 
      return false; 
     } 

customer_view.php

 <?php 

      foreach ($customers as $key => $value) 
      { 
        echo '<p>'. $value->customercode . '</p>'; 
        echo '<p>'. $value->customername . '</p>'; 
      } 

      ?> 

Моя проблема в том, что это просто отображение всех записей, а не только показывать 1 запись за раз. а также мои ссылки на страницы не отображаются. Что я здесь делаю неправильно? Помощь очень ценится. Благодарю.

ответ

0

Попробуйте заменить

$this->db->limit($limit, $start); 
$query = $this->db->query('SELECT customercode, customername, customergroup, customertype, customeraddress, website 
FROM customercard'); 

в

$this->db->select('customercode, customername, customergroup, customertype, customeraddress, website')->from('customercard')->limit($limit, $start)->get(); 
1

ваши ссылки разбивки на страницы будут отображаться с

<?php 

     foreach ($customers as $key => $value) 
     { 
       echo '<p>'. $value->customercode . '</p>'; 
       echo '<p>'. $value->customername . '</p>'; 
     } 
     echo $links; 

     ?> 

также раскомментируйте

//$this->pagination->initialize($config); 

также изменить свой запрос только

// $this->db->limit($limit, $start);// <-- remove this 

     $query = $this->db->query("SELECT customercode, customername, customergroup, customertype, customeraddress, website FROM customercard limit $start, $limit"); 

надеюсь, что это помогает

+0

+1 на раскомментирован заявление пагинация инициализации. Это было также первое, что я видел неправильно, поэтому не хотел создавать для него новый ответ. –

0

Попробуйте один

Модель:

public function fetch_customers($limit, $offset) { 
    $this->db->select('customercode, customername, customergroup, customertype, customeraddress, website'); 
    $query=$this->db->get('t_candidate', $limit, $offset); 

    if ($query->num_rows() > 0) { 
     return $query->result_array(); 
    }else{ 
     return false; 
    } 
} 

Вид: В видовом пасте следующую ссылку, куда вы хотите показать свои ссылки

<?php 
foreach ($customers as $key => $value) 
     { 
       echo '<p>'. $value['customercode'] . '</p>'; 
       echo '<p>'. $value['customername'] . '</p>'; 
     } 
    ?> 

<?php echo $this->pagination->create_links(); ?> 

Контроллер: раскомментируйте следующую строку

$this->pagination->initialize($config); 

Также я предпочел бы предположить, что вы пытаетесь использовать CodeIgniter активные записи, как он защищает БД от SQL инъекций

Смежные вопросы