dean7 Posted April 20, 2009 Share Posted April 20, 2009 hi ive got a wesite that you can pm other users though the website but ive got messages.php: <? ob_start(); //the above line needs to be above ALL HTML and PHP (except for <?). include("config.php"); //gets the config page, which connects to the database and gets the user's information if ($logged[username]) { //checks to see if they are logged in switch($_GET ) { //this allows us to use one page for the entire thing default: Echo" <meta http-equiv='refresh' content='0;URL=messages.php?page=inbox'> "; break; case 'write': if (!$_POST[send]) { //the form hasnt been submitted yet.... echo (" <a href='messages.php'>Go Back</a><br><br> <form method=\"POST\" style=\"margin: 0px;\"> <dl style=\"margin: 0px;\"> <dt>recipient</dt> <dd> <select name='to'> "); $getusers = mysql_query("SELECT * FROM users ORDER BY 'username' ASC"); while ($users = MySQL_Fetch_Array($getusers)) { echo ("<option value=\"$users[username]\">$users[username]</option>"); } //the above line gets all the members names and puts them in a drop down box echo (" </select> </dd> <dt>Message Subject</dt> <dd><input type=\"text\" name=\"subject\" size=\"20\"></dd> <dt>Message</dt> <dd><textarea rows=\"7\" name=\"message\" cols=\"35\"></textarea> </dd><dt> </dt> <dd><input type=\"submit\" value=\"Submit\" name=\"send\"></dd> </dl> </form> "); } if ($_POST[to]) { //the form has been submitted. Now we have to make it secure and insert it into the database $subject = htmlspecialchars(addslashes("$_POST[subject]")); $message = htmlspecialchars(addslashes("$_POST[message]")); $to = htmlspecialchars(addslashes("$_POST[to]")); //the above lines remove html and add \ before all " $send = mysql_query("INSERT INTO `pmessages` ( `title` , `message` , `touser` , `from` , `unread` , `date` ) VALUES ('$subject', '$message', '$to', '$logged[username]', 'unread', NOW())"); echo (" <a href='messages.php?page=inbox'>Go Back</a><br><br> Your message has been sent."); } break; case 'delete': if (!$_GET[msgid]) { echo (" <a href='messages.php?page=inbox'>Go Back</a><br><br> Sorry, but this is an invalid message. "); } else { $getmsg = mysql_query("SELECT * from pmessages where id = '$_GET[msgid]'"); $msg = mysql_fetch_array($getmsg); //hmm..someones trying to delete someone elses messages! This keeps them from doing it if ($msg[touser] != $logged[username]) { echo (" <a href='messages.php?page=inbox'>Go Back</a><br><br> This message was not sent to you! "); } else { $delete = mysql_query("delete from pmessages where id = '$_GET[msgid]'"); echo (" <a href='messages.php?page=inbox'>Go Back</a><br><br> Message Deleted! "); } } break; case 'deleteall': $delete = mysql_query("delete from pmessages where touser = '$logged[username]'"); echo (" <a href='messages.php?page=inbox'>Go Back</a><br><br> All Message Deleted! "); break; case 'inbox': $get = mysql_query("SELECT * from pmessages where touser = '$logged[username]' order by id desc"); echo(" <a href='messages.php?page=write'>Create New Message</a><br><br> <a href='messages.php?page=deleteall'>Delete All Messages</a><br><br> <table border=\"0\" width=\"100%\" cellspacing=\"0\"> <tr> <td align=\"center\" style=\"border-bottom:#000000 solid 1px;\">Subject</td> <td align=\"center\" width=\"125\" style=\"border-bottom:#000000 solid 1px;\">From</td> <td align=\"center\" width=\"97\" style=\"border-bottom:#000000 solid 1px;\">Date</td> <td width=\"25\" style=\"border-bottom:#000000 solid 1px;\">Delete</td> </tr> </table> "); $nummessages = mysql_num_rows($get); if ($nummessages == 0) { echo ("You have 0 messages!"); } else { echo("<table border=\"0\" width=\"100%\" cellspacing=\"1\">"); while ($messages = mysql_fetch_array($get)) { //the above lines gets all the messages sent to you, and displays them with the newest ones on top echo (" <tr> <td><a href=\"messages.php?page=view&msgid=$messages[id]\">"); if ($messages[reply] == yes) { echo ("Reply to: "); } echo ("$messages[title]</a></td> <td width=\"125\">$messages[from]</td> <td width=\"97\">$messages[date]</td> <td width=\"25\"><a href=\"messages.php?page=delete&msgid=$messages[id]\">Delete</a></td> </tr>"); } echo ("</table>"); } break; case 'view': //the url now should look like ?page=view&msgid=# if (!$_GET[msgid]) { //there isnt a &msgid=# in the url echo (" <a href='messages.php?page=inbox'>Go Back</a><br><br> Invalid message!"); } else { //the url is fine..so we continue... $getmsg= mysql_query("SELECT * from pmessages where id = '$_GET[msgid]'"); $msg = mysql_fetch_array($getmsg); //the above lines get the message, and put the details into an array. if ($msg[touser] == $logged[username]) { //makes sure that this message was sent to the logged in member if (!$_POST[message]) { //the form has not been submitted, so we display the message and the form $markread = mysql_query("Update pmessages set unread = 'read' where id = '$_GET[msgid]'"); //this line marks the message as read. $msg[message] = nl2br(stripslashes("$msg[message]")); //removes slashes and converts new lines into line breaks. echo ("<a href='messages.php?page=inbox'>Go Back</a><br><br> <form method=\"POST\" style=\"margin: 0px;\"> <dl style=\"margin: 0px;\"> <dt><b>$msg[title] -- From $msg[from]</b></dt> <dd>$msg[message]</dd> <dt><b>Reply</b></dt> <dd><textarea rows=\"6\" name=\"message\" cols=\"45\"></textarea></dd> <dt> </dt> <dd><input type=\"submit\" value=\"Submit\" name=\"send\"></dd> </dl></form>"); } if ($_POST[message]) { //This will send the Message to the database $message = htmlspecialchars(addslashes("$_POST[message]")); $do = mysql_query("INSERT INTO `pmessages` ( `title` , `message` , `touser` , `from` , `unread` , `date`, `reply`) VALUES ('$msg[title]', '$message', '$msg[from]', '$logged[username]', 'unread', NOW(), 'yes')"); echo (" <a href='messages.php?page=inbox'>Go Back</a><br><br> Your message has been sent"); $getrecipient = mysql_query("SELECT * FROM users WHERE user = '$to'"); $recipient = mysql_fetch_assoc($getrecipient); mail($recipient[username], "You have a new private message!", "You have a new private message from: <strong>" . $logged['username'] . "</strong> /n/n Click <a href=\"http://speed-racing.netau.net/messages.php?page=inbox\">here</a> to view it.", "From: "); } } else { //This keeps users from veiwing other users comments echo(" <a href='messages.php?page=inbox'>Go Back</a><br><br> <b>Error</b><br />"); echo ("This message was not sent to you!"); }} Echo" </td> </tr> </table> "; break; } } ?> But when someone recives a message i want them to get a number next to it, so say the had 1 message there be somthing like (1) next to the link to inbox. Ive tryed most ways i can but are you able to find out how to make it do that? Please help Thanks Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 20, 2009 Share Posted April 20, 2009 Well what did you actually try? There's quite a lot of code there so i'm not sure if any of it is actually relevant to the question in hand. Quote Link to comment Share on other sites More sharing options...
soak Posted April 20, 2009 Share Posted April 20, 2009 SELECT COUNT(*) AS c FROM `pmessages` WHERE `touser` = ? AND `unread` = 'unread' Where ? is the userid of the currently logged in user. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 20, 2009 Share Posted April 20, 2009 if(mysql_num_rows($sql) > 0) { echo 'You have a message'; } And yeah, I agree with GingerRobot, I didn't actually read any of your code. Quote Link to comment Share on other sites More sharing options...
dean7 Posted April 20, 2009 Author Share Posted April 20, 2009 if(mysql_num_rows($sql) > 0) { echo 'You have a message'; } And yeah, I agree with GingerRobot, I didn't actually read any of your code. Were do i put: if(mysql_num_rows($sql) > 0) { echo 'You have a message'; } on my code?. Sorry but im abit new to all this Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 20, 2009 Share Posted April 20, 2009 Wherever you want to tell them they have a new message. Quote Link to comment Share on other sites More sharing options...
dean7 Posted April 20, 2009 Author Share Posted April 20, 2009 When i put that code in were i want it i get a error message say: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a6651052/public_html/nav.php on line 59 Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 20, 2009 Share Posted April 20, 2009 Well, you have to run the query before it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.