codeigniter分页链接超出

时间:2013-12-26 03:44:52

标签: php codeigniter pagination

我在我的网站上有这个分页。我的问题是下面的链接超出了太多。例如,它转到第228页,那里就没有数据了。

这是我的控制器代码

 public function index(){
    $this->data['title'] = 'Programs | Spring Rain Global Consultancy Inc';
    $this->data['active_menu'] = 'programs';
    $this->data['active_link'] = 'program_menu';

    $config = array();
    $config['base_url'] = base_url(). "programs/index";
    $config['total_rows'] = $this->am->record_count();
    $config['per_page'] = 12;
    $config['uri_segment'] = 3;
    $choice = $config['total_rows'] / $config['per_page'];
    $total_rows = $config['total_rows'] = $this->am->record_count();
    $config['num_links'] = round($choice);

    $this->pagination->initialize($config);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $this->data['fecth_activities'] = $this->am->getAllActivities($config['per_page'], $page);
    $this->data['links'] = $this->pagination->create_links();
    //echo "<pre>";
    //print_r($this->data['links']);
    //echo "</pre>";

    $this->load->view('pages/header', $this->data);
    $this->load->view('templates/programs', $this->data);
    $this->load->view('pages/footer');
    $this->load->view('pages/footer_below');
  }

以下是我的模型

class Activities_model extends CI_Model{
  public function record_count(){
    return $this->db->count_all('seminars');
  }

  public function getAllActivities($start, $limit){
    return $this->db->limit($start, $limit)
                    ->select('seminars.id,
                        seminars.category, 
                        seminars.thumb, 
                        seminars_category.id,
                        seminars_category.category,
                        seminars_category.url,
                        seminars_category.date,
                        seminars_category.abvr'
                      )
                    ->from('seminars')
                    ->join('seminars_category', 'seminars_category.id = seminars.category')
                    ->where('seminars.id  =', 1)
                    ->or_where('seminars.id =', 7)
                    ->or_where('seminars.id =', 12)
                    ->or_where('seminars.id =', 20)
                    ->or_where('seminars.id =', 24)
                    ->or_where('seminars.id =', 29)
                    ->or_where('seminars.id =', 77)
                    ->or_where('seminars.id =', 84)
                    ->or_where('seminars.id =', 103)
                    ->or_where('seminars.id =', 157)
                    ->or_where('seminars.id =', 187)
                    ->or_where('seminars.id =', 234)
                    ->or_where('seminars.id =', 241)
                    ->or_where('seminars.id =', 308)
                    ->or_where('seminars.id =', 314)
                    ->or_where('seminars.id =', 390)
                    ->or_where('seminars.id =', 407)
                    ->or_where('seminars.id =', 414)
                    ->or_where('seminars.id =', 429)
                    ->or_where('seminars.id =', 455)
                    ->or_where('seminars.id =', 462)
                    ->or_where('seminars.id =', 511)
                    ->or_where('seminars.id =', 537)
                    ->or_where('seminars.id =', 573)
                    ->or_where('seminars.id =', 591)
                    ->or_where('seminars.id =', 641)
                    ->order_by('seminars_category.date', 'DESC')
                    ->get()->result_object();
    $query = $this->db->get('seminars');
    if($query->num_rows > 0){
      foreach($query->result() as $row){
        $data[] = $row;
      }
      return $data;
    }
    return false;

  }

}

这件事有效但只有下面的链接超过228。

1 个答案:

答案 0 :(得分:1)

试试这个,

class Activities_model extends CI_Model {

    public function record_count() {

        $this->db->where('seminars.id  =', 1)
                        ->or_where('seminars.id =', 7)
                        ->or_where('seminars.id =', 12)
                        ->or_where('seminars.id =', 20)
                        ->or_where('seminars.id =', 24)
                        ->or_where('seminars.id =', 29)
                        ->or_where('seminars.id =', 77)
                        ->or_where('seminars.id =', 84)
                        ->or_where('seminars.id =', 103)
                        ->or_where('seminars.id =', 157)
                        ->or_where('seminars.id =', 187)
                        ->or_where('seminars.id =', 234)
                        ->or_where('seminars.id =', 241)
                        ->or_where('seminars.id =', 308)
                        ->or_where('seminars.id =', 314)
                        ->or_where('seminars.id =', 390)
                        ->or_where('seminars.id =', 407)
                        ->or_where('seminars.id =', 414)
                        ->or_where('seminars.id =', 429)
                        ->or_where('seminars.id =', 455)
                        ->or_where('seminars.id =', 462)
                        ->or_where('seminars.id =', 511)
                        ->or_where('seminars.id =', 537)
                        ->or_where('seminars.id =', 573)
                        ->or_where('seminars.id =', 591)
                        ->or_where('seminars.id =', 641)
                > from('seminars');
        return $this->db->count_all_results();
    }

    public function getAllActivities($start, $limit) {
        return $this->db->limit($start, $limit)
                        ->select('seminars.id,
                            seminars.category, 
                            seminars.thumb, 
                            seminars_category.id,
                            seminars_category.category,
                            seminars_category.url,
                            seminars_category.date,
                            seminars_category.abvr'
                        )
                        ->from('seminars')
                        ->join('seminars_category', 'seminars_category.id = seminars.category')
                        ->where('seminars.id  =', 1)
                        ->or_where('seminars.id =', 7)
                        ->or_where('seminars.id =', 12)
                        ->or_where('seminars.id =', 20)
                        ->or_where('seminars.id =', 24)
                        ->or_where('seminars.id =', 29)
                        ->or_where('seminars.id =', 77)
                        ->or_where('seminars.id =', 84)
                        ->or_where('seminars.id =', 103)
                        ->or_where('seminars.id =', 157)
                        ->or_where('seminars.id =', 187)
                        ->or_where('seminars.id =', 234)
                        ->or_where('seminars.id =', 241)
                        ->or_where('seminars.id =', 308)
                        ->or_where('seminars.id =', 314)
                        ->or_where('seminars.id =', 390)
                        ->or_where('seminars.id =', 407)
                        ->or_where('seminars.id =', 414)
                        ->or_where('seminars.id =', 429)
                        ->or_where('seminars.id =', 455)
                        ->or_where('seminars.id =', 462)
                        ->or_where('seminars.id =', 511)
                        ->or_where('seminars.id =', 537)
                        ->or_where('seminars.id =', 573)
                        ->or_where('seminars.id =', 591)
                        ->or_where('seminars.id =', 641)
                        ->order_by('seminars_category.date', 'DESC')
                        ->get()->result_object();
        $query = $this->db->get('seminars');
        if ($query->num_rows > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

}

你正在计算数据库中的所有元素......把条件放在 record_count 函数上,它可以解决你的问题