Ok, I have a list of usernames that appear on my website. One of the features is that users can send a private message to any other user.
When a user logs in if there are any unread messages it displays (above the usernames) a link to the unread message in the form of the senders username under the heading "Messages". When a users clicks on an unread message the script changes the colum 'viewed' in table privatemessages to 'yes'.
Ok, so the script can recognise whether to display a message or not for that user who is logged into the system. Great that works well.
My problem is that I want the title of "messages" to dissappear if there are no messages for a user. Which it currently does not.
In the script below I have tried using a mysql_num_rows function and screwed it up. I dont wanna wreck the rest of my script coz it took me ages. So Im posting it here in the hope someone can help me sort it out!!! thanks!!!!
<?php
//CONFIGURATION START
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$user = "pablo";
//CONFIGURATION END
//This function checks to see if there are any MySQL errors or not
function CheckMySQL()
{
if (mysql_errno() > 0)
{
die("<br> MySQL error ".mysql_errno().": ".mysql_error());
}
}
//Lets conect to MySQL so that we can use this script
$db = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$db)
{
die("Failed to open connection to MySQL server.");
}
//Now lets selct which databse we are going to use.
mysql_select_db("mountaincanon");
CheckMySQL();
$query_checkmessages = "SELECT * FROM privatemessages";
$result_checkmessages = mysql_query($query_checkmessages);
CheckMySQL();
echo ("<html>\n<head>\n<title>\nUserList\n</title>\n");
echo("<style type=\"text/css\">\n");
echo ("<!--\n");
echo ("A:link {font-family: arial, helvetica, geneva, sans-serif; text-decoration: none; color: #FFFFFF}\n");
echo ("A:visited {font-family: arial, helvetica, geneva, sans-serif; text-decoration: none; color: #FFFFFF}\n");
echo ("A:hover {font-family: arial, helvetica, geneva, sans-serif; text-decoration: underline; color: red}\n");
echo ("//-->\n");
echo ("</style>\n");
echo ("</head>\n");
echo ("<body>\n");
echo ("<table border=1 width=100 bordercolor=red bgcolor=#000000><tr><td bgcolor=red>\n");
echo ("<font face=arial color=#000000 size=2><b>\n");
echo (".:USERS:.\n");
echo ("</b></font>\n");
echo ("</td></tr>\n");
echo ("<tr><td>\n");
//This checks to see if there are any unread messages for the logged in user.
//This is also where all the errors are occuring!!!!
//Or rather, not errors, I just dont know what to do!
While ($row = mysql_fetch_array($result_checkmessages))
{
$result_numrow = "SELECT * FROM privatemessages";
$displayMessage = mysql_num_rows($result_numrow);
if (($displayMessage != "1") && ($row[to_user] == $user)) {
echo ("<table border=0 cellpadding=0 cellspacing=0>\n");
} else {
echo ("<table border=0 cellpadding=0 cellspacing=0>\n");
echo ("<tr><td><b><font size=2 face=tahoma color=#FFFFFF><u>Messages</u></font></b></td></tr>\n");
}
//The following displays the correct message link for the logged in user
While ($row = mysql_fetch_array($result_checkmessages))
{
if (($row[viewed] == "no") && ($row[to_user] == $user)) {
echo ("<tr><td><b><a href=ViewPM.php?user=$user&from=$row[from_user]><font size=1>$row[from_user]</font></a></b></td></tr>\n");
} else {}
}
echo ("<tr><td><b><font size=1 face=tahoma> </td></tr>\n");
echo ("</table>\n");
}
//This selects all the donating users and echos their name with a dollar sign.
//These users are placed at the top of the list and are sorted in alphabetical order.
$query_donator = "SELECT * FROM userinfo ORDER BY donatorvalue";
$result_donator = mysql_query($query_donator);
CheckMySQL;
echo ("<table border=0 cellpadding=0 cellspacing=0>\n");
While ($row = mysql_fetch_array($result_donator))
{
if ($row[donatorvalue] != "nondonator") {
echo ("<tr><td><b><a href=LoadProfile.php?user=$row[username]><font size=1>$row[donatorvalue]</font></a></b></td></tr>\n");
} else {}
}
echo ("</table>\n");
//For the remaining users. This sorts them in alphabetical order below the donating users.
$query = "SELECT * FROM userinfo ORDER BY username";
$result = mysql_query($query);
CheckMySQL;
echo ("<table border=0 cellpadding=0 cellspacing=0>\n");
While ($row = mysql_fetch_array($result))
{
if ($row[donatorvalue] == "nondonator") {
echo ("<tr><td><b><a href=LoadProfile.php?user=$row[username]><font size=1>$row[username]</font></a></b></td></tr>\n");
} else {}
}
echo ("</table><br>");
echo ("</td></tr><tr><td bgcolor=red align=center>\n");
echo ("<a href=form_register.php><font face=arial color=#000000 size=2><b>Register Now!</b></a></font>\n");
echo ("<br>\n");
echo ("<a href=form.php><font face=arial color=#000000 size=2><b>Login</b></a></font>\n");
echo ("</td></tr></table>\n");
echo ("<p>\n");
echo ("</body>\n</html>");
?>