Php 将分页添加到codeigniter中

Php 将分页添加到codeigniter中,php,codeigniter,pagination,Php,Codeigniter,Pagination,我一直试图将分页添加到我的CodeIgniter项目中,尽管CodeIgniter参考资料说这很“容易”,但我无法让它工作。我花时间在谷歌上搜索,看到了类似的东西,我做了所有我发现的事情,但仍然没有任何效果。我的一个朋友也在使用codeigniter,他也试图帮助我,但没能让它工作。我想我很接近,可能只是忘记了一切。不管怎么说,我正在写一个博客,除了分页之外,其他一切都很好!我目前在控制器中的索引方法下有分页代码,如下所示: public function index()

我一直试图将分页添加到我的CodeIgniter项目中,尽管CodeIgniter参考资料说这很“容易”,但我无法让它工作。我花时间在谷歌上搜索,看到了类似的东西,我做了所有我发现的事情,但仍然没有任何效果。我的一个朋友也在使用codeigniter,他也试图帮助我,但没能让它工作。我想我很接近,可能只是忘记了一切。不管怎么说,我正在写一个博客,除了分页之外,其他一切都很好!我目前在控制器中的索引方法下有分页代码,如下所示:

       public function index()
    {

            $data['blog'] = $this->Blog_model->get_blog();
            $data['title'] = 'Blog archive';

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

            $config['base_url'] = site_url('blog');
            $config['total_rows'] = 1;
            $config['per_page'] = 1;


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

            $data['Pagination'] = $this->pagination->create_links();

            //echo $this->pagination->create_links();

            //END Pagination

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

    }
整个控制器如下所示:

     <?php
   defined('BASEPATH') OR exit('No direct script access allowed');

    class Blog extends CI_Controller {

    public function __construct()
    {
            parent::__construct();


            $this->load->model('Blog_model');
            $this->load->helper('url_helper');
    }

    public function index()
    {

            $data['blog'] = $this->Blog_model->get_blog();
            $data['title'] = 'Blog archive';

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

            $config['base_url'] = site_url('blog');
            $config['total_rows'] = 1;
            $config['per_page'] = 1;


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

            $data['Pagination'] = $this->pagination->create_links();

            //echo $this->pagination->create_links();

            //END Pagination

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

    }

    public function view($slug = NULL)
    {

            $data['blog_item'] = $this->Blog_model->get_blog($slug);

            if (empty($data['blog_item']))
            {
                    show_404();
            }

            $data['title'] = $data['blog_item']['title'];

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

    }
    public function create()
    {
            $this->load->helper('form');
            $this->load->library('form_validation');

            $data['title'] = 'Create a news item';

            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('body', 'Body', 'required');

            if ($this->form_validation->run() === FALSE)
            {
                    $this->load->view('templates/header', $data);
                    $this->load->view('blog/create');
                    $this->load->view('templates/footer');

            }
            else
            {
                    $this->Blog_model->set_blog();
                    redirect('blog');
                    //$this->index();
                    //$this->load->view('templates/header');
                    //$this->load->view('../../blog/index', $data);
                    //$this->load->view('templates/footer');
            }
    }
    }
我的模型上没有任何东西。如果你想看到完整的代码,可以在LAMPCAMP_项目下看到我的github页面,我的名字是ravenusmc。谢谢你在这方面的帮助,祝你度过愉快的一天

我添加了模型代码:

<?php
  class Blog_model extends CI_Model {

    public function __construct()
    {
            $this->load->database();
    }

    public function get_blog($slug = FALSE)
            {
    if ($slug === FALSE)
    {
            $query = $this->db->order_by('entry_date', 'desc')->get('blog');
            return $query->result_array();
    }

    $query = $this->db->get_where('blog', array('slug' => $slug));
    return $query->row_array();
}

    public function set_blog()
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body')
        );

        return $this->db->insert('blog', $data);
    }
}

我将自动加载url帮助程序,而不是在模型中加载它。并删除
$this->load->database()从模型,如果您已经自动加载它

$autoload['helper'] = array('url');

$autoload['libraries'] = array('database','pagination');
其次,我建议为总计行创建一个计数函数

public function count_total() {
    $query = $this->db->get($this->db->dbprefix . 'blog');
    return $query->num_rows();
}
对于get函数,需要一个get和limit以及offset变量

public function fetch_blogs($limit, $start, $slug) {

     if ($slug == FALSE) {

        $this->db->limit($limit, $start);
        $this->db->order_by('entry_date', 'desc');
        if ($query->num_rows() > 0 {
            return $query->result_array();
           } else {
             return FALSE;
        }
     }

     $this->db->limit($limit, $start);

     $query = $this->db->get_where($this->db->dbprefix . 'blog', array('slug' => $slug));

     if ($query->num_rows() > 0 {
        return $query->result_array();
     } else {
        return FALSE;
     }
}
application/config/routes.php
如果您使用博客路由,我认为您需要在博客中创建用于分页的I路由

$route['blog'] = 'blog/index';
$route['blog/(:any)'] = 'blog/index/$1';
博客控制器

首先使用
base\u url()
insead of
site\u url()


您的型号代码在哪里?阅读此内容可能有助于您尝试该页面,但仍然无法工作。。
public function fetch_blogs($limit, $start, $slug) {

     if ($slug == FALSE) {

        $this->db->limit($limit, $start);
        $this->db->order_by('entry_date', 'desc');
        if ($query->num_rows() > 0 {
            return $query->result_array();
           } else {
             return FALSE;
        }
     }

     $this->db->limit($limit, $start);

     $query = $this->db->get_where($this->db->dbprefix . 'blog', array('slug' => $slug));

     if ($query->num_rows() > 0 {
        return $query->result_array();
     } else {
        return FALSE;
     }
}
$route['blog'] = 'blog/index';
$route['blog/(:any)'] = 'blog/index/$1';
<?php

class Blog extends CI_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->model('blog_model');
  }

  public function index() {
    $config["base_url"] = base_url('blog');
    $config["total_rows"] = $this->blog_model->count_total();
    $config["per_page"] = 5; // Change limit to suit what you would like
    $config["uri_segment"] = 2;

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

    $data['Pagination'] = $this->pagination->create_links();

    $start = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;

    // Fetch Blogs

    $slug = '' // What ever you need slug to be example uri segment()

    $data['blog'] = $this->blog_model->fetch_blogs($config["total_rows"], $start, $slug);


    $this->load->view('templates/header', $data);
    $this->load->view('blog/blog_view', $data); // change index to different view name
    $this->load->view('templates/footer');
  }

}