检查结果笨重复数据(Check duplicate data in CodeIgniter resu

2019-10-29 23:45发布

我有一个小论坛,并希望显示最后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();
}

我的结果

Answer 1:

设法得到什么,我认为你正在努力实现通过使用一组由即

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

有关子查询看这个参考前面的问题和这个职位



Answer 2:

谢谢杰克,

我改变

$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();

和it's工作。



文章来源: Check duplicate data in CodeIgniter result
标签: codeigniter