简体   繁体   中英

How to configure CodeIgniter pagination?

Here's my code:

$config['base_url'] = base_url() . 'settings/profile/';
$config['total_rows'] = $data['count_user_posts'];
$config['per_page'] = 3;
$offset = ($this->uri->segment('3') * $config['per_page']) / 2;
$this->pagination->initialize($config);
$config['use_page_numbers'] = True;
$data['user_posts'] = $this->post_model->get_user_posts($_SESSION['user_id'], $config['per_page'], $config['offset']);

Problem is when I click on 2 or any other link, it shows some data from previous page also. Any solution - what am I doing wrong?

Do like this

public function index($offset = 0) 
{
   $this->load->library('pagination');
  $limit = 10;

  $total = $this->legend_model->get_legend_count($language_id);

  $config['base_url'] = base_url().'legend/index/';
  $config['total_rows'] = $total;
  $config['per_page'] = $limit;
  $config['uri_segment'] = 3;

  $config['first_link'] = '<< First';
  $config['last_link'] = 'Last >>';
  $config['next_link'] = 'Next ' . '&gt;';
  $config['prev_link'] = '&lt;' . ' Previous';
  $config['num_tag_open'] = '<span class="number">';
  $config['num_tag_close'] = '</span>';

  $config['cur_tag_open'] = '<span class="current"><a href="#">';
  $config['cur_tag_close'] = '</a></span>';

  $this->pagination->initialize($config);
  $data['offset'] = $offset;
  $data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset);

  $this->template->write('title', 'Legend : Manage Legend');
  $this->template->write_view('content', 'legend/index', $data);
  $this->template->render();
}

In the model

 //Get legend
    public function get_legend($language_id = 1, $limit = 10, $offset = 0)
    {        
        $this->db->select('l.id,lt.title,lt.status');
        $this->db->from('legends l');
        $this->db->join('legend_translations lt', 'l.id = lt.legend_id');
        $this->db->where('lt.language_id', $language_id);
        $this->db->order_by('l.id DESC');
        $this->db->limit($limit, $offset);

        $legend = $this->db->get();

        return $legend->result();
    }

I have got it. this will be the code for $offset variable........... Thank's everyone for helping me....

if($this->uri->segment(4) > 0)
    $offset = ($this->uri->segment(4) + 0)*$config['per_page'] - $config['per_page'];
else
    $offset = $this->uri->segment(4);

Here is an example, how I had implemented

In controller

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

    $config = array();
    $config["base_url"] = base_url() . "city/index";
    $config["total_rows"] = $this->city_model->record_count();
    $config["per_page"] = 20;
    $config["uri_segment"] = 4;

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

    $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
    $data["city"] = $this->city_model->get_cities($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();
}

In model

function record_count() 
{
   return $this->db->count_all("city");
}

function get_cities($limit,$start)
{       

   $this->db->limit($limit,$start);
   $this->db->select('*');
   $this->db->from('city');
   $this->db->order_by('city_name');
   $query = $this->db->get();

   if ($query->num_rows() > 0) {
       foreach ($query->result() as $row) {
             $data[] = $row;
   }

return $data;
}

return false;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM