简体   繁体   中英

PHP/MySQL different value for each row

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

This is not correct:

<h3>You have <?php echo COUNT($teg['user2read']); ?> new messages - from <a href="#"><?php echo $name_of_other_person; ?></a></h3>

count() is used for arrays, but the elements of $teg are just the column values from the table. If you want to count records grouped by a column, you need to use the MySQL COUNT() function in your query. Or in this case, since you only want to count messages that fit a specific criteria, use SUM() :

$get_conversations = "SELECT dm.convo_id, dm.message_id, dm.to_id, dm.from_id, u.name, u.user_id, SUM(dm.user2read = 'no') unread_count" .
                    " 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 dm.user2read = 'no' " .
                    " GROUP BY dm.convo_id" .
                    " LIMIT 1";

Then you can use $teg['unread_count'] to get the count of unread messages in that conversation.

I don't think you need the inner query $get_user2read . All the information is in $get_conversations now. It was wrong, because it wasn't getting the row specific to the conversation in that iteration of the loop.

This $yelp = mysql_fetch_array($query_name); $name_of_other_person = $yelp\\['name'\\]; $yelp = mysql_fetch_array($query_name); $name_of_other_person = $yelp\\['name'\\]; looks wrong.

You need to loop the $yelp like you did $teg.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM