Jump to content

Combining 3 databases to display info?


AidanPT

Recommended Posts

Hey, I'm pretty new to this site so I thought I would try it out! You never know!... I am currently building a private messaging system on my website, it's a lot of code so I will try to cut it down, if you require anymore code to help me out, please let me know and I will post it. The problem I am having, in your "inbox" the messages you have sent or received are displayed with the title of (SUBJECT). When you click the subject, you can see all the messages you or the other user have sent to each other. With first and last names. The 2 codes below gather this information from the databases, which is absolutely fine. Although, instead of showing the (SUBJECT) on the inbox page, i would prefer to show the (FIRST NAME + LAST NAME) of who the message is to or from. Just like you can see in the actual message itself. I cannot figure out how to do this. The second code below does this for the view message page, although the top code gathers information for the inbox page, I have tried my best to combine the users info from the 2nd part to the 1st part, but all this does is show every message as the logged in users first and last names, not who the message is sent to or received from, is there a way to do this? Here are the 2 codes:
 

<?php
function fetch_conversation_summery(){
    $sql = "SELECT
                `conversations`.`conversation_id`,
                `conversations`.`conversation_subject`,
                MAX(`conversations_messages`.`message_date`) AS `conversation_last_reply`,
                MAX(`conversations_messages`.`message_date`) > `conversations_members`.`conversation_last_view` AS `conversation_unread`
            FROM `conversations`
            LEFT JOIN `conversations_messages` ON `conversations`.`conversation_id` = `conversations_messages`.`conversation_id`
            INNER JOIN `conversations_members` ON `conversations`.`conversation_id` = `conversations_members`.`conversation_id`
            WHERE `conversations_members`.`userid` = {$_SESSION['userid']}
            AND `conversations_members`.`conversation_deleted` = 0
            GROUP BY `conversations`.`conversation_id`
            ORDER BY `conversation_last_reply` DESC";
            
    $result = mysql_query($sql);
    
    $conversations = array();
    
    while (($row = mysql_fetch_assoc($result)) !== false){
        $conversations[] = array(
            'id'               => $row['conversation_id'],
            'subject'          => $row['conversation_subject'],
            'last_reply'       => $row['conversation_last_reply'],    
            'name'       => $row['first_name'],            
            'unread_messages'  => ($row['conversation_unread'] == 1),

        );
    }
    
    return $conversations;
}

function fetch_conversation_messages($conversation_id){
    $conversation_id = (int)$conversation_id;
    
    $sql = "SELECT
                  `conversations_messages`.`message_date`,
                  `conversations_messages`.`message_date` > `conversations_members`.`conversation_last_view` AS `message_unread`,
                  `conversations_messages`.`message_text`,
                  `users`.`first_name`,
                  `users`.`last_name`
            FROM `conversations_messages`
            INNER JOIN `users` ON `conversations_messages`.`userid` = `users`.`userid`
            INNER JOIN `conversations_members` ON `conversations_messages`.`conversation_id` = `conversations_members`.`conversation_id`
            WHERE `conversations_messages`.`conversation_id` = {$conversation_id}
            AND `conversations_members`.`userid` = {$_SESSION['userid']}
            ORDER BY `conversations_messages`.`message_date` DESC";
            
    $result = mysql_query($sql);
    
    $messages = array();
    
    while (($row = mysql_fetch_assoc($result)) !== false){
        $messages[] = array(
                'date'     => $row['message_date'],
                'unread'   => $row['message_unread'],
                'text'     => $row['message_text'],
                'first_name' => $row['first_name'],
                'last_name' => $row['last_name'],
        );
    }
    
    return $messages;
}


I need to be able to recall the information as "$coversation['first_name']; $conversation['last_name']" to show the names, although this information is only available on the messages page as "$messages['first_name'];. I am currently displaying the subject as "$conversation['subject'];". Trying to turn subject into the first and last names. Files have been attached for reference. Thanks

pmsettings.php

inbox.php

viewmessage.php

Link to comment
Share on other sites

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.