Files We are going to use
- View_Profile.php
Files We are going to create
- Send_Message.php
- _Send_Message.php
- Help_Desk.php
- Help_Desk_Reply.php
Send_Message.php
Apart from forum there is no other way for players to communicate with each other, and we dont way forum spammed with posts. so we need a way for users to message each other. In this part we are just going to create the send message part and in the next part we are going to create the receive mail.
Step One - Mysql tables.
For this to work well we need some where to save the message. so we are going to create a pm table that has this structure:-
- id (Which Holds the unique id of each message sent)
- Message (Contain the text of the message that was sent)
- sendto (The name of the user who the message is sent to )
- sendby (The name of the user tho sent the message)
- del (Holds a 1 or 0 to say if the message is deleted or not, if it is deleted the user who the message was sent will no longer be able to see this message, but you can. it is good idea to save these message in-case a case is reported, You may need them for further proof.)
- time (the date and time the message was sent)
this is the table. add it to your database
# # Table structure for table `pm` # CREATE TABLE pm ( id int(11) NOT NULL auto_increment, message text NOT NULL, sendto varchar(20) NOT NULL default '', sendby varchar(20) NOT NULL default '', `time` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, del varchar(1) NOT NULL default '1', PRIMARY KEY (id) ) ENGINE=MyISAM AUTO_INCREMENT=137661 DEFAULT CHARSET=latin1; # --------------------------------------------------------
We need to up date the users table with 2 row fields. These are :-
- newmail ( This is sent to 0 when a user has a new message
- message ( This counts the amount of message a user has sent)
they are
newmail char(1) NOT NULL default '1' messages varchar(9) NOT NULL default '0'
One more. This one is for the help desk we are going to create later. This field holds the users current question. that is submitted to help desk.
help_desk text NOT NULL,
You Really need to add these to these to the database otherwise the message sending will not work.
Step two - Send_Message.php
The send message html code is very simple it is just a text box to enter the users name and then a text-area to type in your message and a submit button to submit the information entered. Create a page called Send_Message.php and add the code below.
<? require("Left.php"); ?>
<html>
<head>
</head>
<body>
<?
require("_Send_Message.php");
?>
<form id="messageform" name="messageform" method="post">
<table width="90%" border="1" >
<tr>
<td colspan="2" align="left" >Send Letter: </td>
</tr>
<tr>
<td width="75" align="left" ><b>Send to: </b></td>
<td width="475" align="center" ><input name="sendto" type="text" style='width: 98%; ' value='<?php echo htmlspecialchars($_GET['name']);?>' maxlength="110" /></td>
</tr>
<tr>
<td width="75" align="left" valign="top" ><b>Message: </b></td>
<td width="475" align="center" ><textarea name="message" rows="10"></textarea></td>
</tr>
<tr>
<td colspan="2" align="right" valign="top" ><input name="Send" type="submit" value="Send." onfocus="if(this.blur)this.blur()"/></td>
</tr>
</table>
<p> </p>
<br />
</form>
</body>
</html>
<? require("Right.php"); ?>
This is the full html code you need for Send_Message.php. it is really basic to explain.
Step Three - _Send_Message.php
Because there is a lot of php for sending a mail we are going to separate it form the html so it is different. In the html above you can see the we used a require to get this page we are about to make.
Create a page called _Send_Message and add this php code
<?
$page_url = explode(".", $_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = $page_url[0].".php";
if($_SERVER['REQUEST_URI'] == "/_Send_Message.php"){
exit();
}
if(isset($_POST['Send'])){
$_POST['sendto'] = str_replace(' ', '', $_POST['sendto']);
$m_check = str_replace(' ', '', $_POST['message']);
if((empty($m_check)) or (empty($_POST['sendto']))){ // a valididation to makes sure that all the ields are filled
echo "You left one or more fields open.";
}else {
$multi_messages = explode("-", $_POST['sendto']); // Allows a user to send a mass message to up to 5 people at once. Useful when a member wants to send a message to a lot of friends
$multi_messages = array_unique($multi_messages);
if(count($multi_messages) > 5){
echo "5 is the maximum amount of people you are allowed to send to at once.";
}else{
// checks the name of every user entered in a for loop
for ($i = 0; $i < count($multi_messages); $i++) {
$query = "SELECT name FROM users WHERE name='".mysql_real_escape_string($multi_messages[$i])."'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
// makes a user doent send a message to themself
if($row['name'] == $name){
echo "<br />Its not allowed to send a message to yourselve.";
}else{
if(!empty($row['name'])){
$sql = "INSERT INTO pm SET id = '', sendto = '" .mysql_real_escape_string($row['name']). "', message = '" .mysql_real_escape_string($_POST['message']). "', sendby = '" .mysql_real_escape_string($name). "'";
$res = mysql_query($sql);
if ($res){
$send_to = "<a href=\"View_Profile.php?name=".$row['name']."\" onfocus=\"if(this.blur)this.blur()\">".$row['name']."</a>,";
echo "<br />Your message to ".$send_to." has been sent."; // convermation to say that the message to the user was sent
$result = mysql_query("UPDATE users SET newmail='0' WHERE name='" .mysql_real_escape_string($row['name']). "'")
or die(mysql_error());
$result = mysql_query("UPDATE users SET messages=messages+'1' WHERE id='" .mysql_real_escape_string($_SESSION['user_id']). "'")
or die(mysql_error());
}else{
//if something went wrong and the message could not be sent
echo "error! Your message could not be sent.";
}
}else{
// if one of the users enteren doens tplay this game
echo "<br />".$multi_messages[$i]." doesn't play This game .";
}
}
}
}
}}
?>
there is nothing new that needs explaining. The only complex this here is the for loop. The for loop check every name entered ( the right format to enter multiply names is "Denis-Anotheruser- Then another-etc". You can enter up to 5 users in that format.) then it check the database to see if all of the names entered exist or if the users entered his/her own name.
Testing this script needs two members. You need to create an extra member and send it a message, to check if it works check under ph you should see a new data entered and under the player name under the users table you should see 0 under newmail field.
On View_Profile we need to add a link there that when you click on it it takes you to the send_message page and fills out the name part with the users profile you were viewing. Open View_Profile.php and where it says Username: change this
<?php echo "$profile_name"; ?>
for this
<?php echo "<a href=\"Send_Message.php?name=". $profile_name ."\" onfocus=\"if(this.blur)this.blur()\">".$profile_name."</a>"; ?>
how this works. on the Send_Message.php page you will notice that the text box has this for it value
value='<?php echo htmlspecialchars($_GET['name']);?>'
This is a $_Get method that get the name of the user from the View_profile.php page.
if all of that is done you should get Your message to Denis, has been sent. ("Denis" replaced with the name of the user who you sent the message to.)
Thats all for sending messages for now. In the next part we are going to work on receiving and displaying the message.
Help_Desk.php
Help Desk is a small system to help users understand your game better. Remember we made a hdo system earlier, These hdos will be able to answer questions from normal users. We are going to tie in our hdo system with our send message. The way Help Desk system will work is. :-
- a player will click on Help_Desk.php and submit a question.
- A HDO will then go to Help_Desk_Reply.php and sees the question and click reply.
- It will then take the HDO to Send_Message.php with the players name in the send field and the players question in the message, and below that the hdo will answer the question and the answer will be sent to the player's inbox
Step One - Design
Create a page called Help_Desk.php and add the following html
<form method="post">
<table width="400" border="1" >
<tr>
<td align="left" >Help Desk: </td>
</tr>
<tr>
<td align="left">Please ask a question if you dont understand </td>
</tr>
<tr>
<td align="center" ><textarea name="message" rows="10" ></textarea></td>
</tr>
<tr>
<td align="right"><input name="Send" type="submit" id="Send" onfocus="if(this.blur)this.blur()" value="Send."/></td>
</tr>
</table>
<br />
Please make sure that the question you ask are based on the game
</form>
You can see i gave instruction to players before the submit their question you can do the same.
Step Two - PHP
make sure the help desk field is added under the users table on the database.
help_desk text NOT NULL,
add this php on top of the html you added in step one.
if(isset($_POST['Send']) and !empty($_POST['message'])){
if(strlen($_POST['message']) > 500){
echo "Your message may not contain more then 500 characters.";
}else{
$result = mysql_query("UPDATE users SET help_desk='".mysql_real_escape_string($_POST['message'])."' WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'")
or die(mysql_error());
echo "<b>Your help desk ticket has been send.</b><br />Please have a little bit of patient we will reply to your message as soon as possible.";
}
}
In the code above we have a simple validation to make sure that the player doesn't enter more that 500 words. We used the "strlen" function to do this. "The strlen function returns the length of the given string". So you can use this to count the amount letters in a sentence word.
The code also update the database and add the question to the help_desk field you created.
Step Three - Help_Desk_Reply.php
Create a page called and add this code. This is small code for the staff to see and answer the questions.
<? require("Left.php"); ?>
<html>
<head>
</head>
<body>
<?php if (in_array($name, $admin_array) or in_array($name, $manager_array) or in_array($name, $hdo_array)){ ?>
<? if(!empty($_GET['name'])){
// if you delete the message
$result = mysql_query("UPDATE users SET help_desk='' WHERE name='".mysql_real_escape_string($_GET['name'])."'")
or die(mysql_error());
}
?>
<form method="post">
<table width="550" border="1" align="center">
<?
$result = mysql_query("SELECT help_desk,name FROM users ORDER BY name DESC") or die(mysql_error());
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
$row['help_desk'] = nl2br(htmlspecialchars(stripslashes($row['help_desk'])));
if(!empty($row['help_desk'])){
?>
<tr>
<td colspan="2" align="left" >Help Desk: </td>
</tr>
<tr>
<td colspan="2" align="left" ><?php echo "<a href=\"View_Profile.php?name=".$row['name']."\" onfocus=\"if(this.blur)this.blur()\">".$row['name']."</a>"; ?> Wrote:</td>
</tr>
<tr>
<td colspan="2" align="left" ><?php echo $row['help_desk']; ?></td>
</tr>
<tr>
<td width="275" align="left" " ><?php echo "<a href=\"?name=".$row['name']."\" onfocus=\"if(this.blur)this.blur()\">Delete.</a>"; ?></td>
<td width="275" align="right" ><?php echo "<a href=\"Send_Message.php?name=".$row['name']."&action=helpdesk\" onfocus=\"if(this.blur)this.blur()\">Reply.</a>"; ?></td>
</tr>
<?php }} // while
// end print out results received.
?>
</table>
</form>
<?php } ?>
</body>
</html>
<? require("Right.php"); ?>
The code above check the staff array we create in Part 5 with the players name. if the players name inst in any of the arrays the page will be blank so the user wont be able to see the page because the are not staff, but if you are staff the page should be visible when a question is asked. If there is no question the page will be blank.
Step Four - Send_Message and _Send_Message.php
So far this system isnt completed we need to make sure that the staff will be able to reply to the question asked. for this we need to add a small code to send message. So open Send_Message.php and _Send_Message.php. On Send_Message.php replace the textarea on line 22 with this
<textarea name="message" rows="10"><?php
if($_GET['action'] == "helpdesk" and (in_array($name, $admin_array) or in_array($name, $manager_array) or in_array($name, $hdo_array))){
$sql = "SELECT help_desk FROM users WHERE name='".mysql_real_escape_string($_GET['name'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$help_desk = htmlspecialchars($row->help_desk);
echo "
[hr][b]Your questions was:[/b]
".stripslashes($help_desk);
}
if (!empty($_GET['reply'])){
$query = "SELECT * FROM pm WHERE id='".mysql_real_escape_string($_GET['reply'])."'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if($row['sendto'] != $name){
echo "You are not allowed to view this information.";}
else {
echo "
";// empty line at reply.
echo "[b]".htmlspecialchars($row['sendby'])." wrote:[/b]";
echo "
";
echo htmlspecialchars(stripslashes($row['message']));
}// if not your reply.
}
?></textarea>
This gets the question the user asked and fills it out in this text area then the staff can answer.
On _Send_Message.php add this on line 52 before the }else{
// helpdesk.
if($_GET['action'] == "helpdesk" and (in_array($name, $admin_array) or in_array($name, $manager_array) or in_array($name, $hdo_array))){
$result = mysql_query("UPDATE users SET help_desk='' WHERE name='".mysql_real_escape_string($_GET['name'])."'")
or die(mysql_error());
}
This reset the users help desk so it dont show in the Help_Desk_Reply.
If you see any mistakes / improvement please post below or if you have any problems.
To full test this for now get your other account to submit a question and answer it and then check if it is on the database. In the next Part we are going to look at viewing the messages sent to you and we may start a simple CSS design to start giving our game some life, See you Then.






MultiQuote





|