2013-05-04 68 views
0

在CI的分页,数据指标应遵循的偏移。例如:如果限制为10,则第二个指标应该有10抵消,这意味着该指数将开始从11至20CodeIgniter的分页偏移不工作

我跟着一些教程,但我仍然无法得到这个偏移事情正常工作。 我每次点击不同的分页索引时,我的索引总是被重设为1

这是我的分页代码,音符,我试图呼应$offset,值为(在第二索引= 10,在第三索引= 20,用$limit = 10)所以我不知道为什么它不工作

public function index($offset = 0) { 
     //check authorization 
     if(!isset($_SESSION['username'])) 
      redirect('backend_umat/login'); 

      // the $offset value is true, but the index is still reseted to 1 
     echo $offset; 
     $limit = 10; 
     $result = $this->umat_m->get_umat($limit, $offset); 

     //pagination 
     $config['base_url'] = site_url('/backend_umat/index'); 
     $config['total_rows'] = $result['num_rows']; 
     $config['per_page'] = $limit; 
     $config['uri_segment'] = 3; 
     $config['full_tag_open'] = '<div id="pagination">'; 
     $config['full_tag_close'] = '</div>'; 

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

     $data['pagination'] = $this->pagination->create_links(); 

这是我的模型:

public function get_umat($limit, $offset) { 
     $this->db->select('*')->from('msumat')->limit($limit, $offset)-> 
     join('mskelas', 'msumat.kelas_id = mskelas.kelas_id'); 

$q = $this->db->get(); 
      $result['rows'] = $q->result(); 

      $result['num_rows'] = $this->db->select('*')->from('msumat')-> 
            join('mskelas', 'msumat.kelas_id = mskelas.kelas_id') 
            ->get()->num_rows(); 

return $result; 

感谢˚F或你的帮助:D

+0

我不能在代码中查看错误,如果偏移量正确,那么您的查询又如何? 'echo $ this-> db-> last_query()'在你的模型中的第一个查询之后。什么是错误的,从数据库或分页html数据? – Aurel 2013-05-05 13:51:51

回答

0

首先感谢tomexsans和lighta他们的帮助。对不起,我做了一个“愚蠢”的错误 - 花了大约2-3小时才意识到 - 索引不是跟随$ offset,因为我忘了使用这个变量。我使用一个整数,每当页面加载时我重置为1,因此索引将永远重置为1:P

1
public function index() { 
     //check authorization 
     if(!isset($_SESSION['username'])) 
      redirect('backend_umat/login'); 

     // the $offset value is true, but the index is still reseted to 1 


    //pagination 
    $config['base_url'] = base_url().'backend_umat/index'; 
    // basically you need a separate query to return only numrows 
    $config['total_rows'] = ?; 
    $config['per_page'] = 10; 
    $config['uri_segment'] = 3; 
    $config['full_tag_open'] = '<div id="pagination">'; 
    $config['full_tag_close'] = '</div>'; 

    $this->pagination->initialize($config); 
    $offset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; 

    $result = $this->umat_m->get_umat($config['per_page'], $offset); 

    $data['pagination'] = $this->pagination->create_links(); 

在这里试试这个,我改变了一些代码,希望这个作品。基本上我先初始化分页配置,然后再调用模型中的任何东西。只要做一个单独的查询来获得numrows。

+0

感谢您的帮助,我会很快尝试您的代码,并让您知道结果 – 2013-05-05 13:34:44

+0

对不起,您的代码不起作用,结果与我的代码相同,$ offset的值为true,但索引不起作用 – 2013-05-06 00:54:38

+0

@BlazeTama抱歉,但我不明白你的索引是什么意思? – tomexsans 2013-05-06 01:01:46

1

控制器 site.com/<controller>/index/<page>

public function index($offset = 0) { 
    //check authorization 
    if(!isset($_SESSION['username'])) 
     redirect('backend_umat/login'); 

    $limit = 10; 
    $offset = (int) $offset; 
    $result = $this->umat_m->get_umat($limit, $offset); 

    //pagination 
    $config['base_url']  = site_url('/backend_umat/index'); 
    $config['total_rows']  = $result['num_rows']; 
    $config['per_page']  = $limit; 
    $config['uri_segment'] = 3; 
    $config['full_tag_open'] = '<div id="pagination">'; 
    $config['full_tag_close'] = '</div>'; 

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

    $data['pagination'] = $this->pagination->create_links(); 

型号

public function get_umat($limit, $offset) { 
    $result['rows'] = $this->db 
     ->select('SQL_CALC_FOUND_ROWS, msumat.*, mskelas.*', FALSE) 
     ->limit($limit, $offset == 1 ? 0 : $offset) 
     ->join('mskelas', 'msumat.kelas_id = mskelas.kelas_id') 
     ->get('msumat') 
     ->result(); 

    $req = $this->db->query('SELECT FOUND_ROWS()')->row_array(); 
    $result['num_rows'] = $req['FOUND_ROWS()']; 

    return $result; 
} 

为了不必重写第二个SQL REQ,您可以使用SQL_CALC_FOUND_ROWS检索总如果请求不包含LIMIT声明。但是这可能比两个请求慢。

我使用FALSE作为select()中的第二个参数,以便CI不尝试使用反引号保护字段或表名。

->select('*'):是无用的,如果你希望所有领域,CI将在默认情况下做到这一点,如果选择方法不叫。

->get()->num_rows():改用->count_all_results([table])

+0

感谢您的帮助。我会很快尝试你的代码,但是你有更多“初学者”的方式来完成这个吗?我在nettuts上看到的教程提供了一个非常简单的代码(就像我的问题一样),但是我认为我犯了一个“小”错误,所以偏移量不起作用 – 2013-05-05 13:36:20

+0

好的但要小心,nettuts上的所有tuto并不总是更新。 – Aurel 2013-05-05 13:55:28