Ye thanks for that, that fixed problem. I've been rusty then..
However, I've just completed full tutorial, and I came across with two problems only.
In view_conversation.php, when I want to Add Message, it do add a message below, but it's not showing text from a message submited. It shows only (by who/time), but not an that text below.
![Image](http://img33.imageshack.us/img33/8572/notextf.jpg)
I've also checked on mPAdmin, if it exists there maybe, but no, it just shows same values as for site page.
![Image](http://img17.imageshack.us/img17/149/tablesg.jpg)
Could you please check func and view_conversation.php, I was looking for that error for 30m+ couldn't see
![Confused :?](./images/smilies/icon_e_confused.gif)
...
pm_system_func.php
[syntax=php]
<?php
// Fetches a summary of the conversations.
function fetch_conversation_summary() {
$sql = "SELECT
`conversations`.`conversation_id`,
`conversations`.`conversation_subject`,
MAX(`conversations_messages`.`message_date`) AS `conversation_last_reply`,
MAX(`conversations_messages`.`message_date`) > `conversations_members`.`conversation_last_view` AS `conversation_unread`
FROM `conversations`
LEFT JOIN `conversations_messages` ON `conversations`.`conversation_id` = `conversations_messages`.`conversation_id`
INNER JOIN `conversations_members` ON `conversations`.`conversation_id` = `conversations_members`.`conversation_id`
WHERE `conversations_members`.`user_id` = {$_SESSION['user_id']}
AND `conversations_members`.`conversation_deleted` = 0
GROUP BY `conversations`.`conversation_id`
ORDER BY `conversation_last_reply` DESC";
$result = mysql_query($sql);
$conversations = array();
while (($row = mysql_fetch_assoc($result)) !== false) {
$conversations[] = array(
'id' => $row['conversation_id'],
'subject' => $row['conversation_subject'],
'last_reply' => $row['conversation_last_reply'],
'unread_messages' => ($row['conversation_unread'] == 1),
);
}
return $conversations;
}
// Fetches all of the messages in the given conversation.
function fetch_conversation_messages($conversation_id) {
$conversation_id = (int)$conversation_id;
$sql = "SELECT
`conversations_messages`.`message_date`,
`conversations_messages`.`message_date` > `conversations_members`.`conversation_last_view` AS `message_unread`,
`conversations_messages`.`message_text`,
`users`.`username`
FROM `conversations_messages`
INNER JOIN `users` ON `conversations_messages`.`user_id` = `users`.`user_id`
INNER JOIN `conversations_members` ON `conversations_messages`.`conversation_id` = `conversations_members`.`conversation_id`
WHERE `conversations_messages`.`conversation_id` = {$conversation_id}
AND `conversations_members`.`user_id` = {$_SESSION['user_id']}
ORDER BY `conversations_messages`.`message_date` DESC";
$result = mysql_query($sql);
$messages = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$messages[] = array(
'date' => $row['message_date'],
'unread' => $row['message_unread'],
'text' => $row['message_text'],
'username' => $row['username']
);
}
return $messages;
}
// Sets the last view time to the current time for the given conversation.
function update_conversation_last_view($conversation_id) {
$conversation_id = (int)$conversation_id;
$sql ="UPDATE `conversations_members`
SET `conversation_last_view` = UNIX_TIMESTAMP()
WHERE `conversation_id` = {$conversation_id}
AND `user_id` = {$_SESSION['user_id']}";
mysql_query($sql);
}
// Creates a new conversation, making the given users a member.
function create_conversation($user_ids, $subject, $body) {
$subject = mysql_real_escape_string(htmlentities($subject));
$body = mysql_real_escape_string(htmlentities($body));
mysql_query("INSERT INTO `conversations` (`conversation_subject`) VALUES ('{$subject}')");
$conversation_id = mysql_insert_id();
$sql = "INSERT INTO `conversations_messages` (`conversation_id`, `user_id`, `message_date`, `message_text`)
VALUES ({$conversation_id}, {$_SESSION['user_id']}, UNIX_TIMESTAMP(), '{$body}')
";
mysql_query($sql);
$values = array("({$conversation_id}, {$_SESSION['user_id']}, UNIX_TIMESTAMP(), 0)");
foreach ($user_ids as $user_id) {
$user_id = (int) $user_id;
$values[] = "({$conversation_id}, {$user_id}, 0, 0)";
}
$sql = "INSERT INTO `conversations_members` (`conversation_id`, `user_id`, `conversation_last_view`, `conversation_deleted`)
VALUES " . implode(", ", $values);
mysql_query($sql);
}
// Checks to see if the given user is a number of the given conversation.
function validate_conversation_id($conversation_id) {
$conversation_id = (int)$conversation_id;
$sql = "SELECT COUNT(1)
FROM `conversations_members`
WHERE `conversation_id` = {$conversation_id}
AND `user_id` = {$_SESSION['user_id']}
AND `conversation_deleted` = 0";
$result = mysql_query($sql);
return (mysql_result($result, 0) == 1);
}
// Adds a message to the given conversation.
function add_conversation_message($conversation_id, $text) {
$conversation_id = (int)$conversation_id;
$text = mysql_real_escape_string(htmlentities($text));
$sql = "INSERT INTO `conversations_messages` (`conversation_id`, `user_id`, `message_date`, `message_text`)
VALUES ({$conversation_id}, {$_SESSION['user_id']}, UNIX_TIMESTAMP(), '{$text}')";
mysql_query($sql);
}
// Deletes (or marks as deleted) a given conversation.
function delete_conversation($conversation_id) {
$conversation_id = (int)$conversation_id;
$sql = "SELECT DISTINCT `conversation_deleted`
FROM `conversations_members`
WHERE `user_id` != {$_SESSION['user_id']}
AND `conversation_id` = {$conversation_id}";
$result = mysql_query($sql);
if (mysql_num_rows($result) === 1 && mysql_result($result, 0) == 1) {
mysql_query("DELETE FROM `conversations` WHERE `conversation_id` = {$conversation_id}");
mysql_query("DELETE FROM `conversations_members` WHERE `conversation_id` = {$conversation_id}");
mysql_query("DELETE FROM `conversations_messages` WHERE `conversation_id` = {$conversation_id}");
} else {
$sql = "UPDATE `conversations_members`
SET `conversation_deleted` = 1
WHERE `conversation_id` = {$conversation_id}
AND `user_id` = {$_SESSION['user_id']}";
mysql_query($sql);
}
}
?>[/syntax]
view_conversation.php
[syntax=php]
<?php
include'core/init.php';
protect_page();
include 'includes/overall/header.php';
?>
<section class="section">
<div class="content_box"><h2>Messages</h2></div>
<div class="inner_content">
<p class="section_subname">View conversation</p>
<div id="global_data">
<div>
<a href="messages.php"> Messages</a>
</div>
<?php
$errors = array();
$valid_conversation = (isset($_GET['conversation_id']) &&
validate_conversation_id($_GET['conversation_id']));
if ($valid_conversation === false) {
$errors[] = 'Invalid conversation ID.';
}
if (isset($_POST['message'])) {
if (empty($_POST['message'])) {
$errors[] = 'You must enter a message.';
}
if (empty($errors)) {
add_conversation_message($_GET['conversation_id'], $_GET['message']);
}
}
if (empty($errors) === false) {
foreach($errors as $error) {
echo '<div class"msg_error">', $error, '</div>';
}
}
if ($valid_conversation) {
if (isset($_POST['message'])) {
update_conversation_last_view($_GET['conversation_id']);
$messages = fetch_conversation_messages($_GET['conversation_id']);
} else {
$messages = fetch_conversation_messages($_GET['conversation_id']);
update_conversation_last_view($_GET['conversation_id']);
}
?>
<form action="" method="post">
<div>
<textarea name="message"rows="10" cols="70"></textarea>
</div>
<div>
<input type="submit" value="Add Message">
</div>
</form><br>
<div class="messages">
<?php
foreach ($messages as $message) {
?>
<div class="message <?php if ($message['unread']) echo 'unread'; ?>">
<p class="name"><?php echo $message['username']; ?> (<?php echo date('d/m/Y H:i:s', $message['date']); ?>)</p>
<p class="text"><?php echo $message['text']; ?></p>
</div><br>
<?php
}
?>
</div>
<?php
}
?>
</div>
</div>
</section>
<?php include 'includes/overall/footer.php';?>
[/syntax]
I point that problem could be in view_conversation.php but still haven't seen anything strange..
------------------------------------------------------------------------------------------------------------------------------------------------------
Second thing, with deleting conversations.
Function for deleting conversation is fully functional, but for anchor tag (where's delete conversation),
isn't working, any idea ?
[syntax=php]
<a href="message.php?delete_conversation=<?php echo $conversation['id']; ?>">[x]</a>
[/syntax]
[syntax=php]
<div class="conversation <?php if ($conversation['unread_messages']) echo 'unread'?>">
<h3>
<a href="message.php?delete_conversation=<?php echo $conversation['id']; ?>">[x]</a>
<a href="view_conversation.php?conversation_id=<?php echo $conversation['id']; ?>"><?php echo $conversation['subject']; ?></a>
</h3>
<p>Last Reply: <?php echo date('d/m/y H:i:s', $conversation['last_reply']); ?></p>
</div>
[/syntax]