检查CodeIgniter结果中的重复数据 [英] Check duplicate data in CodeIgniter result

查看:58
本文介绍了检查CodeIgniter结果中的重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型论坛,想显示最近的10条回复。
我的问题:

I have a small forum and want to show the last 10 reply's. My problem:

我从数据库中得到了一些重复的条目,因为在一个帖子中有一些回复。如何禁用所有重复的帖子,并且仅显示所有帖子中的一个。我试着说,如果有1个以上具有相同forum_post_id的回复>仅显示最后一个。

I get some duplicate entries from the database because there have some replies in one post. How I can disable all duplicate posts and show only one from all posts. I try to say, if more then 1 replys with the same forum_post_id > show only the last.

我的观点:

<?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();
}

我的结果

My result

推荐答案

设法通过使用group by by

Managed to get what I think you are trying to achieve by using a group by i.e.

SELECT * FROM posts GROUP BY forumId;

但是,这使订购帖子时事情变得更加复杂,这有点棘手,但请看一下

However this makes things more complicated when ordering the posts, this is a bit hacky but have a look at this:

SELECT * 
FROM (
SELECT * 
    FROM posts
    ORDER BY date DESC
    LIMIT 18446744073709551615
) AS sub
GROUP BY sub.forumId
ORDER BY id DESC

有关子查询的参考,请参阅此上一个问题和此发布

For reference regarding the sub query look at this previous question and this post

这篇关于检查CodeIgniter结果中的重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆