本文介绍了CodeIgniter分页不起作用(在其他页面中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new in php, and in CI as well. I'm creating my own web application, and i've encountered an issue with pagination.

我在索引上的主要分页功能很不错,没有问题,但是如果我在其他页面上初始化分页,它会显示链接和分页,但是当我单击时,页面会刷新,希望显示为/6(每页内容)显示从数据库生成的相同内容.

My main pagination on index is working as charm without problems, but if i init the pagination in other pages, it shows links as well as the pagination, but when i click, the page refreshes wish /6 (content per page) showing same stuff that have been generated from the database.

那是我在功能索引中使用的代码:

That is the code i use in function index:

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

这是我在功能手套中所使用的:

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

这是我在 model 中使用的代码:

This is the code i'm using in model:

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. application/config中创建pagination.php,以将分页配置与您的控制器分开( https: //gist.github.com/mikedfunk/3504432 )

  1. Create pagination.php in application/config to separate pagination config from your Controller (https://gist.github.com/mikedfunk/3504432)

模型:


/* 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);

}

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

You need adjust to your database config :)

这篇关于CodeIgniter分页不起作用(在其他页面中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:10