2013-06-03 4 views
0

Я пытаюсь использовать разбиение на страницы с помощью CI, проблема в том, что, например: я нажал номер 2 разбивки на страницы и показал мне все 7000 строк на одной странице.Pagination with CI

Мой контроллер:

public function inicio() { //reporte 
    $campus=$this -> session -> userdata('campus'); 

    $this->load->library('pagination'); 

    $config['base_url'] = 'http://zeus.xochicalco.edu.mx/saii/sorteos/login/inicio'; 
    $config['total_rows'] = 7000; 
    $config['per_page'] = 20; 

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


    $data['boletos'] = $this->model_sorteos->reporte($campus);   
    $this -> load -> view('/pages/sorteos/template', $data); 
} 

И это моя модель:

public function reporte($campus,$offset=0){ 
    $x1=0; $x2=0; 
    $id=1; 
    $s= new Sorteo(); 
    $s -> where('nombre','Sorteo 9') -> get(); 
    $s->boleto->where('folio >='.$x1.' AND folio <= '.$x2.' ')->get(); 
    $i=0; 
    $boletos=array(); 
    foreach($s->boleto as $bol){ 

     $b = new Boleto(); 
     $b->where('id',$bol->id)->get(); 
     $b->vendedor->get(); 
     $b->condicion->get(); 
     $b->estado->get(); 
     $b->comprador->get(); 

     $boletos[$i]=array(
         'boleto' => $b->folio, 
         'estado' => $b->estado->nombre, 
         'condicion' => $b->condicion->nombre, 
         'vendedor' => $b->vendedor->nombre." ".$b->vendedor->apellido_paterno." ".$b->vendedor->apellido_materno, 
         'comprador' => $b->comprador->nombre." ".$b->comprador->apellido_paterno." ".$b->comprador->apellido_materno  
     );    
     $i++;   
    } return $boletos; } 

Вид:

<table class="table table-striped table-bordered" style="" > 
      <tr> 
       <td>Boleto</td> 
       <td>Estado</td> 
       <td>Condicion</td> 
       <td>Vendedor</td> 
       <td>Comprador</td> 
      </tr>  
<?php foreach($boletos as $b ){ ?>  
     <tr> 
      <td><?php echo $b['boleto']; ?></td> 
      <td><?php echo $b['estado']; ?></td> 
      <td><?php echo $b['condicion']; ?></td> 
      <td><?php echo $b['vendedor']; ?></td> 
      <td><?php echo $b['comprador']; ?></td> 
     </tr> 
<?php } echo $this->pagination->create_links(); ?> </table> </div> 

Я новичок в этом, какая-то помощь, пожалуйста? Заранее благодарен

+0

Что в вашем файле просмотра? Вы можете попытаться сказать codeigniter, какой сегмент uri имеет смещение. Пример: $ config ['uri_segment'] = 3; – jah

+0

Я добавил представление, у меня есть небольшая проблема, я не понимаю, что такое смещение и как его использовать или добавить в код. – Laura

+0

Как и в вашем примере, вы хотите показать 20 страниц. Итак, первые 20 строк показаны на первой странице, но на странице 2 вы хотите от 20 до 40. Таким образом, смещение будет 20 на странице 2. На странице 3 будет 40 и так далее. Но в тот момент, когда я вижу, вы даже не переносите смещение модели, и похоже, что у вас даже нет предела. – jah

ответ

0

Сетка в ci создает объект для вызова функции с аргументами (нет в соответствии с per_page.) Вы должны сделать функцию (чей url задан в базе) с аргументом, который отправляет этот номер к модели и выборка строк из базы данных. контроллер

/** 
* @author Aniket Singh<[email protected]> 
* @package Page 
* This is the main class of this file 
*/ 
class Page extends CI_Controller{ 
    /** 
    * @package Page 
    * This is the constructor function of the Page class 
    */ 
    function __construct(){ 
     parent::__construct(); 

    } 
    /** 
    * This function will implement the concept of pagination 
    * @package Page 
    * @param $entry decide the the starting row no. in a table while fetching data 
    */ 
    function test($entry=0){ 
     $data=array(); 
     $this->load->library('pagination'); 
     $config['base_url'] ="http://localhost/ci/page/test"; 
     $config['total_rows'] = 4000; 
     $config['per_page'] = 20; 
     $this->pagination->initialize($config); 



     $this->load->model('dbpage'); 
     //Fetch 20 rows at a time staring with id=$entry and save them into $data['entries'] 
     $data['entries']=$this->dbpage->get_entries($entry); 
     $this->load->view('mypage',$data); 

    } 
} 

Модель

class Dbpage extends CI_Model { 

    function __construct(){ 
     parent::__construct(); 
    } 

    function get_entries($entry=0) 
    { 
     $query = $this->db->get('City', 20, $entry); 
     return $query->result(); 
    } 

} 

вид

<html> 
<head> 
<title>Pagination</title> 
</head> 
<body> 
    <? php echo $this->pagination->create_links();?> 
    <table> 
     <tr> 
      <th> ID</th> 
      <th>Name</th> 
      <th>Country code</th> 
      <th>District</th> 
      <th>Population</th> 
     </tr> 
     <?php foreach ($entries as $key=>$value){?> 
      <tr> 
       <td><?php echo $value->ID?></td> 
       <td><?php echo $value->Name?></td> 
       <td><?php echo $value->CountryCode ?></td> 
       <td><?php echo $value->District ?></td> 
       <td><?php echo $value->Population ?></td> 
      </tr> 
     <?php }?> 
    </table> 
</body> 
</html> 

Это поможет вам понять концепцию пагинацией.