简体   繁体   English

CodeIgniter查看博客文章页面

[英]CodeIgniter View Blog Post Page

Hello guys i am trying to create a blog. 大家好,我正在尝试创建一个博客。 The first home page is ok.. i am getting the shrot description and the categories from the database but...i have problems with the links: 第一个主页可以。.我从数据库中获取了shrot描述和类别,但是...我在链接方面遇到了问题:

this are my controller functions: 这是我的控制器功能:

public function index()
    {
        $this->load->model('Model_cats');
        $data['posts'] = $this->Model_cats->getLivePosts(10); 
        $data['cats'] = $this->Model_cats->getTopCategories(); 
        $data['title'] = 'Welcome to Paul Harbuz Blog Spot!';
        $data['main'] = 'public_home';
        $this->load->vars($data);
        $this->load->view('template', $data);
    }

    public function category($id)
    {
        $data['category'] = $this->Model_cats->getCategory($id);
        $data['posts'] = $this->Model_cats->getAllPostsByCategory($id);
        $data['cats'] = $this->Model_cats->getTopCategories();
        $data['title'] = $data['category']['name'];
        $data['main'] = 'public_home';
        $this->load->vars($data);
        $this->load->view('template', $data);
    }
    public function post($id)
    {
        $data['post'] = $this->Model_cats->getPost($id);
        $data['comments'] = $this->Model_cats->getComments($id);
        $data['cats'] = $this->Model_cats->getTopCategories();
        $data['title'] = $data['post']['title'];
        $data['main'] = 'public_post';
        $this->load->vars($data);
        $this->load->view('template');
    }

this are my model function: 这是我的模型函数:

function getTopCategories()
    {
        $this->db->where('parentid',0);
        $query = $this->db->get('categories');
        $data = array();

        if ($query->num_rows() > 0)
        {
            foreach ($query->result_array() as $row)
            {
                $data[$row['id']] = $row['name'];
            }
        }

        $query->free_result();
        return $data;
    }

    function getLivePosts($limit)
    {
        $data = array();

        $this->db->limit($limit);
        $this->db->where('status', 'published');
        $this->db->order_by('pubdate', 'desc');
        $query = $this->db->get('posts');

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

        $query->free_result();
        return $data;
    }

    function getCategory($id)
    {
        $data = array();
        $this->db->where('id',$id);
        $this->db->limit(1);
        $query = $this->db->get('categories');

        if($query->num_rows() > 0)
        {
            $data = $query->row_array();
        }

        $query->free_result();
        return $data;
    }

    function getAllPostsByCategory($catid)
    {
        $data = array();
        $this->db->where('category_id', $catid);
        $this->db->where('status', 'published');
        $query = $this->db->get('posts');

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

    function getPost($id)
    {
        $data = array();
        $this->db->where('id',$id);
        $this->db->limit(1);
        $query = $this->db->get('posts');

        if ($query->num_rows() > 0)
        {
            $data = $query->row_array();
        }

        $query->free_result();
        return $data;
    }

and in the view page i have something like this: 在视图页面中,我有类似以下内容:

if ( count($posts) )
    {
        foreach ($posts as $key => $list)
        {
        echo '<h2>'.$list['title'].'</h2>';
        echo auto_typography(word_limiter($list['body'], 200));
        echo anchor('post/'.$list['id'],'read more >>');
        }

        echo '<br/><br/>';
    }

I'm getting the post id in the url but.. i don't know why the page is not found. 我在url中获取帖子ID,但是..我不知道为什么找不到该页面。

You have to add the controller name to the anchor uri segments. 您必须将控制器名称添加到锚点uri段中。

echo anchor('CONTROLLER/post/'.$list['id'],'read more >>');

More on this topic in the CodeIgniter URLs documentation . 有关更多信息,请参见CodeIgniter URL文档

If you want a URL like http://example.com/post/123 then you have to add the following to your application/config/routes.php file: 如果您想要类似http://example.com/post/123的URL,则必须将以下内容添加到application/config/routes.php文件中:

$route['post/(:num)'] = "CONTROLLER/post/$1";

More on routing is also available in the documentation . 文档中还提供了有关路由的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM