2015-09-20 37 views
0

我在查看頁面中使用分頁數據來顯示選擇頁面的鏈接,但它顯示數據庫中的所有數據(250個數據)它不是每頁30個單獨的數據。請幫我我的看法不是通過分頁使用獨立數據

這是我的模型

function get_item_code($param){ 

    $result = array(); 
    $sql = "select ic.id, ic.item_code, ic.description, maxdt.maxdt, lc.balance,lc.dt "; 
    $sql .= "from tbl_item_code ic "; 
    $sql .= "left join (tbl_lines_code lc inner join (select id, max(dt) maxdt from tbl_lines_code where active = 1 group by id) maxdt "; 
    $sql .= "on lc.id = maxdt.id and lc.dt = maxdt.maxdt) on ic.id = lc.id "; 
    $sql .= "where ic.active = 1 group by ic.id "; 
    // echo $sql; 
    if ($param->limit > 0) 
     $this->db->limit($param->per_page, $param->limit); 
    else 
     $this->db->limit($param->per_page); 
    $query = $this->db->query($sql); 

    if ($query->num_rows() > 0) { 
     $result = $query->result(); 
    } 
    return $result;  
} 

這是我的控制器

public function main(){ 
    $this->load->library('pagination'); 
    $page = $this->uri->segment(3); 

    $param = new stdClass(); 
    if (!isset($page) || $page == '') { 
     $page = 1; 
    } 
    $param->per_page = 30; 
    $param->limit = ($page - 1) * $param->per_page; 

    $paginate_url = site_url('warehouse/main'); 
    $data['total_result'] = $this->m_stock->count_stock(); 
    $config['uri_segment'] = 3; 
    $config['num_links'] = 4; 
    $config['base_url'] = $paginate_url; 
    $config['total_rows'] = $data['total_result']; 
    $config['per_page'] = $param->per_page; 
    $config['use_page_numbers'] = TRUE; 
    $config['page_query_string'] = FALSE; 
    $this->pagination->initialize($config); 
    $data['pagination'] = $this->pagination->create_links(); 

    $data['item'] = $this->m_stock->get_item_code($param); 
    $this->load->view('v_all_stocks', $data); 
} 

,這我認爲敵人回聲$pagination

<?php 
    if(isset($pagination) && $pagination != ''){ 
?> 
<div class="search_pagination"><?php echo $pagination; ?></div> 
<?php } ?> 

,並使用foreach的數據顯示

+0

使用限用這樣的偏移。 「SELECT * FROM xyz LIMIT 15,10」; –

+0

你得到了什麼? 'get_item_code'? –

回答

0

在控制器

function main() 
{ 
    $this->load->library('pagination'); 

    $count = $this->m_stock->count_stock(); 
    $config['base_url'] = base_url() . 'warehouse/main/'; 
    $config['total_rows'] = $count; 
    $config['per_page'] = 30; 
    $config['uri_segment'] = 3; 
    $limit = $config['per_page']; 

    // Bootstrap style 
    $config['full_tag_open'] = '<ul class="pagination">'; 
    $config['full_tag_close'] = '</ul>'; 
    $config['first_link'] = false; 
    $config['last_link'] = false; 
    $config['first_tag_open'] = '<li>'; 
    $config['first_tag_close'] = '</li>'; 
    $config['prev_link'] = '&laquo'; 
    $config['prev_tag_open'] = '<li class="prev">'; 
    $config['prev_tag_close'] = '</li>'; 
    $config['next_link'] = '&raquo'; 
    $config['next_tag_open'] = '<li>'; 
    $config['next_tag_close'] = '</li>'; 
    $config['last_tag_open'] = '<li>'; 
    $config['last_tag_close'] = '</li>'; 
    $config['cur_tag_open'] = '<li class="active"><a href="#">'; 
    $config['cur_tag_close'] = '</a></li>'; 
    $config['num_tag_open'] = '<li>'; 
    $config['num_tag_close'] = '</li>'; 


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

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; 
    $data['links'] = $this->pagination->create_links(); 

    $data['item'] = $this->m_stock->get_item_code($limit,$page);//pass item code here 

    $this->load->view('v_all_stocks', $data); 
} 

在模型

function get_item_code($limit,$page) 
{ 

    $sql = "select ic.id, ic.item_code, ic.description, maxdt.maxdt, lc.balance,lc.dt "; 
    $sql .= "from tbl_item_code ic "; 
    $sql .= "left join (tbl_lines_code lc inner join (select id, max(dt) maxdt from tbl_lines_code where active = 1 group by id) maxdt "; 
    $sql .= "on lc.id = maxdt.id and lc.dt = maxdt.maxdt) on ic.id = lc.id "; 
    $sql .= "where ic.active = 1 group by ic.id "; 
    $sql .= "LIMIT $page, $limit"; 

    $query = $this->db->query($sql); 
    $result = $query->result_array(); 

    return $result; 
}