我有2个数据库表,一个用于发布,另一个用于类别,每个帖子按类别ID分配到一个类别。
我想在帖子页面和每个帖子上显示类别名称,我想显示分配给它的类别,所以我做了一个连接数据库表,类别名称正常显示在主帖子页面上,但是当我点击了该类别现在显示的帖子,我得到了Undefined index: name
型号代码:
<?php
class Yacht_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_yachts($slug = FALSE){
if ($slug === FALSE) {
$this->db->join('categories', 'categories.cat_id = yachts.category');
$query = $this->db->get('yachts');
return $query->result_array();
}
$query = $this->db->get_where('yachts', array('slug' => $slug));
return $query->row_array();
}
public function get_yachts_by_category($cat_id){
$this->db->join('categories', 'categories.cat_id = yachts.category');
$query = $this->db->get_where('yachts', array('category' => $cat_id));
return $query->result_array();
}
}
?>
控制器代码:
<?php
class Yachts extends CI_Controller{
public function index(){
$data['title'] = 'Yachts';
$data['yachts'] = $this->yacht_model->get_yachts();
$this->load->view('templates/header');
$this->load->view('yachts/index', $data);
$this->load->view('templates/footer');
}
public function view($slug = NULL){
$data['yacht'] = $this->yacht_model->get_yachts($slug);
if(empty($data['yacht'])){
show_404();
}
$this->load->view('templates/header');
$this->load->view('yachts/view', $data);
$this->load->view('templates/footer');
}
}
?>
发布的HTML代码:
<section>
<div class="rows inner_banner inner_banner_2">
<div class="container">
<h2><span><?php echo $yacht['name']; ?></span></h2>
<ul>
<li><a href="#inner-page-title">Anasayfa</a>
</li>
<li><i class="fa fa-angle-right" aria-hidden="true"></i> </li>
<li><a href="#inner-page-title" class="bread-acti">Gulet Kiralama</a>
</li>
</ul>
</div>
</div>
</section>
$yacht['name'];
来自类别表,它应显示类别名称。
我不确定我是否清楚地解释了我的问题,如果没有,请告诉我,我可以为您提供您需要的任何信息或细节。
答案 0 :(得分:0)
1)您需要使用post_id从数据库中获取该类别名称。
或
2)您可以在会话中存储类别和帖子名称映射数组,并且可以从会话数据中显示相关帖子的类别名称。
答案 1 :(得分:0)
如果您有帖子和类别表,您可以通过使用加入或通过传递类别ID来创建返回类别名称的函数来访问它 让我告诉你们两个
1使用加入 在你的Post模式中创建这个函数
function getPosts(){
$this->db->select('post.*,post_category.*')
->from('post')
->join('post_category','post_category.id=post.post_category','left');
$query = $this->db->get();
return $query->result_array();
}
类别中的第二种方法模型写入功能
public function getCategoryById($id = Null){
$query = $this->db->get_where('post_category',array('id'=>$id));;
return $query->row()->category_title;
}
如果您使用的是第一种方法,则视图文件中的代码将类似于
<?php if(isset($posts) && count($posts) > 0){
foreach($posts as $k => $val){
?>
<tr>
<td><?php echo $k++?></td>
<td><?php echo $val['post_title']?></td>
<td><?php echo $val['post_content']?></td>
<td><?php echo $val['category_title']?></td>
<td><?php echo $val['slug']?></td>
</tr>
<?php } } ?>
如果你想使用第二种方法,你视图中的代码就像
<?php if(isset($posts) && count($posts) > 0){
foreach($posts as $k => $val){
?>
<tr>
<td><?php echo $k++?></td>
<td><?php echo $val['post_title']?></td>
<td><?php echo $val['post_content']?></td>
<td><?php echo $this->category_model->getCategoryById($val['post_category'])?></td>
<td><?php echo $val['slug']?></td>
</tr>
<?php } } ?>
我希望它能帮到你
答案 2 :(得分:0)
您可以尝试这个简单的查询。我希望这会按你的意愿运作。
dataSource
答案 3 :(得分:0)
我找到了解决方案,我在if语句中创建了默认设置为FALSE的连接表,这就是为什么在帖子页面中连接不起作用的原因,但当我将它放在if语句之外时,它的工作原理细
在:
public function get_yachts($slug = FALSE){
if ($slug === FALSE) {
$this->db->join('categories', 'categories.cat_id = yachts.category');
$query = $this->db->get('yachts');
return $query->result_array();
}
$query = $this->db->get_where('yachts', array('slug' => $slug));
return $query->row_array();
}
后:
public function get_yachts($slug = FALSE){
if ($slug === FALSE) {
$this->db->join('categories', 'categories.cat_id = yachts.category');
$query = $this->db->get('yachts');
return $query->result_array();
}
$this->db->join('categories', 'categories.cat_id = yachts.category');
$query = $this->db->get_where('yachts', array('slug' => $slug));
return $query->row_array();
}