Retrieving messages from a user id in mysql/php
Since my last question was very vague, I figured I would clear some things
up. I am learning how to build a private chat application (for educational
reasons only), and I am trying to figure out the best way to do a private
chat. I created a 2 tables, user and chat. In chat, I have 5 columns:
message_id
user_id
username
message
send
I want to retrieve only the messages from the selected user_id.
Right now, here is my send form. Input name send = the data I want to
retrieve. Until I get the hang of things, I only want to retrieve the
user_id number (so you would put a number in the text area to chat to that
username).
<form name="form" method="post">
<input type="text" name="send" id="send" value="">
<input name="user_id" type="hidden" value="<?php echo
$_SESSION['user_id'];?>" id="user_id">
<textarea name="message" class="message" onkeydown="if (event.keyCode
== 13) document.getElementById('submit').click()"></textarea>
<input type="submit" name="Submit" id="submit" value="">
</form>
And here is my php retrieval form
<?php
class Chat extends Core {
public function fetchMessages() {
$this->query("
SELECT chat.message,
users.username,
users.user_id,
chat.send
FROM chat
JOIN users
ON chat.user_id //and chat.send = users.user_id
ORDER BY chat.timestamp
DESC
");
return $this->rows();
}
}
Basically, I want to set a condition to something like:
<?php
$send=$_POST['send'];
$userid=$_POST['user_id'];
if ($send == $userid || (isset($_POST['user_id']))) //this isn't correct-
just trying to display my though
{
//retrieve message from user_id number in sql table, allowing the
people that have the same user_id as send to only read the message.
}
?>
So basically, how can I only retrieve the information for a specific
user_id number, and where would I place this condition?
No comments:
Post a Comment