我有一个小论坛,想显示最近的10条回复。 我的问题:
我从数据库中获得了一些重复的条目,因为一篇帖子中有一些回复。如何禁用所有重复的帖子并仅显示所有帖子中的一个。我试着说,如果有1个以上具有相同forum_post_id的回复>仅显示最后一个。
我的观点:
<?php foreach ($forum_reply as $item): ?>
<?php $standing = $item->forum_post_id ?>
<div class="col-lg-12 robie" onClick="location.href='<?= site_url('viewtopic/index/' . $item->forum_post_id) ?>'">
<div class="catcenter55 row">
<div class="col-md-3 deta grew">
<?php echo $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->post_head; ?>
</div>
<div class="col-md-2 deta">
<?php $topcat = $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->forum_cat_id ;?>
<?php echo $this->db->where('forum_cats2_id', $topcat)->get('forum_cats2')->row()->forum_name; ?>
</div>
<div class="col-md-2 deta">
<?php echo $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->views; ?>
</div>
<div class="col-md-2 deta">
<div class="likebg2">+<?php echo $this->db->where('forum_post_id', $item->forum_post_id)->get('forum_post')->row()->likes; ?></div>
</div>
<div class="col-md-3 deta">
<?php foreach ($this->db->order_by('timestamp', 'DESC')->get('forum_reply')->result() as $stan): ?>
<?php if($standing == $stan->forum_post_id): ?>
<?php echo $this->db->where('user_id', $stan->user_id)->get('users')->row()->username; ?> | <?php echo $this->db->order_by('timestamp', 'DESC')->where('forum_reply_id', $stan->forum_reply_id)->get('forum_reply')->row()->timestamp; ?>
<?php break; ?>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endforeach; ?>
我的控制器
public function index() {
$data['forum_reply'] = $this->db->get('forum_reply')->result();
}
答案 0 :(得分:0)
设法通过使用小组来获得我认为您要实现的目标。
SELECT * FROM posts GROUP BY forumId;
但是,这使订购帖子时事情变得更加复杂,这有点怪异,但请看一下:
SELECT *
FROM (
SELECT *
FROM posts
ORDER BY date DESC
LIMIT 18446744073709551615
) AS sub
GROUP BY sub.forumId
ORDER BY id DESC
有关子查询的参考,请查看此previous question和此post
答案 1 :(得分:0)
谢谢杰克,
我正在改变
$data['forum_reply'] = $this->db->get('forum_reply')->result();
到
$data['forum_reply'] = $this->db->group_by('forum_post_id')->order_by('timestamp', 'DESC')->get('forum_reply')->result();
它正在工作。