0

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

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

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

我用谷歌搜索并试图找到任何解决方案,但没有任何成功。

$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();

另外,我在这个线程中使用帖子 2 中的代码。

是的,如您所见,我是 Codeigniter 的新手。

4

3 回答 3

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();

希望这会有所帮助。

于 2012-11-27T02:59:27.403 回答
0

在你做适当的改变后试试这个代码............ codeIgniter 提供了一个内置的分页类。您可以在用户指南中找到它。

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

  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();
   }

此创建页面链接功能是一个通用功能......更多解释请查看用户指南中的分页类......

于 2012-11-27T01:04:42.593 回答
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
于 2012-11-27T04:18:09.613 回答