Previous | Next

     Now that we have the messages table created and can connect to our database we just made, we need an inbox page that will search for messages for the logged in user, and then display them, and let the user click them. The user should also be allowed to check messages that they want deleted and then press a button which will send the information to a delete page to be processed. The code I use to do that is below, and I have tried to comment it pretty well to explain pretty much what it is all doing.


    <?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
        {
        
//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';
        
        
//Next we come out of PHP and show some HTML, the inbox fullness, links to the other message system pages, the messages, etc.
        
?>
        <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>
        <?php
        
//This stuff and the while loop will query the database, see if you have messages or not, and display them if you do
        
$query "SELECT id, sender, subject, message FROM messages WHERE reciever='$user'";
        
$sqlinbox mysql_query($query);
        
        
//We have a mysql error, we should probably let somone know about the error, so we should print the error
        
if(!$sqlinbox)
            {
            
?>
            <p><?php print '$query: '.$query.mysql_error();?></p>
            <?php
            
}
        
        
//There are no rows found for the user that is logged in, so that either means they have no messages or something broke, lets assume them they have no messages
        
elseif (!mysql_num_rows($sqlinbox) )
            {
            
?>
            <center><p><b>You have no messages to display</b></p></center>
            <?php
            
}
        
        
//There are no errors, and they do have messages, lets query the database and get the information after we make a table to put the information into
        
else
            {
            
//Ok, Lets center this whole table Im going to make just because I like it like that
            //Then we create a table 80% the total width, with 3 columns, The subject is 75% of the whole table, the sender is 120 pixels (should be plenty) and the select checkboxes only get 25 pixels
            
?>
            <center>
            <form name="send" method="post" action="delete.php">
            <table width="80%">
            <tr>
              <td width="75%" valign="top"><p><b><u>Subject</u></b></p></td>
              <td width="120px" valign="top"><p><b><u>Sender</u></b></p></td>  
              <td width="25px" valign="top"><p><b><u>Select</u></b></p></td>
            </tr>
            <?php
            
//Since everything is good so far and we earlier did a query to get all the message information we need to display the information. 
            //This while loop goes through the array outputting all of the message information
            
while($inbox mysql_fetch_array($sqlinbox))
                {
                
//These are the variables we get from the array as it is going through the messages, we have the id of the private message, we have the person who sent the message, we have the subject of the message, and yeah thats it
                
$pm_id $inbox['id'];
                
$sender $inbox['sender'];
                
$subject $inbox['subject'];
                
                
//So lets show the subject and make that a link to the view message page, we will send the message id through the URL to the view message page so the message can be displayed
                //And also let the person see who sent it to them, if you want you can make that some sort of a link to view more stuff about the user, but Im not doing that here, I did it for my game though, similar to the viewmsg.php page but a different page, and with the senders id
                //And finally the checkboxes that are all stuck into an array and if they are selected we stick the private message id into the array
                //I will only let my users have a maximum of 50 messages, remeber that ok? Because that's the value I will later in another page
                //Here is finally the html output for the message data, the while loop keeps going untill it runs out of messages
                
?>
                <tr>
                  <td width="75%" valign="top"><p><a href="viewmsg.php?msg_id=<?php echo $pm_id?>"><?php echo $subject?></a></p></td>
                  <td width="120px" valign="top"><p><?php echo $sender?></p></td>
                  <td width="25px" valign="top"><input name="pms[]" type="checkbox" value="<?php echo $pm_id?>"></td>
                </tr>
                <?php
                
//This ends the while loop
                
}
            
//Here is a submit button for the form that sends the delete page the message ids in an array
            
?>
            <tr>  
            <td colspan="3"><input type="submit" name="Submit" value="Delete Selected"></td>
            <td></td>
            <td></td>
            </tr>
            </table>
            </center>
            <?php
            
//So this ends the else to see if it is all ok and having messages or not
            
}
        
        
//This ends that first thing that checks if you are logged in or not
        
}
    
?> 


     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.