PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 27th, 2012, 05:21 PM
paulocore paulocore is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 16 paulocore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 45 sec
Reputation 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.

Reply With Quote
  #2  
Old December 27th, 2012, 05:54 PM
Rhytz's Avatar
Rhytz Rhytz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 100 Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 11 h 4 m 50 sec
Reputation Power: 50
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.

Reply With Quote
  #3  
Old December 27th, 2012, 06:03 PM
paulocore paulocore is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 16 paulocore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 45 sec
Reputation 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?

Reply With Quote
  #4  
Old December 27th, 2012, 06:07 PM
Rhytz's Avatar
Rhytz Rhytz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 100 Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 11 h 4 m 50 sec
Reputation Power: 50
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.

Reply With Quote
  #5  
Old December 27th, 2012, 06:10 PM
paulocore paulocore is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 16 paulocore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 45 sec
Reputation 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?

Reply With Quote
  #6  
Old December 27th, 2012, 06:11 PM
Rhytz's Avatar
Rhytz Rhytz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 100 Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 11 h 4 m 50 sec
Reputation Power: 50
Quote:
Originally Posted by paulocore
Hm, what do you mean by PDO?


It is the way you communicate with your database.

Reply With Quote
  #7  
Old December 27th, 2012, 06:12 PM
paulocore paulocore is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 16 paulocore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 45 sec
Reputation Power: 0
You mean config?

Reply With Quote
  #8  
Old December 27th, 2012, 06:15 PM
paulocore paulocore is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 16 paulocore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 45 sec
Reputation Power: 0
Quote:
Originally Posted by Rhytz
It is the way you communicate with your database.


Can we talk on msn?

I'm really confused with this.

Reply With Quote
  #9  
Old December 27th, 2012, 06:32 PM
Rhytz's Avatar
Rhytz Rhytz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 100 Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level)Rhytz User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 11 h 4 m 50 sec
Reputation Power: 50
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

Reply With Quote
  #10  
Old December 27th, 2012, 07:35 PM
paulocore paulocore is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 16 paulocore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 45 sec
Reputation 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>


Quote:
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

Reply With Quote
  #11  
Old December 27th, 2012, 11:44 PM
sir_drinxalot's Avatar
sir_drinxalot sir_drinxalot is offline
Known to taste like chicken
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: In front of my computer
Posts: 377 sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 3 h 27 m 44 sec
Reputation Power: 293
Send a message via MSN to 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.
__________________
"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?

Last edited by sir_drinxalot : December 27th, 2012 at 11:48 PM. Reason: linkified

Reply With Quote
  #12  
Old December 28th, 2012, 06:21 AM
paulocore paulocore is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 16 paulocore User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 45 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PM Count PHP

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap