0

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

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.

My View:

<?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; ?>

My Controller

public function index() {
    $data['forum_reply'] = $this->db->get('forum_reply')->result();
}

My result enter image description here

Triple77
  • 23
  • 4

2 Answers2

0

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

Jake Doran
  • 86
  • 6
0

Thank you Jake,

I was change

$data['forum_reply'] = $this->db->get('forum_reply')->result();

to

$data['forum_reply'] = $this->db->group_by('forum_post_id')->order_by('timestamp', 'DESC')->get('forum_reply')->result();

and it´s working.

Triple77
  • 23
  • 4