The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PM System PHP
Discuss PM System PHP in the PHP Development forum on Dev Shed. PM System PHP PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 28th, 2012, 09:29 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 16
Time spent in forums: 2 h 37 m 45 sec
Reputation Power: 0
|
|
|
PM System PHP
Hello there.
I've made one PM System PHP like 1 day ago, still working on it, its the first time i'm doing it, now i'm coding the inbox, did the count already, now i'm doing the echo's, but i'm not getting on how to fix this.
I've made something like this :
If theres 1 new message it will be Inbox(1 new message)
If theres 2 or more new messages it will be Inbox(2 new messages)
If theres no messages it will be Inbox(0 no messages)
I want it to be Inbox(No messages)
Ill paste here the code i'm using if someone could give me the right code for it, i will be thankfull.
Code:
<?php echo
$msgs;
if($msgs > 0)
{
if($msgs == 1)
{
echo ' new message';
}
else
{
echo ' new messages';
}
}
else
{
echo ' no messages';
}
?>
Code:
<?php $user = $_SESSION['user'];
$count = mysql_query("SELECT COUNT(message_read) AS unread FROM messages WHERE to_user = '".$user."' AND message_read = 0");
$msgs = mysql_result($count, 'unread'); ?>
Thanks.
|

December 28th, 2012, 09:43 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Hi,
the code is all crammed into one line, so it's hard to read.
But as far as I can tell, you have to move the echo $msgs; into the outermost "if" statement so that the count will only be shown in case of $msgs > 0.
|

December 28th, 2012, 09:50 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 16
Time spent in forums: 2 h 37 m 45 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Jacques1 Hi,
the code is all crammed into one line, so it's hard to read.
But as far as I can tell, you have to move the echo $msgs; into the outermost "if" statement so that the count will only be shown in case of $msgs > 0. |
Edited the post, check it please.
|

December 28th, 2012, 09:51 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
My suggestion still holds true.
|

December 28th, 2012, 09:54 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 16
Time spent in forums: 2 h 37 m 45 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Jacques1 My suggestion still holds true. |
edited the code, now it looks like this, but i still couldn't figure how do i make this without appearing the 0.
Code:
<?php
echo $msgs;
if($msgs == 0)
{
echo 'no messages';
}
{
if($msgs == 1)
{
echo ' new message';
}
elseif($msgs > 1)
{
echo ' new messages';
}
}
?>
|

December 28th, 2012, 10:46 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Please read my reply again. I did not say that you should change the "if" statement. I said this:
Quote: | Originally Posted by Jacques1 you have to move the echo $msgs; into the outermost "if" statement so that the count will only be shown in case of $msgs > 0. |
So you have to do this:
PHP Code:
<?php
if($msgs > 0)
{
echo $msgs; // only display $msgs if $msgs > 0
if($msgs == 1)
{
echo ' new message';
}
else
{
echo ' new messages';
}
}
else
{
echo ' no messages';
}
The logic behind this should be pretty simple: Currently, your code displays the count in any case. That's not what you want. You only want to display the count if it's bigger than 0. So you have to move the "echo" inside the corresponding "if" statement. Now you have: if the count is bigger, then display it (otherwise don't).
I should also tell you that your code is open to SQL injections and XSS and that the "mysql_" functions are ancient, but I'm a bit tired of this ...
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|