I've searched and I've tried many different things but I haven't been able to come up with anything to work the way I want. What I have is a direct message system but I want to show next to the users name how many new messages they have from that person if it's more than 0. Everything I've tried just shows one value for all of the conversations. In my database I have a column that is if the user who received the message read it, if they had it turns the 'no' into a 'yes', otherwise it shows 'no'.
Thanks in advance for your help!
EDIT:
One thing for my database...Only the first message in the conversation has both the to_id and from_id after that all the other messages in the conversation only has the from_id. So what I'm trying to do is count how many where the from_id is not your user_id, so the non-signed in user user_id. If that makes sense.
Here is my code accessing the database:
// Get the conversations
$get_conversations = "SELECT dm.convo_id, dm.message_id, dm.to_id, dm.from_id, dm.user2read, u.name, u.user_id" .
" FROM users u" .
" JOIN direct_messages dm" .
" ON dm.from_id = u.user_id" .
" OR dm.to_id = u.user_id" .
" WHERE u.user_id = " . $_SESSION['user_id'];
" GROUP BY dm.convo_id" .
" LIMIT 1";
// Run the conversation query
$convo_result = mysql_query($get_conversations);
And then some HTML code And more php in the body tags
<?php
while ($teg = mysql_fetch_array($convo_result)) {
if($teg) {
$to_id2 = $teg['to_id'];
$from_id2 = $teg['from_id'];
if($from_id2 == $_SESSION['user_id']) {
$id_of_other_person = $to_id2;
} else if($from_id2 != $_SESSION['user_id']) {
$id_of_other_person = $from_id2;
}
// Get the name of the other person
$get_other_name = "SELECT name FROM users WHERE user_id = " . $id_of_other_person;
// Run the query on the other person's name
$query_name = mysql_query($get_other_name);
if($query_name) {
$yelp = mysql_fetch_array($query_name);
$name_of_other_person = $yelp['name'];
}
$get_user2read = "SELECT dm.convo_id, dm.message_id, dm.to_id, dm.from_id, dm.user2read, u.name, u.user_id" .
" FROM users u" .
" JOIN direct_messages dm" .
" ON dm.from_id = u.user_id" .
" OR dm.to_id = u.user_id" .
" WHERE u.user_id = " . $_SESSION['user_id'];
" AND user2read = 'no'" .
" GROUP BY dm.convo_id" .
" LIMIT 1";
$query_user2read = mysql_query($get_user2read);
$alg = mysql_fetch_array($query_user2read);
?>
<?php if(COUNT($alg['user2read']) > 0 && $alg['user2read'] == 'no') : ?>
<h3>You have <?php echo COUNT($teg['user2read']); ?> new messages - from <a href="#"><?php echo $name_of_other_person; ?></a></h3>
<?php else : ?>
<?php endif; ?>
<?php
}
}
?>
I would post in image of my direct_messages table but I don't have enough reputation... Here is a diagram of my table:
Field Type Collation Attributes Null Default Extra Action
convo_id bigint(20) No
message_id int(11) No
to_id int(11) No
from_id int(11) No
message varchar(5000) latin1_swedish_ci No
timestamp timestamp No CURRENT_TIMESTAMP
user1read varchar(3) latin1_swedish_ci No
user2read varchar(3) latin1_swedish_ci No
EDITED: I erased the backslashes
session_start();anywhere. You are using it, right? ;-) If not, then you must include it inside all your pages.($alg\['user2read'\])the way you're placing the backslashes, that doesn't seem right to me. If you're trying to escape the quotes, you need to do\'LIMIT 1, but then you have awhileloop over the results? Don't you want to return all the user's conversations?LIMIT 1is not in awhileloop. I have the firstSELECTstatement to get the conversations and then I was using the second one to get the number of new messages whereuser2read = 'no'