PDA

View Full Version : A couple problems [PHP]



wsg14
27-07-2008, 11:22 PM
<?php

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

$query = mysql_query("SELECT * FROM `private_messages` WHERE `to` = '".$sess_username."' ORDER BY date DESC");
$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."'");

while ($display = mysql_fetch_array($query)){
if($pm_count == 0){
echo 'You do not have any messages at this time.';
} else{
if($query->date == $today){
echo '<b>Today</b><br />';
if(!$read == 0){
echo "<b>" . $display['title'] . " - " . $display['from'] . "</b><br />";
} else {
echo $display['title'] . " - " . $display['from'] . "<br />";
}
}
if($query->date == $yesterday){
echo '<b>Yesterday</b><br />';
if(!$read == 0){
echo "<b>" . $display['title'] . " - " . $display['from'] . "</b><br />";
} else {
echo $display['title'] . " - " . $display['from'] . "<br />";
}
}
if(!$read == 0){
echo $display['date'] . "<br />";
echo "<b>" . $display['title'] . "-" . $display['from'] . "</b><br />";
} else {
echo $display['date'] . "<br />";
echo $display['title'] . "-" . $display['from'] . "</b><br />";
}
}
}
}
?>With that, code basically I want the PM's to show like this:


Today
yo - user1

Yesterday
brb - user2

01.01.01
nah - user3

etc.But with that code it's showing this:

Yesterday
lolol - a guy
07-27-2008
lolol-a guyFirst problem is that it's showing 1 message twice, in two different time frames (07-27-2008 - that's what is in the DB for 'date').

I cannot seem to find out why it's doing that. Many thanks to whoever fixes it.

Moved by Invent (Forum Moderator) from Designing & Development: Please post in the correct forum next time, thanks :).

wsg14
28-07-2008, 03:38 AM
Forgot to post the second problem.

Which is: the date in the DB is today's date but it's still categorized under yesterday.

Source
28-07-2008, 02:01 PM
I'll have a read through later, just I'm a lazy ***. So if invent/dentafrice/whoever doesn't get to it i'll have a read through.

Delatory
30-07-2008, 07:16 PM
<?
if($logged['in'] === 0){
header("Location: index.php");
} else {
// Check for PM's
$query = mysql_query("SELECT * FROM private_messages WHERE to='".$sess_username."'")or die(mysql_error());
if(mysql_num_rows($query)=="0"){
echo "You do not have any messages at this time.";
} else {
// Dates
$today = date("d.m.Y");
$yesterday = date("d.m.Y", mktime(0, 0, 0, date("m") , date("d")-1, date("y")));
// Today
$query = mysql_query("SELECT * FROM private_messages WHERE date='".$today."' AND to='".$sess_username."'")or die(mysql_error());
if(mysql_num_rows($query)){
echo "<b>Today</b><br />";
while($display=mysql_fetch_array($query)){
if(!$display[read]) echo "<b>";
echo $display["title"]." - ".$display["from"];
if(!$display[read]) echo "</b>";
echo "<br />";
}
}

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

// Other Dates
$query = 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($query)){
if($display["date"]==$lastdate){
if(!$display[read]) echo "<b>";
echo $display["title"]." - ".$display["from"];
if(!$display[read]) echo "</b>";
echo "<br />";
} else {
$lastdate=$display["date"];
echo "<b>".$lastdate."</b><br />";
if(!$display[read]) echo "<b>";
echo $display["title"]." - ".$display["from"];
if(!$display[read]) echo "</b>";
echo "<br />";
}
}
}
}
?>


Instead of going through each message. It should of been done by date.
Get all the private messages from today = +1 query.
Get all the private messages from yesterday = +1 query.
Get all the private messages excluding today and yesterday = +1 query.

For the last bit aswell i had to put on a lastdate to check if its still in the right group other wise it would come out like

01.01.01
nah - user 3

01.01.01
nah2 - user 4

ect. ect.

Any problems with it post and i'll see if i can spot the error but looking through it now i doubt i can find any error, i would test the script but i only just reinstalled my laptop and dont have a php server running yet. sry.

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