CodeIgniter分页不起作用(在其他页面中)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeIgniter分页不起作用(在其他页面中)相关的知识,希望对你有一定的参考价值。

我是php的新手,也是CI的新手。我正在创建自己的Web应用程序,我遇到了分页问题。

我对索引的主要分页是没有问题的魅力,但是如果我在其他页面中启动分页,它会显示链接以及分页,但是当我点击时,页面刷新wish / 6(每页内容)显示相同的东西从数据库生成的。

这是我在函数索引中使用的代码:

public function index($offset = 0)
        $config['base_url'] = base_url() . '/usernames/index/';
        $config['total_rows'] = $this->db->count_all('usernames');
        $config['per_page'] = 6;
        $config['uri_segment'] = 3;

        $config['full_tag_open'] = '<ul class="pagination justify-content-center">';
        $config['full_tag_close'] = '</ul>';
        $config['attributes'] = ['class' => 'page-link'];
        $config['first_link'] = false;
        $config['last_link'] = false;
        $config['first_tag_open'] = '<li class="page-item">';
        $config['first_tag_close'] = '</li>';
        $config['prev_link'] = 'Previous';
        $config['prev_tag_open'] = '<li class="page-item">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = 'Next';
        $config['next_tag_open'] = '<li class="page-item">';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li class="page-item">';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="page-item active"><a href="#" class="page-link">';
        $config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
        $config['num_tag_open'] = '<li class="page-item">';
        $config['num_tag_close'] = '</li>';
        $config['attributes'] = array('class' => 'page-link');

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

        $data['title'] = 'Lastest Usernames';

        $data['posts'] = $this->post_model->get_usernames(FALSE, $config['per_page'], $offset);

        $this->load->view('templates/header');
        $this->load->view('usernames/index', $data);
        $this->load->view('templates/footer');
    

这是我在功能Snapchat中使用的:

public function snapchat($offset = 0)
        $config['base_url'] = base_url() . '/usernames/snapchat/';
        $config['total_rows'] = $this->db->count_all('usernames');
        $config['per_page'] = 6;
        $config['uri_segment'] = 3;

        $config['full_tag_open'] = '<ul class="pagination justify-content-center">';
        $config['full_tag_close'] = '</ul>';
        $config['attributes'] = ['class' => 'page-link'];
        $config['first_link'] = false;
        $config['last_link'] = false;
        $config['first_tag_open'] = '<li class="page-item">';
        $config['first_tag_close'] = '</li>';
        $config['prev_link'] = 'Previous';
        $config['prev_tag_open'] = '<li class="page-item">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = 'Next';
        $config['next_tag_open'] = '<li class="page-item">';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li class="page-item">';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="page-item active"><a href="#" class="page-link">';
        $config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
        $config['num_tag_open'] = '<li class="page-item">';
        $config['num_tag_close'] = '</li>';
        $config['attributes'] = array('class' => 'page-link');

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

        $data['title'] = 'Lastest Snapchat Usernames';

        $data['posts'] = $this->post_model->get_usernames(FALSE, $config['per_page'], $offset);
        $filter = "Snapchat";
        $filteredData = $this->post_model->get_snapchat($filter);

        $data=[
            'F_username' => $filteredData->result_array()
        ];

          $this->load->view('templates/header');
          $this->load->view('usernames/snapchat', $data);
          $this->load->view('templates/footer');
    

这是我在模型中使用的代码:

public function get_snapchat($filter, $slug = FALSE, $limit = FALSE, $offset = FALSE)
        if ($limit) 
            $this->db->limit($limit ,$offset);
        
        $filter = "Snapchat";

        $query = $this->db->get_where('usernames', array('platform' => $filter));
        return $query;
    
答案

这是最简单的步骤:

  1. pagination.php中创建application/config以从控制器中分离分页配置(https://gist.github.com/mikedfunk/3504432
  2. 楷模 :

/* Your_model.php */
// -------------------

// Get Data With Limit
public function data($start, $limit) 

  return $this->db->from('TABLE_NAME')
                  ->limit($start, $limit)
                  ->get();


// Total All Rows
public function total() 

  return $this->db->count_all_results('TABLE_NAME');


// Get First Records
public function first_record($current_page, $per_page) 

  return ($current_page - 1) * $per_page;


  1. 控制器:
/* Your_controller.php */
// -------------------

public function output() 

  // Library
  $this->load->library('pagination');

  // Pagination Config
  $config['base_url'] = site_url('Your/Controller');
  $config['per_page'] = 30;
  $config['uri_segment'] = 3;

  // Starting
  $curpage = $this->uri->segment(3);
  $start = $this->your_model->first_record($curpage, $config['per_page']);
  $limit = $config['per_page'];

  // Get Result
  $data['result'] = $this->your_model->data($start, $limit);

  // Get Total Records
  $config['total_rows'] = $this->your_model->total();

  // Process Pagination
  $this->pagination->initialize($config);

  // Prep Data
  $data['navigation'] = $this->pagination->create_links();

  // Load View (Passing $result & $navigation data)
  $this->layout->view('YOUR_VIEW_HERE', $data);


你需要调整你的数据库配置:)

以上是关于CodeIgniter分页不起作用(在其他页面中)的主要内容,如果未能解决你的问题,请参考以下文章

为啥我在 codeigniter 中的分页不起作用? (404 未找到)

CodeIgniter 3分页不起作用

Android房间分页不起作用

jquery数据表服务器端分页不起作用

数据表分页不起作用?

woocommerce产品类别短代码分页不起作用