Jump to content

Recommended Posts

I want to show how many new messages someone has in their in-game mailbox.  I already have the mail setup, but now I want to show how many messages the players have next to their mail link.

 

Basically, I want this: mail (with new message count in parentheses)

 

If someone can point me in the right direction, I would be oh so grateful.  As I only have internet access to help me, links to tutorials that show me how to achieve this would be best.  I'm really new to this, so I'm not even sure if this is the right place.

 

Thanks in advance..

Link to comment
https://forums.phpfreaks.com/topic/129271-display-mail-count/
Share on other sites

The mail is stored using php/mysql.  I didn't write the script, just installed it.

 

Here's the mail.php:

<?PHP

define('DK_LOADED', '1');
if (file_exists('install_mail.php')) { die("Please delete <b>install_mail.php</b> from your game directory before continuing."); }
include('lib.php');
include('cookies.php');
$link = opendb();
$controlquery = doquery("SELECT * FROM {{table}} WHERE id='1' LIMIT 1", "control");
$controlrow = mysql_fetch_array($controlquery);
$userrow = checkcookies();
if ($userrow == false) { 
    if (isset($_GET["do"])) {
        if ($_GET["do"] == "verify") { header("Location: users.php?do=verify"); die(); }
    }
    header("Location: login.php?do=login"); die(); 
}
if ($controlrow["gameopen"] == 0) { display("The game is currently closed for maintanence. Please check back later.","Game Closed"); die(); }
if ($controlrow["verifyemail"] == 1 && $userrow["verify"] != 1) { header("Location: users.php?do=verify"); die(); }
if ($userrow["authlevel"] == 2) { die("Your account has been blocked. Please try back later."); }

if (isset($_GET["do"])) {
    $do = explode(":",$_GET["do"]);
if ($do[0] == "inbox") { inbox(); }
    elseif ($do[0] == "reply") { reply($do[1]); }
    elseif ($do[0] == "read") { read_mail($do[1]); }
    elseif ($do[0] == "New") { write_mail(); }
    elseif ($do[0] == "Mass") { Mass_mail(); }
    elseif ($do[0] == "Delete") { Delete_mail($do[1]); }
} else { inbox(); }

function inbox() {
    global $userrow, $controlrow;
    $query = doquery("SELECT * FROM {{table}} WHERE UserTo='".$userrow[id]."' ORDER BY id DESC LIMIT 50", "mail");

    $page = "<form method=\"POST\" action=\"mail.php?do=Delete\">
        <table width=\"100%\"><tr><td style=\"padding:1px; background-color:black;\">
			<table width=\"100%\" style=\"margins:0px;\" cellspacing=\"1\" cellpadding=\"3\">
				<tr>
					<th colspan=\"4\" style=\"background-color:#000000;\">
						<center>Inbox</center>
					</th>
				</tr>
				<tr>
					<th width=\"40%\" style=\"background-color:#000000;\">
						Subject:
					</th>
					<th width=\"20%\"style=\"background-color:#000000;\">
						From:
					</th>
					<th width=\"30%\" style=\"background-color:#000000;\">
						Date:
					</th>
					<th style=\"background-color:#000000;\">
						Delete
					</th>
				</tr>\n";
    	
    if (mysql_num_rows($query) == 0) 
	{ 
        	$page .= "<tr>
				  	<td style=\"background-color:#FFFFFF;\" colspan=\"4\">
				  		<b>You have no Messages</b></td></tr>\n";
    	} 

else 
	{ 
        	while ($row = mysql_fetch_array($query)) 
			{
            		$query2 = doquery("SELECT * FROM {{table}} WHERE id='$row[userFrom]'", "users");
            		$author = mysql_fetch_array($query2);



       			 	if($row["STATUS"] == "read")
					{
						$page .= "<tr>
								  	<td style=\"background-color:#000000;\">
										<a href=\"mail.php?do=read:".$row["id"]."\">".$row["Subject"]."</a>
									</td>
									<td style=\"background-color:#000000;\">
										<a href=\"index.php?do=onlinechar:".$author["id"]."\">
										".$author["username"]." (".$author["id"].")</a>
									</td>
									<td style=\"background-color:#000000;\">
										".$row["SentDate"]."
									</td>
									<td style=\"background-color:#000000;\">
									<center><input type=\"checkbox\" name=\"".$row["id"]."\" value=\"yes\" /></center>
									</td>
								 </tr>\n";
					}
			  else
			  		{
                			$page .= "<tr>
								 	<td style=\"background-color:#000000;\">
										<a href=\"mail.php?do=read:".$row["id"]."\">".$row["Subject"]."</a>
									</td>
									<td style=\"background-color:#000000;\">
										<a href=\"index.php?do=onlinechar:".$author["id"]."\">
										 ".$author["username"]." (".$author["id"].")</a>
									</td>
									<td style=\"background-color:#000000;\">
										".$row["SentDate"]."
									</td>
									<td style=\"background-color:#000000;\">
										<center><input type=\"checkbox\" name=\"".$row["id"]."\" value=\"yes\" /></center>
									</td>
								 </tr>\n";
            			} //end of else loop
			} //end of while loop
	} //end of main else loop

    	$page .= "</table></td></tr></table>";
	$page .= "<table>
			<tr>
				<td><input type=\"submit\" name=\"do\" value=\"New\" /></td>
				<td><input type=\"submit\" name=\"do\" value=\"Delete\" /></td>";
    if ($userrow["authlevel"] == 1)
   		$page .= "<td><input type=\"submit\" name=\"do\" value=\"Mass\" /></td>";
    	$page .= "</tr></table></form>";
    	display($page, "Mail :: Inbox");

}//end of function inbox()

function read_mail($id) {
    
global $userrow, $controlrow;
    $query = doquery("SELECT * FROM {{table}} WHERE id='$id'", "mail");
$update_query=doquery("UPDATE {{table}} SET STATUS='read' WHERE UserTo='$userrow[id]' AND id='$id'", "mail");
    $row = mysql_fetch_array($query);
    
if (!$row)
        display("No such Message!<br /><a href=\"javascript: history.go(-1)\">back</a>", "Mail :: Error");
    if ($row['UserTo'] != $userrow['id'])
        die("Hack attempt. This has been sent to the administrator");

    $query2 = doquery("SELECT * FROM {{table}} WHERE id='$row[userFrom]'", "users");
    $author = mysql_fetch_array($query2);


    $Message = $row[Message];

$page = "<table width=\"100%\">
		 <tr>
		 	<td style=\"padding:1px; background-color:black;\">
				<table width=\"100%\" style=\"margins:0px;\" cellspacing=\"1\" cellpadding=\"3\">
					<tr>
						<td colspan=\"2\" style=\"background-color:#000000;\">
							<b><a href=\"mail.php\">Inbox</a> :: ".$row["Subject"]."</b>
						</td>
					</tr>
				</table>
    				<table width=\"100%\" style=\"margins:0px;\" cellspacing=\"1\" cellpadding=\"3\">
						<td width=\"50%\" style=\"background-color:#000000;\">
							<b>From: <a href=\"index.php?do=onlinechar:".$author["id"]."\">
							".$author["username"]." (".$author["id"].")</a></b>
						</td>
		 		</table>
				<table width=\"100%\" style=\"margins:0px;\" cellspacing=\"1\" cellpadding=\"3\">
					<tr>
						<td style=\"background-color:#000000; vertical-align:center;\">
							<b>Send date:</b> ".$row["SentDate"]."
						</td>
					</tr>	
				</table>
				<table width=\"100%\" style=\"margins:0px;\" cellspacing=\"1\" cellpadding=\"3\">
					<tr>
						<td style=\"background-color:#000000; vertical-align:center;\">
							<b>Subject:</b> ".$row["Subject"]."
						</td>
					</tr>
				</table>
				<table width=\"100%\" height=\"200\" style=\"margins:0px;\" cellspacing=\"1\" cellpadding=\"3\">
					<tr>
						<td style=\"background-color:#000000;\">
							".nl2br($Message)."
						</td>
					</tr>
				</table>
		  	</td>
		  </tr>
		  </table><br />";

$page .= "<table width=\"100%\">
		<tr>
			<td style=\"background-color:#000000; vertical-align:center;\">
				<b>Reply to message:</b>
			</td>
		</tr>
		<tr>
			<td style=\"background-color:#000000; vertical-align:center;\">
							<form action=\"mail.php?do=reply:".$row["id"]."\" method=\"post\">
							<b>Subject: </b>
							<input type=\"text\" name=\"Subject\" value=\"RE: $row[subject]\" size=\"20\" maxlength=\"20\" />
			</td>
		</tr>
		</table>
		<table width=\"100%\">	          
		<tr>
			<td>
				<center><textarea name=\"Message\" rows=\"8\" cols=\"44\"></textarea><br /><br /></center>
				<center><input type=\"submit\" name=\"submit\" value=\"Submit\" /> 
				<input type=\"reset\" name=\"reset\" value=\"Reset\" /></center>
							</form>
			</td>
		 </tr>
		 </table>";
    
    display($page, "Mail :: Reading Mail");

}

function reply($reply) {
    global $userrow;

    $query = doquery("SELECT * FROM {{table}} WHERE id=$reply", "mail");
    $mail = mysql_fetch_assoc($query);
    $query2 = doquery("SELECT * FROM {{table}} WHERE id=".$mail['UserFrom']."", "users");
    $mailer = mysql_fetch_assoc($query2);
    $Subject = $_POST["Subject"];
     
        	extract($_POST);
		if ($Message == "")
			{
				display("Why would you like to send empty mail?<br />
			     	    Please go <a href=\"javascript: history.go(-1)\">back</a> and try again.", "Mail :: Error");
			}
		else 	
			$Message = $_POST['Message'];    

if	(isset($_POST['Subject']))
		{	
			extract($_POST);
			if ($Subject == "")
				$Subject="None";
			else			
				$Subject = $_POST['Subject'];
		 }

    $query = doquery("INSERT INTO {{table}} SET id='',UserTo=".$mail['UserFrom'].",UserFrom='".$userrow['id']."',Subject='$Subject',Message='$Message',SentDate=NOW(), STATUS='unread'", "mail");
    header("Location: mail.php");
    die();

}

function Mass_mail() {
    global $userrow;

    if ($userrow["authlevel"] != 1)
        header("Location: mail.php");
    if (isset($_POST['Message'])) {

        	extract($_POST);
		if ($Message == "")
			{
				display("Why would you like to send empty mail?<br />
			     	    Please go <a href=\"javascript: history.go(-1)\">back</a> and try again.", "Mail :: Error");
			}
		else 	
			$Message = $_POST['Message'];    

if	(isset($_POST['Subject']))
		{	
			extract($_POST);
			if ($Subject == "")
				$Subject="None";
			else			
				$Subject = $_POST['Subject'];
		 }
        $oquery = doquery("SELECT * FROM {{table}}", "users");
        while ($receiver = mysql_fetch_assoc($oquery))
            doquery("INSERT INTO {{table}} SET id='',UserTo=".$receiver['id'].",UserFrom='".$userrow['id']."',Subject='$Subject',Message='$Message',SentDate=NOW(), STATUS='unread'", "mail");
        header("Location: mail.php");
        die();
    }
    $page = "<form action=\"mail.php?do=Mass\" method=\"post\">
         <table width=\"100%\">
		 <tr>
		 	<td>
				Mass email Message (<b>Sending to all users in game</b>):<br /><br/ >
				Title:<br />
				<input type=\"text\" name=\"Subject\" size=\"20\" maxlength=\"20\" /><br /><br />
				Message:<br /><textarea name=\"Message\" rows=\"7\" cols=\"40\"></textarea><br /><br />
				<input type=\"submit\" name=\"submit\" value=\"Submit\" /> 
				<input type=\"reset\" name=\"reset\" value=\"Reset\" />
			</td>
		</tr>
		</table></form>";
    display($page, "Mail :: Mass Emailer");

}

function write_mail() {
    global $userrow;

if (isset($_POST["submit"]))
	{	
		extract($_POST);



	if ($Message == "")
		{
			display("Why would you like to send empty mail?<br />
	     		    Please go <a href=\"javascript: history.go(-1)\">back</a> and try again.", "Mail :: Error");
		}
	else 	
		$Message = $_POST['Message'];   	


	if ($Subject == "")
		$Subject="None";
	else			
		$Subject = $_POST['Subject'];


		$errors= 0;
		$errorlist = "";
		$option = "";

		$oquery = doquery("SELECT * FROM {{table}} WHERE username='".$_POST['name']."' LIMIT 1", "users");
    		$receiver = mysql_fetch_assoc($oquery);
	    $oquery2 = doquery("SELECT * FROM {{table}} WHERE username='".$userrow['charname']."' LIMIT 1", "users");
	    $sender = mysql_fetch_assoc($oquery2); // now we have picked the username of the guy sending mail

	    $oqueryID = doquery("SELECT * FROM {{table}} WHERE id='".$_POST['ID']."' LIMIT 1", "users");
	    $receiver_ID = mysql_fetch_assoc($oqueryID);
		$oqueryID2 = doquery("SELECT * FROM {{table}} WHERE id='".$userrow['id']."' LIMIT 1", "users");
	    $sender_ID = mysql_fetch_assoc($oqueryID2); // now we have picked the username of the guy sending mail

     
		if ($receiver == "" & $receiver_ID =="") //if there is no ID or no Username entered we won't allow sending mail
	   		{
				$errors++;
				$errorlist .= "You have to fill out both the <b>Username</b> field and the <b>ID</b> field.<br>
							   You cannot leave either field empty.  I know, I'm working on it.";
			}

		if ($receiver != "" & $receiver_ID =="")// if this is true then user obviouslly wants to send over Username
			{
				if ($receiver == $sender) //if name of reciver = name of sender we won't allow sending mail to ourslf
				{	
				$errors++;
				$errorlist .= "What is the use of sending mail to yourself?";
				}		

				$option="0";
			}

		if ($receiver == "" & $receiver_ID !="")// if this is true then user obviouslly wants to send over user ID
			{
				if ($receiver_ID == $sender_ID) //if id of reciever = id of sender we won't allow sending mail to ourself 
				{
					$errors++;
					$errorlist .= "What is the use of sending mail to yourself?";
				}

				$option="1";
			}

		if ($errors == 0) //if all is well and if nothing returns error then we can start sending
			{ 
        		if($option==0)//this is option for sending over Username
					{	
						doquery("INSERT INTO {{table}} SET id='',UserTo=".$receiver['id'].",UserFrom='".$userrow['id']."',Subject='$Subject',Message='$Message',SentDate=NOW(), STATUS='unread'", "mail");
        					header("Location: mail.php");
        					die();
					}

				if($option==1)//this is option for sending over user ID
					{
						doquery("INSERT INTO {{table}} SET id='',UserTo=".$receiver_ID['id'].",UserFrom='".$userrow['id']."',Subject='$Subject',Message='$Message',SentDate=NOW(), STATUS='unread'", "mail");
        					header("Location: mail.php");
        					die();
					}
			}
		else //if we don't have error counter at 0 then we display the error
			{
				display("<b>Error(s):</b><br /><div style=\"color:red;\">$errorlist</div><br />
				        Please go <a href=\"javascript: history.go(-1)\">back</a> and try again.", "Mail :: Error");
			}
	}//end of if (isset($_POST["submit"]))

   
    $page = "<form action=\"mail.php?do=New\" method=\"post\">
		<table width=\"100%\">
		<tr>
			<td>
				<b>Username:</b><br /><input type=\"text\" name=\"name\" size=\"30\" maxlength=\"30\" /><br />
				<b>ID:</b><br /><input type=\"text\" name=\"ID\" size=\"10\" maxlength=\"10\" /><br />
				<b>Subject:</b><br /><input type=\"text\" name=\"Subject\" size=\"20\" maxlength=\"20\" /><br /><br />
				<b>Message:</b><br /><textarea name=\"Message\" rows=\"8\" cols=\"44\"></textarea><br /><br /><br />
				<input type=\"submit\" name=\"submit\" value=\"Submit\" /> 
				<input type=\"reset\" name=\"reset\" value=\"Reset\" />
			</td>
		</tr>
		</table></form>";

    display($page, "Write Mail");

}

function Delete_mail($id) {
    global $userrow;

    if ($_POST['do'] == 'New') {
        header("Location: mail.php?do=New");
        die();
    }
    if ($_POST['do'] == 'Mass') {
        header("Location: mail.php?do=Mass");
        die();
    }
    foreach($_POST as $a => $b) {
        if ($a != "do")
            doquery("DELETE FROM {{table}}  WHERE id={$a}", "mail");
    }
    header("Location: mail.php");
    die();
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/129271-display-mail-count/#findComment-670225
Share on other sites

  • 2 weeks later...

Okay, after a long time searching, I found out I should use mysql_num_rows or SELECT COUNT().  But, after trying a million times, I'm not sure if I got the syntax right.

 

Here's the code:

<?php

function checkmail () {

define('DK_LOADED', '1');
if (file_exists('install_mail.php')) { die("Please delete <b>install_mail.php</b> from your game directory before continuing."); }
include('lib.php');
include('cookies.php');
$link = opendb();
$controlquery = doquery("SELECT * FROM {{table}} WHERE id='1' LIMIT 1", "control");
$controlrow = mysql_fetch_array($controlquery);
$userrow = checkcookies();
if ($userrow == false) { 
    if (isset($_GET["do"])) {
        if ($_GET["do"] == "verify") { header("Location: users.php?do=verify"); die(); }
    }
    header("Location: login.php?do=login"); die(); 
}
if ($controlrow["gameopen"] == 0) { display("The game is currently closed for maintanence. Please check back later.","Game Closed"); die(); }
if ($controlrow["verifyemail"] == 1 && $userrow["verify"] != 1) { header("Location: users.php?do=verify"); die(); }
if ($userrow["authlevel"] == 2) { die("Your account has been blocked. Please try back later."); }

$result = mysql_query("SELECT STATUS FROM dk_mail where STATUS="unread");

$num_rows = mysql_num_rows($result);

echo $num_rows;

}

?>

 

Here's what I'm trying to do: use the DB "name"; table "mail"; field "status"; and in status, messages are marked read or unread.  I'm trying to get it to show the number of unread messages.

 

If someone can point in the right direction, I would be oh so grateful...

 

 

Link to comment
https://forums.phpfreaks.com/topic/129271-display-mail-count/#findComment-677945
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.