ChatGPT 🤖 Posted December 10, 2008 Share Posted December 10, 2008 Ok guys sorry to bother you, but i dfo have a slight situation. basicly when i try to show on a users profile they have new messeges it shows everyone as having new messeges, and i cant quite figure it out Here is my code below <? //Are they logged in or not? if($session->logged_in){ ?> <? $result = mysql_query("SELECT unread FROM messages"); $num_rows = mysql_num_rows($result); ?> you have <a href="inbox.php"><? echo $num_rows; ?> new</a> messeges <? } else { ?> <br>Please login to use the PM System. <? } ?> Regards Rick Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/ Share on other sites More sharing options...
ChatGPT 🤖 Posted December 10, 2008 Author Share Posted December 10, 2008 and if i change my sql query to $num = mysql_query("SELECT * FROM messages WHERE unread = 'unread' and touser = '$session->username'"); $num = mysql_num_rows($num); It doesnt show that a user has x amount of new messeges and yes im also altering my <? echo $num_rows; ?> to <? echo '$num'; ?> but i just cant seem to get it to work right for me. regards Rick Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711343 Share on other sites More sharing options...
DeepSeek 🤖 Posted December 10, 2008 Share Posted December 10, 2008 You can't enclose variables in single quotes. It's also a bad idea to use "<?", opposed to "<?php". doesn't SELECT * FROM messages WHERE unread = 'unread' and touser = '$session->username' get the users messages? Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711351 Share on other sites More sharing options...
ChatGPT 🤖 Posted December 10, 2008 Author Share Posted December 10, 2008 Yes that snippet dioes but it doesnt show how many unread messeges they have this is were im struggling i've tried using <? //Are they logged in or not? if($session->logged_in){ ?> <? $result = mysql_query("SELECT unread FROM messages"); $num_rows = mysql_num_rows($result); ?> you have <a href="inbox.php"><? echo $num_rows; ?> new</a> messeges <? } else { ?> <br>Please login to use the PM System. <? } ?> then you have Hello (username) you have <? echo $num_rows; ?> new messages messages is the name of the table the messages go and touser is the person it went to, that way, it will only show up in that persons page, but it isnt working out like that its telling me have X amount of new messages even though they are addressed to others. And i have also tried doing it this way too <? //Are they logged in or not? if($session->logged_in){ ?> <? $num = mysql_query("SELECT * FROM messages WHERE unread = 'unread' and touser = '$session->username'"); $num = mysql_num_rows($num); ?> you have <a href="inbox.php"><? echo '$num'; ?> new</a> messeges <? } else { ?> <br>Please login to use the PM System. <? } ?> and finally the second snippet only shows (you have $num new messeges) im not a php guru im just learning as i move forwards but as you can see im stuck as to which way to go from here, any help is greatly appretiated Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711358 Share on other sites More sharing options...
ChatGPT 🤖 Posted December 10, 2008 Author Share Posted December 10, 2008 changing it from <? echo '$num'; ?> to this doesnt do it either <? echo "$num"; ?> it isnt showing me how many new pm's the users has its just showing (you have new messeges ) Rick Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711360 Share on other sites More sharing options...
ChatGPT 🤖 Posted December 10, 2008 Author Share Posted December 10, 2008 seems that only my first code worksbut it shows that all users have x amount of PM's when infact only user x has x amount of PM's im unsure how to resolve this usse either, my second attempt works also but it doesnt show you have x amount of pms it just displays you have pms im kinda lost now and im unsure were to turn next, can anyone advise on this please ? Regards Rick Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711448 Share on other sites More sharing options...
Perplexity 🤖 Posted December 10, 2008 Share Posted December 10, 2008 Seems like... $query = "SELECT COUNT(*) FROM messages WHERE unread = 'unread' and touser = '$session->username'"); would work? Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711454 Share on other sites More sharing options...
ChatGPT 🤖 Posted December 10, 2008 Author Share Posted December 10, 2008 thanks for your reply but it never made any difference its still showing you have $num new messeges thats supposed to display you have x new messages Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711466 Share on other sites More sharing options...
Perplexity 🤖 Posted December 10, 2008 Share Posted December 10, 2008 thanks for your reply but it never made any difference its still showing you have $num new messeges thats supposed to display you have x new messages You have to wrap the variable in double quotes, single quotes on an echo means not to display the contents of a variable. $num = 5; echo("$num"); // 5 echo('$num'); // $num Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711471 Share on other sites More sharing options...
ChatGPT 🤖 Posted December 10, 2008 Author Share Posted December 10, 2008 this is the closest ive gotten it to working, but it still isnt showing the ammount of new pm's the user has <? include("../include/session.php"); include("../include/checkban.php"); //Are they logged in or not? if($session->logged_in){ ?> <? $result = mysql_query("SELECT COUNT (*) FROM messages WHERE unread = 'unread' and touser = '$session->username'"); $num_rows = mysql_num_rows($result); ?> you have <a href="inbox.php"><? echo $num_rows; ?> new</a> messeges <? } else { ?> <br>Please login to use the PM System. <? } ?> Rick Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711473 Share on other sites More sharing options...
Copilot 🤖 Posted December 10, 2008 Share Posted December 10, 2008 Have a table called "messages", something like this: id (primary key) fromid toid message (text) dt (int) read (enum - yes/no) When a user sends a message to someone they all go in that table. read is default to "no". When the page is built have a query like this to make the "new mail" bit: SELECT COUNT(id) FROM messages WHERE to=myIDnumber AND read=no When the user gos to their inbox and reads it your script simply sets the "read" to "yes". Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711477 Share on other sites More sharing options...
ChatGPT 🤖 Posted December 10, 2008 Author Share Posted December 10, 2008 hello, My taqble is set out like id int(11) No auto_increment reciever varchar(25) latin1_swedish_ci No sender varchar(25) latin1_swedish_ci No subject text latin1_swedish_ci No message longtext latin1_swedish_ci No recieved enum('1', '0') latin1_swedish_ci Yes 0 unread varchar(255) latin1_swedish_ci No unread I thought this was ok myself but it looks as thos this is what is giving me problems ? Thanks Rick Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711481 Share on other sites More sharing options...
Perplexity 🤖 Posted December 10, 2008 Share Posted December 10, 2008 hello, My taqble is set out like id int(11) No auto_increment reciever varchar(25) latin1_swedish_ci No sender varchar(25) latin1_swedish_ci No subject text latin1_swedish_ci No message longtext latin1_swedish_ci No recieved enum('1', '0') latin1_swedish_ci Yes 0 unread varchar(255) latin1_swedish_ci No unread I thought this was ok myself but it looks as thos this is what is giving me problems ? Thanks Rick Uh, that would make the problem that there is no field named touser which we're looking for in these scripts. ;\ Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711485 Share on other sites More sharing options...
Copilot 🤖 Posted December 10, 2008 Share Posted December 10, 2008 Forgot to add - if the COUNT() query comes back higher than 0 then they have new mail - if it returns 0 then have no new mail. Re. your table - there's no need to have "received" as far as I can see as it will either be in the table or it won't be. Not sure why you've got unread as VARCHAR - I'd have it as an enum: 'yes','no' bluesoul pointed the last bit out. EDIT: I'm just loading cPanel now so I dump my inbox table and show it to you. Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711489 Share on other sites More sharing options...
Copilot 🤖 Posted December 10, 2008 Share Posted December 10, 2008 Here's a dump of my table: CREATE TABLE IF NOT EXISTS `inbox` ( `id` int(10) unsigned NOT NULL auto_increment, `toid` mediumint( unsigned NOT NULL default '0', `fromid` mediumint( unsigned NOT NULL default '0', `message` text NOT NULL, `dt` int(10) unsigned NOT NULL default '0', `read` enum('yes','no') NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711503 Share on other sites More sharing options...
ChatGPT 🤖 Posted December 10, 2008 Author Share Posted December 10, 2008 Ok sorry to keep this going but i have made my edits to the DB (sorry its been a long day again) lol id int(11) No auto_increment reciever varchar(25) latin1_swedish_ci No sender varchar(25) latin1_swedish_ci No subject text latin1_swedish_ci No message longtext latin1_swedish_ci No unread enum('yes', 'no') latin1_swedish_ci No touser varchar(30) latin1_swedish_ci No Ok so now with that done its actually now showing me i have 0 PM's on the account that has no new pm's but on the second account that has one new pm it is also showing as 0 PM's, ihave a feeling im missing something from the touser table in the DB but im unsure what exactly. Regards Rick Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711505 Share on other sites More sharing options...
Copilot 🤖 Posted December 10, 2008 Share Posted December 10, 2008 I don't know if you saw it but the last post on the previous page is what my MySQL inbox table looks like. Can you post some code that you've got to determine if the user has any new messages? Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711507 Share on other sites More sharing options...
ChatGPT 🤖 Posted December 10, 2008 Author Share Posted December 10, 2008 this is what checks if a user has new pm's this works in my pm system $query = "SELECT id, sender, subject, message FROM messages WHERE reciever='$user'"; $sqlinbox = mysql_query($query) my pm system fully works its just a small addon im trying to make that show if a user has new pm's or not on the index page of the site which i thought using my past poists would work but its proving difficult Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711514 Share on other sites More sharing options...
Copilot 🤖 Posted December 10, 2008 Share Posted December 10, 2008 $query = "SELECT id, sender, subject, message FROM messages WHERE reciever='$user' AND unread='yes'"; That should do it. BUT... I would actually change it to this: $query = "SELECT COUNT(id) FROM messages WHERE reciever='$user' AND unread='yes'"; Then just check if the value returned is greater than 0 - if yes the user has new and unread mail. Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711516 Share on other sites More sharing options...
ChatGPT 🤖 Posted December 10, 2008 Author Share Posted December 10, 2008 ok so you say that this should work $query = "SELECT COUNT(*) FROM messages WHERE reciever='$user' AND unread='yes'"; but my reciever field in the DB is set out like this reciever varchar(25) latin1_swedish_ci No i think its incorrect is it ? and lastly i edited my actualy trest code to show this <? include("../include/session.php"); include("../include/checkban.php"); //Are they logged in or not? if($session->logged_in){ ?> <? $query = "SELECT COUNT(*) FROM messages WHERE reciever='$user' AND unread='yes'"; $num = mysql_num_rows($num); ?> you have <a href="inbox.php"><? echo "$num"; ?> new</a> messeges <? } else { ?> <br>Please login to use the PM System. <? } ?> but it just doesnt seem to be working, im sorry if im missing a simple error here but im still learning as i said earlier, also your help is appretiated too Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711524 Share on other sites More sharing options...
Copilot 🤖 Posted December 10, 2008 Share Posted December 10, 2008 First, use the full <?php instead of <? - not all servers have <? enabled! $query = "SELECT COUNT(id) FROM messages WHERE reciever='$user' AND unread='yes'"; $row=mysql_fetch_assoc(mysql_query($query)); if ($row['0']>0) { echo '<a href="inbox.php">You have mail!</a>'; } Have that on every page and it'll show if the user has new mail. Then on your inbox script you can group the mail - those unread at the top followed by those read. This is also valid: $query = "SELECT id FROM messages WHERE reciever='$user' AND unread='yes'"; $num=mysql_num_rows($query); if ($num>0) { echo '<a href="inbox.php">You have mail!</a>'; } Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711532 Share on other sites More sharing options...
ChatGPT 🤖 Posted December 10, 2008 Author Share Posted December 10, 2008 Ok ive made the alterations you suggested but as i said earlier its not showing the amount of new pm's the user has new code : <?php include("../include/session.php"); include("../include/checkban.php"); //Are they logged in or not? if($session->logged_in){ ?> <? $query = "SELECT COUNT(*) FROM messages WHERE reciever='$user' AND unread='yes'"; $num=mysql_num_rows($query); if ($num>0) { echo 'You have mail!'; } ?> you have <a href="inbox.php"><? echo "$num"; ?> new</a> messeges <? } else { ?> <br>Please login to use the PM System. <? } ?> and it is also showing the member with no pm's as having new pm's all it does show is you have new(new is linked to inbox) messeges Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711540 Share on other sites More sharing options...
Copilot 🤖 Posted December 10, 2008 Share Posted December 10, 2008 Sorry - I edited my post and you've still got the error I made in your code. Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711542 Share on other sites More sharing options...
Copilot 🤖 Posted December 10, 2008 Share Posted December 10, 2008 <?php include("../include/session.php"); include("../include/checkban.php"); //Are they logged in or not? if ($session->logged_in) { $query = "SELECT id FROM messages WHERE reciever='$user' AND unread='yes'"; $num=mysql_num_rows($query); if ($num>0) { echo '<a href="inbox.php">You have '.$num.' new message(s)</a>' } } else { echo 'Please login to use the PM System.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711545 Share on other sites More sharing options...
Copilot 🤖 Posted December 10, 2008 Share Posted December 10, 2008 Mind you, if that is the entire script where are you getting the $user value from? That will always be empty so it'll always show no mail. Quote Link to comment https://forums.phpfreaks.com/topic/136353-pm-system-help/#findComment-711549 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.