Thread: PM System PHP

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep 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.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,904
    Rep Power
    1045
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    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.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,904
    Rep Power
    1045
    My suggestion still holds true.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    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';
     }
    }
    ?>
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,904
    Rep Power
    1045
    Please read my reply again. I did not say that you should change the "if" statement. I said this:

    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 ...

IMN logo majestic logo threadwatch logo seochat tools logo