用codeigniter写的博客的简单分页

时间:2017-03-12 17:30:01

标签: php codeigniter pagination

我正在尝试在我的CodeIgniter博客中创建一个简单的分页,这是我的代码,我正在使用自动加载器

$autoload['libraries'] = array('database','session','pagination');
$autoload['helper'] = array('form','url');

我的控制器;

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('BlogModel');
}
public function index($offset=0)
{
$this->load->library('table');
$config["base_url"] = base_url('blog');
//$config["total_rows"] = $this->BlogModel->get_Count(); // There are currently 36 Posts
$config["total_rows"] = 36; // There are currently 36 Posts
$config["per_page"] = 5; // Change limit to suit what you would like
$config['use_page_numbers'] = TRUE;
$limit = $config['per_page'];
$this->pagination->initialize($config);
$blog_pagination = $this->pagination->create_links();
$Posts  = $this->BlogModel->get_Posts($limit, $offset);
$assignData=array('data'=>$Posts,'blog_pagination'=>$blog_pagination);
$this->load->view('header');
$this->load->view('blog', $assignData);
$this->load->view('footer');
}
}

我在我的模型中有这个;

<?php
class BlogModel extends CI_Model {
    public function __construct(){
        // Call the Model constructor
        parent::__construct();
        $this->load->library('memcached_library');
    }
     public function get_Posts($limit, $offset){
            $this->db->select('*');
            $this->db->where('post_status','published');
            $query= $this->db->get('cms_posts',$limit, $offset);
            $data=$query->result_array();
            return $data;
    }
    public function get_Count() {
        //$query = $this->db->get($this->db->dbprefix . 'blog');
            $this->db->select('*')->from('cms_posts');
            $this->db->where('post_status','published');
            $query= $this->db->get();
            //$data=$query->result_array();
            return $query->num_rows();
    }
}
?>

我在我的视图中使用它;

<div id="primary" class="left-column">
<?php 
if(!empty($data)){
foreach($data as $row){
?>
                <main id="main" role="main">
                    <article id="<?php echo $row['post_id']; ?>" class="post-item">
                        <div class="right">
                            <div class="post-thumbnail">
                                <img src="<?php echo base_url()."assets/images/uploads/test_n-150x150.jpg";?>" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" sizes="(max-width: 320px) 100vw, 320px" width="320" height="143">
                             </div>
                                <!-- END .post-thumbnail -->
                        </div>
                        <div class="left">
                            <h2 class="post-title">
                                <a href="<?php echo base_url()."blog/".$row['post_url']; ?>" title="<?php echo $row['post_title']; ?>" rel="bookmark"><?php echo $row['post_title']; ?></a>
                             </h2>
                        <div class="post-meta">
                            <span class="vcard author">
                                <a href="<?php echo base_url()."author/".$row['post_author']; ?>" class="vcard author"><?php echo ucwords($row['post_author']); ?></a>
                            </span>
                            <time class="entry-date published" datetime="<?php echo $row['post_date']; ?>"><?php echo date("d M Y", strtotime($row['post_date'])); ?></time>
                       </div>
                       <div class="post-excerpt">
                        <p>
                            <?php
                            echo(substr($row['post_content'], 0, 250)); 
                            <a class="read-more" href="<?php echo base_url()."blog/".$row['post_url']; ?>">Read More  &raquo;</a>
                        </p>
                       </div>
                        <!-- END .post-excerpt -->
                        </div>
                        <!-- END .left -->
                    </article>
                    <!-- END .post-item -->
                </main>
                <?php
                     }
                        }
                 ?>
                <!-- END .site-main -->
            <div class="pagination-wrap">
                <?php echo $blog_pagination; ?>
            </div>      
            </div>

修改: 我的路线如下:

$route['default_controller'] = 'index';
$route['blog/:any'] = 'blog/post/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

我不知道我错过了哪些内容,请在此代码中引导我或纠正我。那个你

更新 我的博客现在显示5在第一页上发布并创建分页但仅在 123&gt; Last> 分页创建并且在偏移2(/ blog / 2)上有页面空白。

2 个答案:

答案 0 :(得分:1)

也许你可以使用我的方法使用uri段来$ offset,就像这样

public function house()
{
    $config['base_url']     = site_url().'/user/house/';
    $config['total_rows']   = $this->houses->select_row_house_design();
    $config['per_page']     = 12;
    $config['cur_tag_open'] = '<li><a><b>';
    $config['cur_tag_close'] = '</li></a></b>';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $this->pagination->initialize($config);
    $from = $this->uri->segment('3');
    $data['design'] = $this->houses->select_all_house_design($config['per_page'],$from);
    $title['menu'] = 'house design';
    $this->template('user/house',$data,$title);
}

答案 1 :(得分:0)

像这样设置控制器, 这也很简单

        <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('BlogModel');
$this->load->library('table');
}
public function index($offset=0)
{

$config['base_url'] = site_url('blog/index');
$config["total_rows"] = $this->BlogModel->get_Count(); // There are currently 36 Posts
$config["per_page"] = 5; // Change limit to suit what you would like
$config['num_links'] = 10;
/*use this code for bootstrap 
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a  class="">';
        $config['cur_tag_close'] = '</a></li>';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '<li>';
        $config['next_tag_open'] = '<li class="next">';
        $config['next_tag_close'] = '</li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['first_link'] = '<<';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = '>>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

*/

$this->pagination->initialize($config);
$data['blog_pagination'] = $this->pagination->create_links();
$data['posts']  = $this->BlogModel->get_Posts($config['per_page'],$this->uri->segment(3));
$this->load->view('header');
$this->load->view('blog', $data);
$this->load->view('footer');
}
}