Previous | Next

     We can connect to the database, see the list of messages we have, and delete the messages we select. You will have noticed that on page two when we made the inbox page, each title of the private message was its own link to this page with "?msg_id=" and then the id number of the private message stuck on the end. This is how we are sending the variable $msg_id through the URL. We can on the view message page request the variable msg_id and, PHP will put the number it sees in the url into a variable we can actually use in the code. Once we have the message id we need to find out who sent it, who is supposed to be the reciever, the body of the message, etc. We also need to do a check to see if someone may have tried changing the number in the URL to get into someone else's message, and if not display the body of the message. I do not have a reply button, that may be something I will add to this in the future. At least this is a good start for a private messaging system.


    <?php
session_start
();
$user $_SESSION['username'];
    
    include 
'db.php';
    
    
//This checks to see if a user is logged in or not by seeing if the sessioned username varialble exists.
    //You could change this check to however you want to validate your members, this is just how I did it.
    
if(!$user)
        {
        echo 
"<br><p>Blah blah you arent logged in and stuff, you should do that or something</p><br>";
        }
        
    else
        {
        
//We need to grab the msg_id variable from the URL.
        
$msg_id $_REQUEST['msg_id'];
        
        
//Get all of the information about the message with the id number sent through the URL
        
$view_msg mysql_query("SELECT * FROM messages WHERE id = '$msg_id'");
        
$msg mysql_fetch_array($view_msg);
        
        
$reciever $msg['reciever'];
        
$sender $msg['sender'];
        
$subject $msg['subject'];
        
$message $msg['message'];
        
        
//If the person who is supposed to recieve the message is the currently logged in user everything is good
        
if($reciever == $user)
            {
            
//The message was recieved, so lets update the message in the database so it wont show up in the sent page any more
            
mysql_query("UPDATE messages SET recieved='1' WHERE id = '$msg_id'");
            
            
//Query the database to see how many messages the logged in user has, then do a little math
            //Find the percentage that your inbox is full (message count divided by 50)
            //50 messages maximum, you can change that
            
$sql mysql_query ("SELECT pm_count FROM users WHERE username='$user'");
            
$row mysql_fetch_array ($sql);
            
$pm_count $row['pm_count'];
            
            
//This is the math to figure out the percentage.
            //The message could divided by 50 then multiplied by 100 so we dont have a number less than 1
            
$percent $pm_count/'50';
            
$percent $percent '100';
            
            
//Now we will display the little navigation thing, the fullness of the inbox, then display message information stuff, like who its from, the subject, and the body
            
?>
            <br>
            <center>
            <b><p><a href="inbox.php">Inbox</a> | <a href="compose.php">Compose</a> | <a href="sent.php">Sentbox</a></b>
            <b><p><?php echo "$pm_count"." of 50 Total  |  "."$percent"."% full"?></p></b>
            </center>
            <br>
            
            <table width="80%">
              <tr>
                <td width="120px"><p>From:</p></td>
                <td width=""><p><a href = "<?php echo "../user/profile.php?user_name=$sender"?>"><?php echo $sender?></a></p></td>
              </tr>
              
              <tr>
                <td width="120px"><p>Subject:</p></td>
                <td width=""><p><?php echo $subject?></p></td>
              </tr>
              
              <tr>    
                <td width="120px"><p>Message Body:</p></td>
                <td width=""><p><?php echo $message?></p></td>
              </tr>
            </table>
            </center>
            <?php
            
}
        
//Everything is not good, someone tried to look at somone else's private message
        
else
            {
            
?>
            <p>It appears you are trying to view someone else's private message. Please view your own private messages, or go away.</p>
            <?php
            
}
        }
    
?>
    


     One last thing real quick, if you encounter any problems during this tutorial or with code not working, PLEASE e-mail me at "bedfordd @ egmods.com" (Remove the spaces).

Part One - Getting Started: The first page will tell you how to set up the database and table that you will need for the Private Messaging System, and will also show you how to connect to the database you just created.

Part Two - Inbox: The second page will teach you first of all, how to make the inbox. This page will search for any message that have been sent to the logged in user, display them, let the user click a link to view them, and finally to select them for deletion.

Part Three - Delete: The third page will recieve the information from the inbox page for which private messages need to be deleted, process the information, and to actually do the deletion.

Part Four - View Messages: The fourth page will teach you how to recieve the id variable sent through the URL when you click the link on the inbox to view the message. It will double check that you are the reciever of the message, and will display the message in full.

Part Five - Compose: The fifth page will teach you how to make the form to write a message and to send the form to itself, do some error checking to make sure they didnt try to send a blank message, or a message to a non existant user, or a message to themself.

Part Six - Sent Messages: The sixth, and least useful page, will just show you which messages you have sent to other users that haven't been read or deleted yet.