我会尽可能清楚地解释这一点:
这是我的Hobby Controller功能,它可以获取有关帖子的所有数据,并在group_homepage视图中显示这些数据。
public function group_homepage($hobby_id){
$this->load->model('model_posts');
$data['posts'] = $this->model_posts->get_all_posts($hobby_id);
$this->load->view('group_homepage',$data);
}
现在的问题是,对于我在视图中显示的每个帖子,我还想显示用户的个人资料图片和名称(在我的数据库的USER表中找到)以及与之关联的所有评论那个特定的帖子(在我的数据库的COMMENT表中找到)。
我的group_homepage看起来像这样:
<ul id='news_feed'>
<?php
foreach($posts as $post){
echo "<li>";
echo "<div class='left_post'>";
echo "<a href='".base_url()."/index.php/login/open_profile/".$user_id."'>";
echo"<img src='".base_url()."/uploads/".**USER'S PHOTO**."' alt='profile picture'>";
echo "</a>";
echo "</div>";
echo "<div class='right_post'>";
echo $post->message_text;
echo " <p class='post_details'>written by <span>";
echo **USER'S NAME**;
echo "</span>, ";
$db = $post->timestamp;
$timestamp = strtotime($db);
echo date("d-m-Y, G:i", $timestamp);
**DISPLAY ALL POST'S COMMENTS**
echo "</p></div></li>";
}
?>
</ul>
您是否有任何想法如何根据每篇文章检索此信息并将其与我已有的有关帖子的其他信息一起显示?
模型功能非常简单:
public function get_all_posts($hobby_id){
$this->db->where('group_id',$hobby_id);
$query = $this->db->get('post');
return $query->result();
}
我还应该提一下,我的POST表有以下字段:post_id,user_id,timestamp,message_text,group_id和like
答案 0 :(得分:0)
开始之前,请在功能
中查看本用户指南codeigniter Active Record Classpublic function get_all_posts($hobby_id){
$this->db->where('group_id',$hobby_id);
$this->db->join('users', 'users.id = post.user_id'); //join with users table
$this->db->join('comments', 'comments.user_id = post.user_id'); //join with comments table
$query = $this->db->get('post');
return $query->result();
}
现在您将获得所有数据,包括用户的个人资料和评论,根据您的要求进行循环和显示。例如,将名称和图片打印到视图
echo $post->name;
echo $post->picture; //you have to use image tag here to display user's picture.