Codeigniter,设置分页

时间:2012-11-26 19:53:43

标签: php codeigniter

所以,我在我的网站上设置分页时遇到了问题。

首先,如果我在我网站的第一页,并按下转到第2页,“1”仍然保持粗体。虽然我可以在网址中看到我在第2页。

其次,我需要一些帮助来正确检索数据。如果我在第1页,我想获得所有记录1-10。如果我在第2页,我想从11-20获得所有记录。知道了吗?

我已经google'd并尝试找到任何解决方案,但没有任何成功。

$this->db->select('*');
$this->db->from('comments');
$this->db->where('comments.url_friendly', $id);
$this->db->join('allt', 'allt.url_friendly = comments.url_friendly', 'left');
$this->db->order_by('comments.date', 'asc');
$data['query'] = $this->db->get();

$data['title'] = 'Kommentarer - '.$data['query']->row()->amne;

$config['base_url'] = base_url(). 'kommentarer/'.$data['query']->row()->url_friendly;
$config['total_rows'] = $data['query']->num_rows();
$config['per_page'] = 10;

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

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

另外,我正在使用this thread中的帖子2中的代码。

是的,我是Codeigniter的新手,你可以看到。

3 个答案:

答案 0 :(得分:1)

$qry = "select * from comment c left join allt a on c.url_friendly = a.url_friendly where c.url_friendly = {$id}";

$per_page = 10;
$offset = ($this->uri->segment(3) != '' ? $this->uri->segment(3):0);//this will get the current page(assuming that page number is in 3rd uri segment). if youy on page one,this will be set to zero

$config['base_url'] = base_url(). 'kommentarer/'.$data['query']->row()->url_friendly;
$config['total_rows'] = $this->db->query($qry)->num_rows();
$config['per_page'] = $per_page;
$config['uri_segment'] = 3; //this is dependent to where your page number displays in url

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

$qry .= " limit {$per_page} offset {$offset} ";
$data['result'] = $this->db->query($qry)->result();

$data['pagination'] = $this->pagination->create_links();
希望这会有所帮助。

答案 1 :(得分:0)

在您做了正确的更改后尝试此代码......... codeIgniter提供了一个内置的Pagination类。您可以在用户指南中找到它。

在函数中定义一个起始索引变量,您希望将分页用作零。

  public function pagination($start_index = 0)
   {

     $result = $this->model->get_results($data); //this $data is the argument which you are passing to your model function. If you are using database to get results array.

     $items_per_page = 10;   //this is constant variable which you need to define

      $filtered_result = array_splice($result, $start_index, ITEM_PER_PAGE_USERS);

     $model['$filtered_result'] = $filtered_result;

     $total_rows = count($result);

     $model['page_links'] = create_page_links (base_url()."/controlelr_name/pagination",ITEM_PER_PAGE_USERS, $total_rows);

     $this->load->view('controller_name/view_file_name', $model);
 }

    function create_page_links($base_url, $per_page, $total_rows) {

      $CI = & get_instance();
      $CI->load->library('pagination');

      $config['base_url'] = $base_url;
      $config['total_rows'] = $total_rows;
      $config['per_page'] = $per_page; 

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

      return $CI->pagination->create_links();
   }

这个创建页面链接功能是一个通用函数.............更多解释从用户指南检查分页类......

答案 2 :(得分:0)

$query = $this->db->query("SELECT COUNT(*) as jml from comments");
    foreach ($query->result() as $row) {
        $row = $row->jml;
    } // to count all result in your table


    $config['base_url'] = base_url() . 'comments/index/'; // set the base url for pagination
    $config['total_rows'] = $row; // total rows
    $config['per_page'] = '10'; // the number of per page for pagination
    $config['uri_segment'] = 3; // see from base_url. 3 for this case
    $config['first_link'] = 'Awal';
    $config['last_link'] = 'Akhir';
    $config['next_link'] = 'Selanjutnya';
    $config['prev_link'] = 'Sebelumnya';
    $this->pagination->initialize($config);
    $data['comments'] = $this->MArtikel->getAllCommentsPagination($config['per_page'], $this->uri->segment(3));
    // end pagination