i have a chat website and i want the user to have a list available of users sorted by who they last chatted with(like whatsapp). how do i do this? i tried many stack overflow answers but none of them worked for me so far. when using the code i use now the names of the users repeat for every message that exists. this query isn't working: "SELECT * FROM dms WHERE sentTo = ".$_SESSION['id']." or sentBy = ".$_SESSION['id'].";" this is what my database looks like:
this is my code:
<?php
$sql = "SELECT * FROM dms WHERE sentTo = ".$_SESSION['id']." or sentBy = ".$_SESSION['id'].";";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$sql2 = "SELECT id, username FROM users WHERE id = ".$row['sentTo'].";";
$result2 = mysqli_query($conn, $sql2);
if (mysqli_num_rows($result2) > 0) {
while ($row2 = mysqli_fetch_assoc($result2)) {
echo "<a href='dms.php?talkingTo=".$row2['id']."'>".$row2['username']."</a>";
}
}else{
echo "<p>It's empty</p>";
}
}
}else{
echo "<p>It's empty</p>";
}
?>
By using this query:
"SELECT * FROM dms WHERE sentTo = ".$_SESSION['id']." or sentBy = ".$_SESSION['id'].";
Correct me if I am wrong.
What you need to do is group the chats based on the similarity of the combination of thesentBy
andsentTo
columns. This can never be similar if you want to skip duplicates. In addition, you want to order the chats based on thedate
column in descending.
From an accepted answer to a mostly similar question I got "one method of doing what you want that uses a correlated subquery, to find the minimum created date/time for a matching conversation".
It also sorts the results based on the date.
So making these corrections and customizing the query from the older question to your context - the above query you used should be changed to(Replacewhere 1
by concatenating the id from session: $_SESSION['id']):
SELECT m.*
FROM dms m
WHERE 1 in (sentBy, sentTo) AND
m.date = (SELECT MAX(m2.date)
FROM dms m2
WHERE (m2.sentBy = m.sentBy AND m2.sentTo = m.sentTo) OR
(m2.sentBy = m.sentTo AND m2.sentTo = m.sentBy)
)
ORDER BY m.date DESC
I run this query on an identical dataset of your dms table and got 2 unique results.
One additional thing you may need to check is the link you want to generate by using the anchor:
echo "<a href='dms.php?talkingTo=".$row2['id']."'>".$row2['username']."</a>";
This is echoing out theid
of the current user. I think you were supposed to echo out theid
of the who they are chatting to? If so you need to run the query to get who it is sent from instead.
Hope this helps.
We need to group the dms records based on who the chat is with, knowing that I (id 1) am either the sender or recipient.
If it is sentTo 1 (Me) and sentBy 2 (You) then it is sentBy we are interested in. Conversely, if it is sentTo 2 (You) and sentBy 1 (Me) then it is sentTo we are interested in.
Putting this into SQL we have -
IF(sentTo = 1, sentBy, sentTo)
We can now use this in our query of the dms table to group by who we are interacting with and use MAX to find the most recent interaction -
SELECT IF(sentTo = 1, sentBy, sentTo) chatWith, MAX(date) mostRecent
FROM dms
WHERE sentTo = 1 OR sentBy = 1
GROUP BY chatWith
In your code you are running an additional query per row returned by the first query. This is not necessary as we can join to the users table -
SELECT u.id, u.username, t.mostRecent
FROM (
SELECT IF(sentTo = 1, sentBy, sentTo) chatWith, MAX(date) mostRecent
FROM dms
WHERE sentTo = 1 OR sentBy = 1
GROUP BY chatWith
) t
JOIN users u ON t.chatWith = u.id
ORDER BY t.mostRecent DESC
Putting all of this into your code we end up with -
<?php
$sql = "
SELECT u.id, u.username, t.mostRecent
FROM (
SELECT IF(sentTo = {$_SESSION['id']}, sentBy, sentTo) chatWith, MAX(date) mostRecent
FROM dms
WHERE sentTo = {$_SESSION['id']} OR sentBy = {$_SESSION['id']}
GROUP BY chatWith
) t
JOIN users u ON t.chatWith = u.id
ORDER BY t.mostRecent DESC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<a href='dms.php?talkingTo={$row['id']}'>{$row['username']}</a><br>";
}
} else {
echo "<p>It's empty</p>";
}
I have tested this with 1M randomly (ish) generated rows and it returns consistently in less than 0.02s on my local dev machine.
UPDATE - some test results
EXPLAIN output for each of the three queries with a test dataset of 10k rows - users.id between 1 and 500 and date between 2021-01-01 and current.
/* groupwise max */
EXPLAIN
SELECT u.id, u.username, t.mostRecent
FROM (
SELECT IF(sentTo = 1, sentBy, sentTo) chatWith, MAX(date) mostRecent
FROM dms
WHERE sentTo = 1 OR sentBy = 1
GROUP BY chatWith
) t
JOIN users u ON t.chatWith = u.id
ORDER BY t.mostRecent DESC;
# Serverside execution time: 0.001s
# Rows examined: 66
# Rows returned: 22
id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | PRIMARY | ALL | 43 | 100.00 | Using filesort | ||||||
1 | PRIMARY | u | eq_ref | PRIMARY | PRIMARY | 2 | t.chatWith | 1 | 100.00 | Using where | |
2 | DERIVED | dms | index_merge | IDX_from,IDX_to,IDX_from_to,IDX_to_from | IDX_to,IDX_from | 4,4 | 43 | 100.00 | Using union(IDX_to,IDX_from); Using where; Using temporary |
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.