如何配置CodeIgniter分页?

时间:2013-02-18 08:24:59

标签: php codeigniter

这是我的代码:

$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']);

问题是当我点击2或任何其他链接时,它也会显示上一页的一些数据。任何解决方案 - 我做错了什么?

3 个答案:

答案 0 :(得分:1)

这样做

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

在模型中

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

答案 1 :(得分:1)

我知道了。这将是$ offset变量的代码...........感谢所有人帮助我......

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

答案 2 :(得分:1)

这是一个例子,我是如何实现的

在控制器中

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

在模型中

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;

}