1
我想獲取新聞帖子的類別名稱。我可以很容易地得到類別ID,但我想要得到類別名稱使用類別Id或其他方法。我應該怎麼做?如何從CodeIgniter中的類別模塊獲取新聞模塊中的帖子的類別名稱HMVC
這是我怎麼顯示我的博客文章在控制器
function post($slug = FALSE)
{
if (isset($slug)) {
$data['query'] = $this->mdl_blogs->get_where_slug($slug);
$data['view_file'] = "blog_view";
$this->load->module('template');
$this->template->public_one_col($data);
}
}
在模型
function get_where_slug($slug){
$table = $this->get_table();
$this->db->where('news_slug', $slug);
$query=$this->db->get($table);
return $query;
}
in view
<?php
foreach ($query->result() as $row) {
$data['news_title'] = $row->news_title;
$news_body = $row->news_body;
$news_slug = $row->news_slug;
$category_id = $row->category_id;
//$data['category_name'] = $row->category_name;
//$news_category = $row->news_category;
?>
<h2><a href="<?php echo (base_url().'blogs/post/'.$news_slug) ;?>">
<?php echo $data['news_title'];?></a></h2>
<p><?php echo $news_body;?></p>
<p><?php echo $category_id;?></p>
<?php
}
?>
<?php
//echo Modules::run('comments');
?>
在這裏我也想顯示類別名稱。我想從類別表得到它
這裏是我的表
in News table
news_id
category_id
news_slug
......
in categories
category_id
category_name
category_slug
謝謝哈立德我這樣做,所以不成功。你拯救了我的一天,但是你誤以爲這是一個地方,並且在$ table之後有n個。$ data ['query'] = $ this-> db-> select('category_name'); \t \t $ data ['query'] = $ this-> db-> from('categories'); \t \t $ data ['query'] = $ this-> db-> join('news','categories.category_id = news.category_id'); \t \t $ data ['query'] = $ this-> db-> get(); – Robin