We can send and recieve, read, and delete messages all fine and everything, hopefully anyways. This page will let you see what messages
you have sent to other users that have not been read yet, once a message is read it gets flagged, and will not show up here anymore, but
just because you sent a message to someone and it doesn't show up on this page doesn't mean they read it, the message may have been
deleted by the reciever. On this page we just get all messages that were sent by the currently logged in user, which have not been recieved.
Then we display all the message like we did on the inbox page. This page is very short, and hopefully easily understandable.
$user = $_SESSION['username'];
//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.
echo "<br><p>Blah blah you arent logged in and stuff, you should do that or something</p><br>";
//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, the messages we have sent that werent recieved or deleted yet
<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>
//Get all message that are sent by the logged in user, and that have the recieved field set to zero, meaning they have not been recieved, once read the view message page sets the recieved field for the message to 1
$query = "SELECT * FROM messages WHERE sender='$user' AND recieved='0'";
$sqlinbox = mysql_query($query);
//If there is a MySQL error we need to display the error because something has gone horribly wrong on the server.
<p><?php print '$query: '.$query.mysql_error();?></p>
//If all messages sent by the user were recieved or deleted, then jsut say that there are no messages not recieved
elseif (!mysql_num_rows($sqlinbox) )
<p><b>You have no un-recieved messages to display</b></p>
//If there are messages that have not been recieved yet we need to display those messages to the user
//Lets make a table for the messages to go in, to keep things organised and such
<table width="80%" border="0">
<td width="" valign="top"><p><b><u>Subject</u></b></p></td>
<td width="120px" valign="top"><p><b><u>Sender</u></b></p></td>
//This is a while loop that goes through the array while there are still things in it, getting the subject and reciever and displaying them
while($inbox = mysql_fetch_array($sqlinbox))
$reciever = $inbox['reciever'];
$subject = $inbox['subject'];
<td width="" valign="top"><p><?php echo "$subject"; ?></p></td>
<td width="120px" valign="top"><p><?php echo "$reciever"; ?></p></td>
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
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.