我正在做一个codeigniter项目,我是一个新的代码点火器,但现在在某种程度上理解它。我为uni构建了一个简单的应用程序,我试图显示哪些项目属于哪些类别。
我的数据库结构
|------------------| |--------------------| |---------------------------------|
| books | | book_categories | | book_linktable |
|------------------| |--------------------| |---------------------------------|
| book_id | | book_category_id | | book_linktable_book_id |
| book_name | | book_category_name | | book_linktable_book_category_id |
| book_description | |--------------------| |---------------------------------|
| book_grade |
| book_author |
| book_edition |
| book_publisher |
| book_price |
| book_image |
|------------------|
好的,所以在我的模型中使用连接我已成功从book_linktable中提取了我想要的数据,并且在我的视图中使用简单的foreach循环显示了它。
麻烦的是,如果一本书属于2个或更多类别,它在book_linktable中看起来像这样:
|------------------------|---------------------------------|
| book_linktable_book_id | book_linktable_book_category_id |
|------------------------|---------------------------------|
| 1 | 1 |
| 1 | 2 |
|------------------------|---------------------------------|
(所以这里的书1属于类别1和2)
然而,问题就出现了,当我在视图中执行foreach循环时,它会将它们显示为两个不同的书籍,如下所示:
|Name | Grade | Price | Categories |
|---------------------------------------|
|Book1 | Grade 1 | £7.00 | Announcement |
|Book1 | Grade 1 | £7.00 | Event |
我只需要了解如何回应书籍所放置的不同类别,而不是多次显示的书籍。像这样:
|Name | Grade | Price | Categories |
|----------------------------------------------|
|Book1 | Grade 1 | £7.00 | Announcement, Event |
这是我的模型功能:
function get_books(){
$this->db->select('books.book_id, books.book_name, books.book_grade, books.book_edition, books.book_price, book_categories.book_category_id, book_categories.book_category_name');
$this->db->from('book_linktable');
$this->db->join('books', 'book_linktable.book_linktable_book_id = books.book_id');
$this->db->join('book_categories', 'book_linktable.book_linktable_book_category_id = book_categories.book_category_id');
$query = $this->db->get();
return $query->result();
} //function get_books()
这是我的控制器功能:
function books($condition = FALSE){
if($condition === TRUE OR $this->admin_model->logged_in() === TRUE)
{
$data = $this->admin_model->display();
$data['query'] = $this->admin_model->get_books();
$data['page_title'] = 'Books';
$this->load->view('admin_section/books/main', $data);
}
else
{
$this->load->view('admin_section/details');
}
} //function books()
以下是我在视图中的预告:
<?php
foreach($query as $row) {
echo "<tr>";
echo "<td>" . $row->book_name . "</td>";
echo "<td>" . $row->book_grade . "</td>";
echo "<td>" . $row->book_edition . "</td>";
echo "<td>" . "£" . $row->book_price . "</td>";
echo "<td>" . $row->book_category_name . "</td>";
echo "<td>" . anchor('books_admin/edit_book/' . $row->book_id, img(array('src'=>'/sys_icons/edit.png','border'=>'0','alt'=>'Edit'))) . " ";
echo "<td>" . anchor('books_admin/delete_book/' . $row->book_id, img(array('src'=>'/sys_icons/delete.png','border'=>'0','alt'=>'Delete', 'class' => 'delete_book'))) . "</td>";
echo "</tr>";
}
?>
非常感谢任何帮助
谢谢, 汤姆
答案 0 :(得分:0)
function getMyRequirements()
{
$query = "
select
books.book_id,
books.book_name,
books.book_grade,
books.book_edition,
books.book_price,
group_concat(book_categories.book_category_id) as book_category_id,
book_categories.book_category_name
from book_linktable
left join books
on book_linktable.book_linktable_book_id = books.book_id
left join book_categories
on book_linktable.book_linktable_book_category_id = book_categories.book_category_id
group by books.book_id
";
$query = $this->db->query($query);
return $query->result() ;
}
使用此查询。组concat会给你逗号分隔字段,你可以在php端爆炸。
在控制器中
function getWhatiNeed(){
$this->load->model('test');// The model where the getMyRequirements function is
$result = $this->test->getMyRequirements();
$data['result'] = $result;
$this->load->view('my_view',$data);
}
现在在视图中循环结果
if(count($result)>0)
foreach($results as $row){
$categories = explode (',',$row->book_category_id);
foreach($categories as $category){
echo $category;
}
}