Grouping messages that have similarities

Ven

Joined
Jun 28, 2023
Messages
1
Reaction score
0
I have a webpage for messaging purposes. The SQL table for the webpage looks something like this:

msg_idmsgmsg_sendermsg_date
1Lorem ipsum dolor sit ametUser 12023-06-28
2consectetur adipiscing elitUser 22023-06-29
3sed do eiusmod tempor incididuntUser 12023-06-29
4ut labore et dolore magna aliquaUser 12023-06-29

So far, the output on my webpage is:
User 1 (2023-06-28)
Lorem ipsum dolor sit amet
User 2 (2023-06-29)
consectetur adipiscing elit
User 1 (2023-06-29)
sed do euismod tempor incididunt
User 1 (2023-06-29)
ut labore et dolore magna aliqua.

Meanwhile I want the output to be:
2023-06-28
User 1

Lorem ipsum dolor sit amet
2023-06-29
User 2

consectetur adipiscing elit
User 1
sed do euismod tempor incididunt
ut labore et dolore magna aliqua.

Basically if the message is sent on the same date I don't want to display the date twice. And if the message is sent by the same user I don't want to display the user's username twice (or thrice, or any else). How can I possibly achieve this? I'm still figuring my way around PHP and mySQL and I'm stuck right now. A help might be very lovely, to be honest @_@

Thank you very much in advance!

Edit: the PHP code looks something like this just in case. ultimately basic stuff tho lol
PHP:
<?php
include "connect.php";
$data = mysqli_query($connect, "SELECT * from `table1`");
$rows = mysqli_fetch_all($data, MYSQLI_ASSOC);
foreach ($rows as $row) {
if($row["msg_sender"] == "User 1") {
echo "<div class='msg'><div class='msgcontainer'>" . $row["msg"] . "</div><div class='msgdate'>" . $row["msg_date"] . "</div></div>";
}elseif($row["msg_sender"] == "User 2") {
echo "<div class='msg'><div class='msgcontainer'>" . $row["msg"] . "</div><div class='msgdate'>" . $row["msg_date"] . "</div></div>";
}
}
?>
 
Last edited:
Joined
Sep 4, 2022
Messages
128
Reaction score
16
you have to store in 2 different vars : both the date and the user.
then the code structure will test if the date is the same , then if the user is the same, in a first time.
after the test, you can fire the 'echo' for the date parts , and for the user part.
 
Joined
Jul 4, 2023
Messages
359
Reaction score
41
Why not to use sql to do this job for you. :cool:

Try sql like this (just add GROUP BY):

SQL:
SELECT * FROM `table1` GROUP BY `msg_date` DESC, `msg_sender` DESC, `msg` DESC


and in php ...

PHP:
<?php

include "connect.php";
$data = mysqli_query($connect, "SELECT * FROM `table1` GROUP BY `msg_date` DESC, `msg_sender` DESC, `msg` DESC");
$rows = mysqli_fetch_all($data, MYSQLI_ASSOC);

$group_to_date = null;
$group_for_sender = null;

foreach ($rows as $row) {
    if ($group_to_date != $row['msg_date']) {
        echo "<div class='msg_date'>" . $row['msg_date'] . "</div>";
        $group_to_date = $row['msg_date'];
        $group_for_sender = null;
    }

    if ($group_for_sender !=  $row['msg_sender']) {
        echo "<div class='msg_sender'>" . $row['msg_sender'] . "</div>";
        $group_for_sender = $row['msg_sender'];
    }

    echo "<div class='msg'>" . $row['msg'] . "</div>";
}
?>


For:
SQL:
GROUP BY `msg_date` DESC, `msg_sender` DESC, `msg` DESC

Bez tytułu.png


For:
SQL:
GROUP BY `msg_date` ASC, `msg_sender` DESC, `msg` DESC

Bez tytułu 2.png
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,757
Messages
2,569,543
Members
45,026
Latest member
camilin05

Latest Threads

Top