【问题标题】:Adding Pagination into codeigniter将分页添加到 codeigniter
【发布时间】:2016-01-24 16:38:17
【问题描述】:

我一直在尝试将分页添加到我的代码点火器项目中,尽管代码点火器参考资料说它“容易”,但我无法让它工作。我花了一些时间在谷歌上搜索并看到类似的东西,我做了我发现的所有事情,但仍然没有任何效果。我的一位朋友也在使用 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');
            }
    }
    }

根据我的观点,我有这个:

 <h1><a href="create">Create A blog Entry</a></h1>

        <?php echo $Pagination; ?>

  <div class="blog">
   <h2><?php echo "Blog"; ?></h2>
    <div class="row">
        <?php foreach ($blog as $blog_item): ?>
            <div class="col-md-6">
                <div class='panel panel-primary'>
                    <div class='panel-heading'>
                        <?php echo $blog_item['title']; ?>
                    </div>
                    <div class="panel-body">
                <?php echo  $blog_item['body']; ?>
            </div>
            <div class="panel-footer">
                <?php echo $blog_item['entry_date']; ?>
            </div>
          </div>
        </div>
        <?php endforeach; ?>
    </div>
</div>

最后,我的 config/autoload.php 文件有这个:

$autoload['libraries'] = array('database','Pagination');

我的模型上没有任何东西。如果有人想查看完整的代码,那就是我在 LAMPCAMP_Project 下的 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);
    }
}

【问题讨论】:

标签: php codeigniter pagination


【解决方案1】:

我会自动加载 url helper 而不是在模型中加载它。如果您已经自动加载它,请从模型中删除 $this-&gt;load-&gt;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_url()而不是site_url()

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

}

这里是 CI2 和 CI3 的用户指南链接

CI3:http://www.codeigniter.com/user_guide/libraries/pagination.html

CI2:http://www.codeigniter.com/userguide2/libraries/pagination.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多