Thread: PM Count PHP

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

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0

    PM Count PHP


    Hello there, i'm new here, some friend told me to go here to resolve my problem that i'm having for long time ago, anyways, ill try to explain.

    I've coded one Private messages system so far, i'm trying to put at USER CP one text that says " Your inbox " if theres new messages that the user haven't seen yet, it will be changed to " Your inbox(number of unread messages) "

    Any idea on how to make it?

    Thanks
    Paulo.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    105
    Rep Power
    51
    This will depend on the structure of your database.

    Lets say you have a field that keeps track of the read status of the private messages.

    All you need to do is query your database, count all the messages with "unread" status.

    PHP Code:
    $nRows $pdo->query('select count(*) from blah')->fetchColumn(); 
    echo 
    count($nRows); 
    Next you do an if statement to see if the query returned any unread messages.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    So the blah will be the message_read = 0 column am i right?

    Since i have a column that is message_read, Everytime users read the message the column will be auto updated to 1 it means its already visited by user, and if the message is 0 it means the user haven't seen the message yet.

    PHP Code:
    $nRows $pdo->query('select count(*) from messages WHERE message_read = '0' AND to_user='$userfinal' ')->fetchColumn();  echo count($nRows); 
    This?
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    105
    Rep Power
    51
    That was just an example. You will need to adjust it depending on your database structure.

    For example you would also need to consider user id's. Simply querying like this will return ALL unread messages from all users.


    EDIT: Just saw you edited your post. Something like that should indeed do the trick. Are you using PDO to query your database?
    Last edited by Rhytz; December 27th, 2012 at 06:21 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    Hm, what do you mean by PDO?

    PHP Code:
    }else{ echo ' $nRows = $pdo->query('select count(*) from messages WHERE message_read '0' AND to_user='$userfinal' ')->fetchColumn();  echo count($nRows)'; }} 
    Should i do this?
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    105
    Rep Power
    51
    Originally Posted by paulocore
    Hm, what do you mean by PDO?
    It is the way you communicate with your database.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    You mean config?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    Originally Posted by Rhytz
    It is the way you communicate with your database.
    Can we talk on msn?

    I'm really confused with this.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    105
    Rep Power
    51
    Lets keep it on the forums so everyone can learn

    I understand you already have a working system and want to add this functionality to it.

    Can you post some of your current code?

    Your code will depend on the way your current system communicates with your database.

    There are 3 ways:

    PDO
    MySQLi
    or the old mysql functions
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    Well i will post here all the codes.

    inbox.php
    PHP Code:
    <?php session_start(); require "/Inetpub/vhosts/core5.pt/httpdocs/includes/global.php"$userfinal=$_SESSION['user'];  // get the messages from the table. $get_messages = mysql_query("SELECT message_id FROM messages WHERE to_user='$userfinal' ORDER BY message_id DESC") or die(mysql_error()); $get_messages2 = mysql_query("SELECT * FROM messages WHERE to_user='$userfinal' ORDER BY message_id DESC") or die(mysql_error()); $num_messages = mysql_num_rows($get_messages); // display each message title, with a link to their content echo '<ul>'; for($count = 1; $count <= $num_messages; $count++) {  $row = mysql_fetch_array($get_messages2); //if the message is not read, show "(new)" after the title, else, just show the title. if($row['message_read'] == 0) { echo '<a href="read_message.php?messageid=' . $row['message_id'] . '">' . $row['message_title'] . '</a>(<strong><font color="red">Novo*</font></strong>)<br>'; }else{ echo '<a href="read_message.php?messageid=' . $row['message_id'] . '">' . $row['message_title'] . '</a><br>'; }} echo '</ul>'; echo '<form name="newmsgfrm" method="post" action="novamp.php">'; echo '<input type="submit" value="Enviar nova mensagem">'; echo '</form>';  echo '<form name="backfrm" method="post" action="index.php">'; echo '<input type="submit" value="Voltar ao inicio">'; echo '</form>'; ?>
    read_message.php
    PHP Code:
    <?php session_start(); $userfinal=$_SESSION['user']; require "/Inetpub/vhosts/core5.pt/httpdocs/includes/global.php";  $messageid $_GET['messageid']; $message mysql_query("SELECT * FROM messages WHERE message_id = '$messageid' AND to_user = '$userfinal'"); $message=mysql_fetch_array($message); $q="UPDATE messages SET message_read='1' WHERE message_id = '$messageid' AND to_user = '$userfinal'"mysql_query($q);   echo "<h1>Titulo : ".$message['message_title']."</h1><br><br>"; echo "<h3>De : ".$message['from_user']."<br><br></h3>"; echo "<h3>Mensagem : <br>".$message['message_contents']."<br></h3>";  echo '<form name="backfrm" method="post" action="caixadeentrada.php">'; echo '<input type="submit" value="Voltar a caixa de entrada">'; echo '</form>'?>
    Messageconfig.php
    PHP Code:
    <?php session_start(); require "/Inetpub/vhosts/core5.pt/httpdocs/includes/global.php";  $title=$_POST['message_title']; $to=$_POST['message_to']; $content=$_POST['message_content']; $from=$_POST['message_from']; $time=$_POST['message_date'];  $ck_receiver "SELECT usuario FROM usuarios WHERE usuario = '".$to."'";   if( mysql_num_rowsmysql_query$ck_receiver ) ) == ){ die("O usuario que estas a tentar enviar uma mensagem privada nao existe na nossa base de dados, certifica-te que nao ta em branco.<br><br> <form name=\"back\" action=\"novamp.php\" method=\"post\"> <input type=\"submit\" value=\"Tentar novamente\"> </form> "); } elseif(strlen($content) < 1){ die("Nao podes enviar uma mensagem em branco, por favor escreve qualquer coisa.<br><br> <form name=\"back\" action=\"novamp.php\" method=\"post\"> <input type=\"submit\" value=\"Tentar novamente\"> </form> "); } elseif(strlen($title) < 1){ die("Tens de ter um titulo.<br><br> <form name=\"back\" action=\"novamp.php\" method=\"post\"> <input type=\"submit\" value=\"Tentar novamente\"> </form> "); }else{ mysql_query("INSERT INTO messages (from_user, to_user, message_title, message_contents, message_date) VALUES ('$from','$to','$title','$content','$time')") OR die("Could not send the message: <br>".mysql_error()); echo "A mensagem foi enviada com sucesso, aguarda a resposta do utilizador."?> <form name="back" action="inbox.php" method="post"> <input type="submit" value="Back to The Inbox"> </form> <?php ?>
    New_message.php
    PHP Code:
    <?php session_start(); require "/Inetpub/vhosts/core5.pt/httpdocs/includes/global.php"$userfinal=$_SESSION['user']; $user=$userfinal?> <form name="message" action="mensagemco.php" method="post"> Titulo : <input type="text" name="message_title">  <br> Para : <input type="text" name="message_to"> <br> Mensagem : <br> <textarea rows="20" cols="50" name="message_content"> </textarea> <?php echo '<input type="hidden" name="message_from" value="'.$user.'"><br>'?> <input type="submit" value="Submit"> </form>
    Originally Posted by Rhytz
    Lets keep it on the forums so everyone can learn

    I understand you already have a working system and want to add this functionality to it.

    Can you post some of your current code?

    Your code will depend on the way your current system communicates with your database.

    There are 3 ways:

    PDO
    MySQLi
    or the old mysql functions
    Any idea?

    p.s I'm sorry for the time of waiting, i'm currently working on the website so i forgot about this topic LOL
  20. #11
  21. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    312
    from a quick glance at the code you are using the old mysql_connect() mysql_query() functions.

    On top of this you are taking data directly from $_POST and using them in queries. This leaves you wide open to SQL Injection .

    I would strongly recommend that you look at using PDO (PHP Data Objects) with prepared statements to eliminate a lot of that sql injection risk.

    If you really don't want to use PDO for some reason, you should at the very least sanitise your inputs.

    Never, ever trust any data a user can potentially modify, whether it is GET, POST or session data.
    Last edited by sir_drinxalot; December 27th, 2012 at 11:48 PM. Reason: linkified
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0
    Originally Posted by sir_drinxalot
    from a quick glance at the code you are using the old mysql_connect() mysql_query() functions.

    On top of this you are taking data directly from $_POST and using them in queries. This leaves you wide open to SQL Injection .

    I would strongly recommend that you look at using PDO (PHP Data Objects) with prepared statements to eliminate a lot of that sql injection risk.

    If you really don't want to use PDO for some reason, you should at the very least sanitise your inputs.

    Never, ever trust any data a user can potentially modify, whether it is GET, POST or session data.
    Coded it already, and it works well.

IMN logo majestic logo threadwatch logo seochat tools logo