Codeigniter->使用Pagination与sql select

时间:2010-12-04 22:49:59

标签: codeigniter pagination

好的,我学会了如何用codeigniter进行分页,但我坚持的是你如何加入类似的东西:

$this->db->query("Select * FROM film_list WHERE category = 'documentary');

如何让分页只返回上述数据?我不想只返回表中的所有数据。

function start()
{
    $config['base_url'] = 'http://localhost/sakila/index.php/site/start/';
    $config['total_rows'] = $this->db->get('film_list')->num_rows();
    $config['per_page'] = '10';
    $config['num_links'] = '20';
    $config['full_tag_open'] = '<div id="pagination">';
    $config['full_tag_close'] = '</div>';


    $data['main_content'] = "start";
    $data['records'] = $this->db->get('film_list', $config['per_page'], 
                                      $this->uri->segment(3));
    $this->pagination->initialize($config);
    $this->load->view('includes/template', $data);
    echo $this->pagination->create_links();}

我已经做到了这一点并且它有效,从sql表中提取所有数据将其插入到HTML表中并正确分页,我只想将其限制在某个类别。

这是视图,如果它有帮助。

<div id="main">
  <h1>Documentaries</h1>
  <?php 
    echo $this->table->generate($records);
    echo $this->pagination->create_links();
  ?>
 </div>

2 个答案:

答案 0 :(得分:2)

希望此代码能为您提供帮助。

<?php
//CONTROLLER

$qry = "Select * FROM film_list WHERE category = 'documentary";

$limit = 10;
$offset = ($this->uri->segment(3) != '' ? $this->uri->segment(3):0);

$config['base_url'] = 'http://localhost/sakila/index.php/site/start/';
$config['total_rows'] = $this->db->query($qry)->num_rows();
$config['uri_segment'] = 3;
$config['per_page'] = $limit;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';

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

$qry .= " limit {$limit} offset {$offset} ";

$data['result_per_page'] = $this->db->query($qry)->result();

$this->load->view('includes/template', $data);

为您的观点..

<div id="main">
   <h1>Documentaries</h1>
   <?php 
       foreach($result_per_page as $row)
   {
    echo $row->column_to_show; //display result to your databse.column_to_show is just a dummy column name of the query result
   }

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

答案 1 :(得分:1)

您需要添加where子句。而不只是:

$data['records'] = $this->db->get('film_list', $config['per_page'], 
                                      $this->uri->segment(3));

尝试这样的事情:

$this->db->where('category','documentary');
$data['records'] = $this->db->get('film_list', $config['per_page'], 
                                      $this->uri->segment(3));