2013-02-13 188 views
0

大家好我正在尝试创建一个博客。第一个主页是确定..我收到从数据库中油粕描述和类别,但...我有链接的问题:CodeIgniter查看博客文章页面

这是我的控制器功能:

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

这是我的模型功能:

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; 
    } 

,并在视图页面我有这样的事情:

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/>'; 
    } 

我得到的邮件ID在URL中,但..我不知道为什么页面没有找到。

回答

3

您必须将控制器名称添加到锚点uri段。

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

CodeIgniter URLs documentation中关于此主题的更多内容。

如果你想要像http://example.com/post/123一个URL,那么你必须添加以下到您的application/config/routes.php文件:

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

更多关于路由也可在documentation

+0

我认为这是我的帖子...所以我做到了: - ??或不.. – emcee22 2013-02-13 19:09:36

+0

号码是您的功能。您的控制器名称是“post”函数所在类的名称。 – 2013-02-13 19:10:18

+0

非常感谢!帮助我很多。 @Marvin Rabe – emcee22 2013-02-13 19:17:51