Jump to content

Can someone explain why this does not work


Go to solution Solved by affordit,

Recommended Posts

I am trying to pull results from a messages table and group the messages by a thread_id I have 3 messages with the same thread_id, but when I get the results it comes out like this;

 

SUBJECT 1 - MESSAGE 1

SUBJECT 1 - MESSAGE 1

SUBJECT 1 - MESSAGE 1

SUBJECT 1 - MESSAGE 2

SUBJECT 1 - MESSAGE 2

SUBJECT 1 - MESSAGE 2

SUBJECT 1 - MESSAGE 3

SUBJECT 1 - MESSAGE 3

SUBJECT 1 - MESSAGE 3

SUBJECT 2 - MESSAGE 1

 

can someone tell me where I screwed up?

Here is what I have,

function getMessages($user_id){
$user_id = $_SESSION['user_id'];

            $query = mysql_query("SELECT * FROM `messages` WHERE `recipient_id` = $user_id or `sender_id` = $user_id");
            
                while ($row = mysql_fetch_array($query)){
                    $thread_id = $row['thread_id'];
                    $result = mysql_query("SELECT COUNT(message_id) FROM `messages` WHERE `thread_id` = $thread_id");
                        $count = mysql_result($result,0);
                        
                        $i = 0;
                        while($i != $count){
                    $message_id = $row['message_id'];
                    $subject = $row['subject'];
                    $message = $row['message'];
                    $sender_id = $row['sender_id'];
                
                print "<p style = 'padding-left:10px; font-weight:bold;'>$message</p>";
                $i++;
                }
                print "<p style = 'padding-left:10px; font-weight:bold;'><hr></p>";
                }
                
}


$m = getMessages($user_id);
print $m;

Thanks everyone

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.