PDA

View Full Version : [PHP] Private Message Help



wsg14
06-09-2008, 12:00 AM
<?php

if($logged['in'] === 0){
header("Location: index.php");
} else {

$query = mysql_query("SELECT * FROM `private_messages` WHERE `to` = '". $sess_username."'");
$pm_count = mysql_num_rows($query);
$today = date("m.d.Y");
$yesterday - mktime(0, 0, 0, date( 'm' ) , date( 'd' ) - 1, date( 'y' ));
$read = mysql_query("SELECT `read` FROM `private_messages` WHERE `to` = '".$sess_username."'");

echo ($today);

while ($display = mysql_fetch_array($query1)){
if($pm_count == 0){
echo 'You do not have any messages at this time.';
} else{
$query1 = mysql_query("SELECT * FROM private_messages WHERE date='".$today."' AND to='".$sess_username."'")or die(mysql_error());
if(mysql_num_rows($query1) > 0){
echo "<b>Today</b><br />";
while($display=mysql_fetch_array($query1)){
if($read === 0){ echo "<b>";
echo "</b>";
echo $display["title"]." - ".$display["from"];
echo "<br />";
} else {
echo "</b>";
echo $display["title"]." - ".$display["from"];
echo "<br />";
}
}
}



$query2 = mysql_query("SELECT * FROM private_messages WHERE date='".$yesterday."' AND to='".$sess_username."'")or die(mysql_error());
if(mysql_num_rows($query2) > 0){
echo "<b>Yesterday</b><br />";
while($display=mysql_fetch_array($query2)){
if($read === 0){ echo "<b>";
echo "</b>";
echo $display["title"]." - ".$display["from"];
echo "<br />";
} else {
echo "</b>";
echo $display["title"]." - ".$display["from"];
echo "<br />";
}
}
}
$query3 = mysql_query("SELECT * FROM private_messages WHERE `date`!='".$today."' AND `date`!='".$yesterday."' AND `to`='".$sess_username."'")or die(mysql_error());
$lastdate="";
while($display=mysql_fetch_array($query3)){
if($display["date"]==$lastdate){
if($read === 0){ echo "<b>";
echo "</b>";
echo $display["title"]." - ".$display["from"];
echo "<br />";
} else {
echo "</b>";
echo $display["title"]." - ".$display["from"];
echo "<br />";
}
}
}
$lastdate=$display["date"];
echo "<b>".$lastdate."</b><br />";
if($read === 0){ echo "<b>";
echo "</b>";
echo $display["title"]." - ".$display["from"];
echo "<br />";
} else {
echo "</b>";
echo $display["title"]." - ".$display["from"];
echo "<br />";
}
}
}
}
?>I've come back to coding PHP, and I'm starting a simple script. I'm having aporblem though, it's not displaying any PM's (there's only 1 in the database) I echoed what the $today variable comes out as, and it says 09.05.2008. I enetered that in the 'date' column for the PM and nothing showed up. Anybody know what's up with it? +rep

EDIT: I'm now getting the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to='Will'' at line 1

Which has to do witht he following code:
$query = mysql_query("SELECT * FROM `private_messages` WHERE `to` = '".$sess_username."'");

Calon
06-09-2008, 12:55 PM
$query = mysql_query("SELECT * FROM `private_messages` WHERE `to` = '$sess_username'");

wsg14
06-09-2008, 09:01 PM
It still gives me that error.

Decode
06-09-2008, 09:45 PM
You need to rename the row "to" to something else.

Hypertext
06-09-2008, 11:12 PM
To is a MySQL reserved word, backticks should not produce this error, but it will still become very confusing use something like pmTo, pmFrom, etc. Also, you don't need to concatenate variables to strings if you are using double quotes.

This is a bug not an error. Your <b> to denote a unread message is not enclosed around the message name.

Don't use parenthesis in language constructs. Language constructs are usually (in your ide) highlighted as green, like echo, include, etc.

I don't understand your logic for a read pm.

Add a column to your table called pmRead and have it as a tinyint(1) defaulted to 0, and then instead of using if($read === 0), use if(!$row['read']), also when your doing triple-equals/=== you could cut down code and just use if($var) as opposed to if($var === 1).

When using dates in MySQL, I'd advise you to use MySQLs DATETIME, it's quite good for doing date checks as you can use mysql's BETWEEN keyword to check for dates, a lot easier than unix time, it comes like MM-DD-YYYY 00:00:00, correct me if I'm wrong.

Try and neaten up your code and name variables more semantically, it might be easy for you to remember as you coded it, but it becomes quite difficult when people get confused between $query, $query1, $query2, etc.

Nitpick: When using things like $display["title"], use single quotes, as you can't use variables within single quotes, it will speed up execution time as it doesn't have to check for variables etc. Post your code again when you've renamed things.

Oh, also, it makes it easier to read if you stick with one way of spacing like:
Sometimes you'll use spaces, and sometimes you won't, especially in your MySQL queries, it gets hard to read when everythings together like



$query3 = mysql_query("SELECT * FROM private_messages WHERE `date`!='".$today."' AND `date`!='".$yesterday."' AND `to`='".$sess_username."'")or die(mysql_error());

You have things like `date`!='".$today."' AND should be:


`date` != '$today' AND


...much easier to read.

Also can you explain what $lastdate actually does.

Excellent2
06-09-2008, 11:41 PM
*REMOVED*

Edited by Flisker (Forum Moderator): Please do not be rude to forum members

Calon
07-09-2008, 01:47 AM
*REMOVED*

Edited by Flisker (Forum Moderator): Please do not be rude to forum members

Moderator's approve posts, how did it get through to be edited.

wsg14
08-09-2008, 11:16 PM
Thanks for the advice Charlie, it helped a bit. I'm still having aproblem though. it's now display double of the "Today" column, and it's also display the PM not only form today, but from yesterday too. (all under the Today column). It looks like this:

Today
hey - guy
yo - guy
Today
hey - guy
yo - guy

It also isn't coming up bold.

Hypertext
09-09-2008, 12:53 AM
Can you repost your current code?

Thanks.

wsg14
09-09-2008, 02:14 AM
Sorry, thought I did.


<?php

if($logged['in'] === 0){
header("Location: index.php");
} else {

$query = mysql_query("SELECT * FROM `private_messages` WHERE `pmto` = '".$sess_username."'");
$pm_count = mysql_num_rows($query);
$today = date("m.d.Y");
$yesterday - mktime(0, 0, 0, date( 'm' ) , date( 'd' ) - 1, date( 'y' ));
$read = mysql_query("SELECT `read` FROM `private_messages` WHERE `pmto` = '".$sess_username."'");

while ($display = mysql_fetch_array($query)){
if($pm_count === 0){
echo 'You do not have any messages at this time.';
} else{
$query1 = mysql_query("SELECT * FROM `private_messages` WHERE `date` ='".$today."' AND `pmto` ='".$sess_username."'")or die(mysql_error());
if(mysql_num_rows($query1) > 0){
echo "<b>Today</b><br />";
while($display = mysql_fetch_array($query1)){
if($read === 0){ echo "<b>";
echo $display['title']." - ".$display['from'];
echo "</b>";
echo "<br />";
} else {
echo "</b>";
echo $display['title']." - ".$display['from'];
echo "<br />";
}
}
} else {}

$query2 = mysql_query("SELECT * FROM `private_messages` WHERE `date`='".$yesterday."' AND `pmto`='".$sess_username."'")or die(mysql_error());
if(mysql_num_rows($query2) > 0){
echo "<b>Yesterday</b><br />";
while($display1 = mysql_fetch_array($query2)){
if($read === 0){ echo "<b>";
echo "</b>";
echo $display1['title']." - ".$display1['from'];
echo "<br />";
} else {
echo "</b>";
echo $display1['title']." - ".$display1['from'];
echo "<br />";
}
}
} else {}

$query3 = mysql_query("SELECT * FROM `private_messages` WHERE `date` !='".$today."' AND `date` !='".$yesterday."' AND `pmto` ='".$sess_username."'")or die(mysql_error());
while($display2 = mysql_fetch_array($query3)){
if($read === 0){ echo "<b>";
echo "</b>";
echo $display2['title']." - ".$display2['from'];
echo "<br />";
} else {
echo "</b>";
echo $display2['title']." - ".$display2['from'];
echo "<br />";
}
}
}
}
}
?>

Hypertext
09-09-2008, 03:01 AM
try

<?php

if($logged['in'] === 0){
header("Location: index.php");
}
else {
$query = mysql_query("SELECT * FROM `private_messages` WHERE `pmto` = '$sess_username'");
$pm_count = mysql_num_rows($query);
$today = date("m.d.Y");
$yesterday - mktime(0, 0, 0, date('m') , date('d')-1, date('y'));
$read = mysql_query("SELECT `read` FROM `private_messages` WHERE `pmto` = '$sess_username'");

while ($display = mysql_fetch_array($query)){
if(!$pm_count){
echo 'You do not have any messages at this time.';
}
else {
$query1 = mysql_query("SELECT * FROM `private_messages` WHERE `date` = '$today' AND `pmto` = '$sess_username'") or die(mysql_error());
if(mysql_num_rows($query1) > 0) {
echo "<b>Today</b><br />";
while($display = mysql_fetch_array($query1)) {
if($read === 0) {
echo "<b>";
echo $display['title']." - ".$display['from'];
echo "</b>";
echo "<br />";
}
echo $display['title']." - ".$display['from'];
echo "<br />";
}
}
}
$query2 = mysql_query("SELECT * FROM `private_messages` WHERE `date` = '$yesterday' AND `pmto` = '$sess_username'") or die(mysql_error());
if(mysql_num_rows($query2) > 0) {
echo "<b>Yesterday</b><br />";
while($display1 = mysql_fetch_array($query2)) {
if(!$read) {
echo "<b>";
echo $display1['title']." - ".$display1['from'];
echo "</b>";
echo "<br />";
}
else {
echo $display1['title']." - ".$display1['from'];
echo "<br />";
}
}
}
$query3 = mysql_query("SELECT * FROM `private_messages` WHERE `date` != '$today' AND `date` != '$yesterday' AND `pmto` = '$sess_username'") or die(mysql_error());
while($display2 = mysql_fetch_array($query3)) {
if(!$read) {
echo "<b>";
echo $display2['title']." - ".$display2['from'];
echo "</b>";
echo "<br />";
}
else {
echo $display2['title']." - ".$display2['from'];
echo "<br />";
}
}
}
}
}
?>

It may look pretty to use excessive whitespace, but generally it's only good in certain situations.

wsg14
09-09-2008, 12:51 PM
It now shows up like this:

yo - guy
hey - guy
yo - guy
hey - guy

wsg14
10-09-2008, 12:42 PM
Anybody?

Hypertext
11-09-2008, 12:30 AM
Check your table, maybe the rows are like that.

Want to hide these adverts? Register an account for free!