PDA

View Full Version : Private Messaging [PHP]



wsg14
25-07-2008, 11:53 PM
I'm learning PHP, so I'm just making this simple system.

So what I want to happen is: if the user has no private messages it displays a message. Otherwise it shows the messages (haven't done that yet). I just want some confirmation that the code will work, and if it doesn't what's worng with it.


<?php

$query = mysql_query("SELECT * FROM `private_messages` WHERE `id` = '".$sess_id."' ORDER BY date DESC");
$pm_count = mysql_num_rows($query);

if($pm_count == 0){
echo 'You do not have any messages at this time.';
} else{
wotev;
}

?>+rep

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

Dentafrice
25-07-2008, 11:59 PM
wotev; isn't going to work :P

but yes, that looks like it should work.

wsg14
26-07-2008, 12:01 AM
heh. :rolleyes:

Alright, thanks. :)

Source
26-07-2008, 12:18 AM
What I don't understand is ... why post something like that?

Surely it would be easier to test it yourself to see if there are any errors than to ask a community?

Next time DIY. But yes, that will work.

Edit// Why say +rep when you don't have any rep power.

Hypertext
26-07-2008, 12:28 AM
He's doing it for his post count.

Source
26-07-2008, 12:30 AM
Ah right. 1337 ^.^

// I guess that post was for your post count aswel :P

Dentafrice
26-07-2008, 12:31 AM
How do you know?

Source
26-07-2008, 12:32 AM
You forget Mr Mingle. He knows everything.

This comes to mind: http://upload.wikimedia.org/wikipedia/commons/thumb/b/ba/Alexander_The_Man_Who_Knows.jpg/371px-Alexander_The_Man_Who_Knows.jpg

Hypertext
26-07-2008, 12:33 AM
Sorry, are you trying to argue?

Dentafrice
26-07-2008, 12:34 AM
Yes, I would like to know "how do you know he is doing it for his postcount?".

How hard is that to comprehend?

You really didn't need to make another pointless post, asking me if I wanted to argue.. when you could have simply answered the blooming question.

wsg14
26-07-2008, 12:50 AM
Aside from the **** that's been posted, I was actually focusing on this:

SELECT * FROM `private_messages` WHERE `'".$u->."'id` = '".$sess_id."' ORDER BY date DESCHow would I make it so I could add $u->id ($u being a whole different query, and id is gradding the `id` data from that query) into the query above?

Hypertext
26-07-2008, 12:55 AM
I don't really understand what you want to do, but if it's get $u->id it'll be

SELECT * FROM `private_messages` WHERE `{$u->id}` = '{$sess_id}' ORDER BY date DESC

Can you elaborate on what the variables are, and what you want to do.

wsg14
26-07-2008, 12:57 AM
Never mind, I got it.

Thanks Denta for the only contributing post (that helped, and the one above from Charlie) in this thread.

Dentafrice
26-07-2008, 12:57 AM
SELECT * FROM `private_messages` WHERE `uid`='$sess_id' ORDER BY date DESC
If $u->id is from a different query, can you post that?

@edit: Just saw your post, what was wrong?

wsg14
26-07-2008, 01:03 AM
I had:


mysql_query("SELECT * FROM `private_messages` WHERE $u->id = '".$sess_id."' ORDER BY date DESC");When really it should be (what it is currently):


mysql_query("SELECT * FROM `private_messages` WHERE `'".$u->id."' = '".$sess_id."' ORDER BY date DESC");What would I do if I wanted to only display messages from a single person, I know that's what that code should accomplish but...

All that is going to do is fetch private_messages where the user's id = the logged in user's id. How would I make it so that it'll only grab the PM's which have that user's id - because the private messages table doesn't even have any refrence to a user id. (that most likely doesn't make sense)

So what I'm trying to say is: it will not fetch the private messages for the user that's logged in.

EDIT: Once again, got it.

wsg14
26-07-2008, 01:57 AM
Yay, another problem! (well not a problem, but w.e)


$today = date("m.d.Y");
$yesterday = ($today--);I know you can do $variable++; but can you do $variable--;?

What I'm trying to do is said in the variable name, $yesterday. I'm trying to make it grab the date, but make it negative one so it displays yesterday's date. Is that the right way to do it? If not what is the right way?

Dentafrice
26-07-2008, 03:07 AM
I'm not going to tell you how to do it.

You try it first ;)

Protege
26-07-2008, 10:16 AM
Using mktime and date, PHP.net give you a basic outline of it on their func date page.

Dont look if you dont want me to give it away to you. :)


<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
That'll basically produce a timestamp (unix) which you can convert using date.

eg:



<?php
$yesterday = mktime( 0, 0, 0, date( 'm' ) , date( 'd' ) - 1, date( 'y' ) );
$yesterdays_date = date( 'm.d.Y', $yesterday );
echo( $yesterdays_date );
?>

wsg14
26-07-2008, 02:30 PM
Thanks Protege, but found the mktime documentation on PHP.net earlier.

Just hitting one problem that;s stopping me from continuing.


<?php

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

if($pm_count == 0){
echo 'You do not have any messages at this time.';
} else{
if($query->send_date == $today){
echo '<b>Today</b><br />';
if(!$read == 0){
echo '<b>$display[\'name\']</b> - $display[\'from\']<br />';
} else {
echo '$display[\'name\'] - $display[\'from\']<br />';
}
}
if($query->send_date == $yesterday){
echo '<b>Yesterday</b>';
if(!$read == 0){
echo '<b>$display[\'name\']</b> - $display[\'from\']<br />';
} else {
echo '$display[\'name\'] - $display[\'from\']<br />';
}
}

}

?>

Error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/kolzy/public_html/ws/private.php on line 21

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/kolzy/public_html/ws/private.php on line 22

Line 21:

$display = mysql_fetch_array($query);

Line 22:

$pm_count = mysql_num_rows($query);

I tried using a while statement, that got rid of the first error but not the error for line 22. Would I use two while statements?

Tom-743
26-07-2008, 06:39 PM
Because its going to be selecting more than 1 pm you need to use while;



$pm_count = mysql_num_rows($query);
$today = date("m.d.Y");
$yesterday - mktime(0, 0, 0, date( 'm' ) , date( 'd' ) - 1, date( 'y' ));

if($pm_count == 0){
echo 'You do not have any messages at this time.';
} else{
while ( $display = mysql_fetch_array ( $query ) ) {
if($query->send_date == $today){
echo '<b>Today</b><br />';
if(!$read == 0){
echo '<b>$display[\'name\']</b> - $display[\'from\']<br />';
} else {
echo '$display[\'name\'] - $display[\'from\']<br />';
}
}
if($query->send_date == $yesterday){
echo '<b>Yesterday</b>';
if(!$read == 0){
echo '<b>$display[\'name\']</b> - $display[\'from\']<br />';
} else {
echo '$display[\'name\'] - $display[\'from\']<br />';
}
}
}
EDIT: allso i may be wrong but i dont think the variables in that will work becuase of the \ in them;


echo '$display[\'name\'] - $display[\'from\']<br />';If you were getting a error before you changed it u should have changed it to this;


echo $display['name'] . " - " . $display['from'] . "<br />";

Protege
26-07-2008, 06:40 PM
I dont understand why your using $query->send_date?

wsg14
26-07-2008, 07:02 PM
It's basically saying the the send_date table from private_messages = today's date.

Is there another way to accomplish the same thing?

Thanks Tom, but when I use your code you gave me for the echo $display... I had <b>code</b> in it. If I keep it I get an error "unexpected "<"".

CoolHostUK-Dom
26-07-2008, 07:27 PM
try this :)

<?php

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

if($pm_count == 0){
echo 'You do not have any messages at this time.';
} else{
if($query->send_date == $today){
echo '<b>Today</b><br />';
if(!$read == 0){
echo "<b>".$display['name']."</b> - ".$display['from']."<br />";
} else {
echo "".$display['name']." - ".$display['from']."<br />";
}
}
if($query->send_date == $yesterday){
echo '<b>Yesterday</b>';
if(!$read == 0){
echo "<b>".$display['name']."</b> - ".$display['from']."<br />";
} else {
echo "".$display['name']." - ".$display['from']."<br />";
}
}

}

?>

Protege
26-07-2008, 07:34 PM
Dont think Dom's is right but I commented where I got stuck re-coding it.



<?php

$today = date( 'm.d.Y' );
$yesterday = date( 'm.d.Y', mktime( 0, 0, 0, date( 'm' ), date( 'd' ) - 1, date( 'y' ) ) );
$mysqlQuery = mysql_query( 'SELECT * FROM `private_messages` WHERE `to` = "' . $sess_username . '" ORDER BY `date` DESC' );
if( mysql_num_rows( $mysqlQuery ) == 0 )
{
echo( 'Sorry, you have no messages at the this present time' );
}
else
{
while( $r = mysql_fetch_array( $mysqlQuery ) )
{
if( $r[ 'send_date' ] == $today )
{
echo( 'Today' );
}
elseif( $r[ 'send_date' ] == $yesterday )
{
echo( 'Yesterday' );
// don't know where you define $read lol
}
}
}
?>

wsg14
26-07-2008, 08:48 PM
Thanks for the help Dom and Protege, but Tom beat you all to the punch.

Hypertext
26-07-2008, 08:50 PM
Not to be pedantic but echo is not a function and therefore doesn't require parenthesis. It looks like a problem with how your outputting the data, download Aptana Studio for free, and debug in that (it will underline bad code).

iTech
26-07-2008, 08:55 PM
Not to be pedantic but echo is not a function and therefore doesn't require parenthesis. It looks like a problem with how your outputting the data, download Aptana Studio for free, and debug in that (it will underline bad code).


You're gay.

Edited by Invent (Forum Moderator): Please don't be rude.

Invent
26-07-2008, 08:56 PM
Not to be pedantic but echo is not a function and therefore doesn't require parenthesis.

Maybe so, but it does make debugging easier and doesn't slow down your script execution time badly.

Dentafrice
26-07-2008, 08:57 PM
It is a language construct, but it works either ways. You can use it like you are calling it as a method/function.



echo "hey";


is the same as



echo("hey");


as well as the same as



echo 'hey';


as well as



echo ('hey');


So it really doesn't matter, but server setups vary.

Aptana Studio free version isn't really good. I suggest you *look* for a professional copy of Aptana Studio with the PHP plugin, or a copy of Zend Studio for Eclipse (free 30 day trial)

I would recommend Zend Studio over Aptana :)

wsg14
26-07-2008, 08:58 PM
I'll go hunting for Zend Studio later on. Is it better than phpDesigner 08?

Dentafrice
26-07-2008, 08:59 PM
I think it is, I used to use phpDesigner08. Zend is pretty hard to get used to though :P

wsg14
26-07-2008, 09:00 PM
Meh, better start now then.

Protege
26-07-2008, 09:06 PM
*removed*

Edited by Invent (Forum Moderator): Please don't be rude.

Dentafrice
26-07-2008, 09:12 PM
Well put!

+REP.

iTech
26-07-2008, 09:13 PM
*removed*.


Hole in one.

wsg14
26-07-2008, 09:13 PM
Haha. :P

So we got Charlie and Greg, who else is going on the list?

Protege
26-07-2008, 09:14 PM
I had to rant about something, and his post was most needed in my views...

People who act like **** & use macs - all of them.

Time to get an infraction lalala... but if someone called Myke is to do that - oh there be fun.

CoolHostUK-Dom
26-07-2008, 09:18 PM
You're gay.

rofl, that made me laugh irl so much :D
+ REP :P

Hypertext
26-07-2008, 09:58 PM
I'm on a list?

Dentafrice
26-07-2008, 10:01 PM
Yes, the idiot list.

Invent
26-07-2008, 10:02 PM
Protege, I resent that comment you made about mac owners (If I remember your post correctly, ahah!). I have a mac, I'm not a "***" thank you very much! :P

iTech
26-07-2008, 10:15 PM
Protege, I resent that comment you made about mac owners (If I remember your post correctly, ahah!). I have a mac, I'm not a "***" thank you very much! :P

You are, some of the time.

Invent
26-07-2008, 10:16 PM
Speak for yourself, my friend ;)

wsg14
26-07-2008, 10:17 PM
Yes, the idiot list.

Bingo.

Invent
26-07-2008, 10:21 PM
I know I've contributed towards it, but can we please get back to the thread topic as this has gone quite off-topic :P

iTech
26-07-2008, 10:50 PM
Speak for yourself, my friend ;)

I can admit I'm an ignorant *******.

Protege
26-07-2008, 11:18 PM
You are classed now, you just warned me. :)

Dentafrice
26-07-2008, 11:20 PM
Be warned...
hahaha.

Protege
26-07-2008, 11:35 PM
yah fo sho lols I don't mind I'll take the lil "punishment"

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