我是Codeigniter的新手,开始学习编写简单的博客 我有3张桌子
Table 1 'news'
---------------------------------
id | title | slug | text | image |
---------------------------------
Table 2 'categories'
----------
id | name |
----------
Table 3 'news_cat'
---------------------------
id | news_id | category_id |
---------------------------
in this table `id` is autoincrement
我希望看到这些类别有哪些帖子来自' news'在我的视图页面
查看档案
<?php foreach ($news as $news_item): ?>
<?php echo $news_item['title'] ?>
<p><?=$news_item['text'] ?>
<? foreach($news_item['cat_data'] as $cat_data):?>
The category is:<?=$cat_data ?>
<?php endforeach;?>
<?php if(!empty($news_item['image'])) { ?>
<img src ="/uploads/<?=$news_item['image'];?>">
<?php } ?>
<a href="news/<?=$news_item['slug'] ?>">View article</a>
Controller
public function index()
{
$data['news'] = $this->news_model->get_news();
foreach ($data['news'] as $key => $value) :
$id = $data['news'][$key]['id'];
$data['news'][$key]['cat_data'] = $this->categories_model->get_by_id($id);
// IN THIS I HAVE QUESTION, how insert in my array several values by method get_by_id() and what must have this method
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
模型 公共函数get_by_id($ id) {
in this model i think i need use JOIN, but how i don`t know
}
答案 0 :(得分:0)
希望以下代码示例可以帮助您..如果我理解您的问题,这将为您提供类别名称和ID值列表
$this->db->select('categories.name,categories.id');
$this->db->from('categories');
$this->db->join('news_cat',"news_cat.category_id= categories.id");
$query = $this->db->get();
$data['category_list']=$query->result_array();