使用分页CI列出和路由帖子

时间:2018-10-04 03:33:27

标签: php codeigniter

  

印度尼西亚语=英语

     

baca =已读

     

judul =标题

     

kepala =标头

     

kaki =页脚

我有routes.php

\.\.  # Escape for 2 dots `..`
\/    # Escapefor slash `/`
.*?   # Takes any character except for line terminators in between other listed characters (above and below)
\/    # Escape for slash `/`

型号:Beranda_model.php

$route['blog']                 = 'beranda/blog';
$route['blog/baca/(:any)']     = 'beranda/baca/$1';
$route['blog/page/(:num)']     = 'beranda/page/$1';

如何创建“博客”功能? 管理员:Beranda.php

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

  public function record_count() {
    return $this->db->count_all('posts');
  }

  public function halaman_blog($limit, $start){
    $this->db->limit($limit, $start);
    $query = $this->db->get('posts');

    if ($query->num_rows()>0) {
      foreach ($query->result() as $row) {
        $data[] = $row;
      }
      return $data;
    }
    return false;
  }

  public function get_posts($slug = FALSE){
    if($slug == FALSE){
      $this->db->order_by('id', 'desc');
      $this->db->limit(2);
      $query = $this->db->get('posts');
      return $query->result_array();
    }
    $query = $this->db->get_where('posts', array('slug' => $slug));
    return $query->row_array();
  }

视图:beranda / blog.php

public function __construct(){
    parent::__construct();
    $this->load->model('beranda_model');
    $this->load->helper('url_helper');
        $this->load->helper('text');
        $this->load->library('pagination');
  }

    public function index(){
        $data['judul'] = 'Test Title';
        $this->load->view('beranda/kepala', $data);
    $this->load->view('beranda/utama');
    $this->load->view('beranda/kaki');
    }

    public function blog(){
        $config = array();
        $config['base_url']   = base_url().'blog/page/';
        $config['total_rows'] = $this->beranda_model->record_count();
        $config['per_page']   = 2;
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['posts'] = $this->beranda_model->halaman_blog($config['per_page'], $page);
        $data['links']= $this->pagination->create_links();


        $header['judul']    = 'Page Name - Title';
        // $data['posts']      = $this->beranda_model->get_posts();


        $this->load->view('beranda/kepala', $header);
    $this->load->view('beranda/blog', $data);
    $this->load->view('beranda/kaki');
    }

    public function baca($slug = NULL){
    $data['posts_item'] = $this->beranda_model->get_posts($slug);

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

        $header['judul'] = $data['posts_item']['title'].' - Testing Title Here';

    $this->load->view('beranda/kepala', $header);
        $this->load->view('beranda/baca', $data);
    $this->load->view('beranda/kaki');
    }

我应该怎么做:

  1. 无法加载请求的文件:blog / page / 2.php
  2. 我想在视图中显示我的帖子列表:beranda / blog.php
  3. 如果我无法在Views:beranda / blog.php中显示我的帖子列表,那怎么办?

0 个答案:

没有答案