doudi5291 2018-04-28 21:40
浏览 58
已采纳

无法在codeigniter“slug”上加载特定页面

I'm trying to load a specific page within a controller. I followed the Codeigniter tutorial and the main pages work but the individual page (loaded with view) doesn't load according to the given slug.

blog.php

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

class Blog extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/index
 *  - or -
 *      http://example.com/index.php/Index/index
 *  - or -

 */

function __construct()
{
    parent::__construct();
    $this->load->model('blog_model');
    $this->load->helper('url_helper');
} 
public function index()
{
    $data['post'] = $this->blog_model->get_posts();
    $data['title'] = 'Blog archive';

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

public function view($slug = NULL)
{
    $data['post'] = $this->blog_model->get_posts($slug);    

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

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

    $this->load->view('header', $data);
    $this->load->view('post', $data);
    $this->load->view('footer', $data);
}
}

blog_model.php

<?php
class Blog_model extends CI_Model {

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

    public function get_posts($slug = FALSE)
    {

    if ($slug === FALSE)
    {
            $this->db->select('*');
            $this->db->from('blog_posts');
            $this->db->join('category', 'category.id = blog_posts.category_id');
            $this->db->join('author', 'author.id = blog_posts.author_id');
            $query = $this->db->get();
            return $query->result_array();
    }

    $this->db->select('*');
    // $this->db->from('blog_posts');
    $this->db->join('category', 'category.id = blog_posts.category_id');
    $this->db->join('author', 'author.id = blog_posts.author_id');
    // $this->db->where('slug', $slug);
    $query = $this->db->get_where('blog_posts', array('slug' => $slug));
    return $query->row_array();
    }

}

As you can see I tried a few combinations because I'm not sure it's retrieving the table in get_posts when slug is not false.

  • 写回答

2条回答 默认 最新

  • dtrphb5597 2018-04-29 04:30
    关注

    try like this

    call url_helper helper in __construct like following

    $this->load->helper('url');
    

    Now, update the routes.php like following

    If your URL like

    http://www.example.com/blog/view/slug

    Your ruote should be like this

    $route['blog/view/(:any)'] = 'blog/view/$1';
    

    If your URL like

    http://www.example.com/view/slug

    Your ruote should be like this

    $route['view/(:any)'] = 'blog/view/$1';
    

    And, your get_posts model function repeating queries, use it simply like below

    public function get_posts($slug = FALSE){
        $this->db->select('*');
        $this->db->from('blog_posts');
        $this->db->join('category', 'category.id = blog_posts.category_id');
        $this->db->join('author', 'author.id = blog_posts.author_id');
        if($slug){
            $this->db->where(compact('slug'));
        }
        $query = $this->db->get();
        return ($query->num_rows() > 1) ? $query->result_array() : $query->row_array();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口