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 Count PHP
Discuss PM Count PHP in the PHP Development forum on Dev Shed. PM Count 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 27th, 2012, 05:21 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 16
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.
|

December 27th, 2012, 05:54 PM
|
 |
Contributing User
|
|
|
|
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.
|

December 27th, 2012, 06:03 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 16
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?
|

December 27th, 2012, 06:07 PM
|
 |
Contributing User
|
|
|
|
|
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.
|

December 27th, 2012, 06:10 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 16
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?
|

December 27th, 2012, 06:11 PM
|
 |
Contributing User
|
|
|
|
Quote: | Originally Posted by paulocore Hm, what do you mean by PDO? |
It is the way you communicate with your database.
|

December 27th, 2012, 06:12 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 16
Time spent in forums: 2 h 37 m 45 sec
Reputation Power: 0
|
|
|
You mean config?
|

December 27th, 2012, 06:15 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 Rhytz It is the way you communicate with your database. |
Can we talk on msn?
I'm really confused with this.
|

December 27th, 2012, 06:32 PM
|
 |
Contributing User
|
|
|
|
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
|

December 27th, 2012, 07:35 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 16
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_rows( mysql_query( $ck_receiver ) ) == 0 ){ 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
|

December 27th, 2012, 11:44 PM
|
 |
Known to taste like chicken
|
|
Join Date: Aug 2003
Location: In front of my computer
|
|
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
|

December 28th, 2012, 06:21 AM
|
|
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 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.
|
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
|
|
|
|
|