2010-12-04 48 views
1

好,我學會了如何做分頁與笨,但什麼我卡上的是你怎麼把這樣的:Codeigniter->使用分頁與SQL SELECT

​​3210

如何獲得分頁到只返回上述數據?我不想只返回表中的所有數據。

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> 

回答

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)); 
+0

非常感謝!!!,工作就像一個魅力,我什麼都試過,但,我知道這是一些簡單..... – 2010-12-05 18:03:12

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

抱歉對準 – Orvyl 2012-11-27 23:02:11