[Solved!]PM System [Part 13]

Post here is you are having problems with any of the tutorials.
Post Reply
Z645
Posts: 33
Joined: Thu Jul 26, 2012 5:08 pm

[Solved!]PM System [Part 13]

Post by Z645 »

Hey guys. I've come into another problem, and now this time, I can't see anything. I tried the die(mysql_error()); function, but nothing happened. I ran through looking for errors and nothing. So here if my code if you wanna try and if you want to see it for yourself heres my site: Zirber Just sign up or something. But my code also:


view_conversation.page.inc.php
[syntax=php]<?php

$errors = array();

$valid_conversation = (isset($_GET['conversation_id']) && validate_conversation_id($_GET['conversation_id']));

if ($valid_conversation === false){
$errors[] = 'Invalid conversation ID.';
}

if (empty($errors) === false){
foreach ($errors as $error){
echo '<div class="msg_error">', $error, '</div>';
}
}

if ($valid_conversation){
$messages = fetch_conversation_messages($_GET['conversation_id']);

?>
<div class="actions">
<a href="index.php?page=inbox">Inbox</a>
<a href="index.php?page=logout">Logout</a>
</div>

<div class="messages">
<?php

foreach ($messages as $message){
?>
<div class="message <?php if ($message['unread']) echo 'unread'; ?>">
<p class="name"><?php echo $message['user_name']; ?> (<?php echo date('d/m/Y H:i:s', $message['date']); ?>)</p>
<p class="text"><?php echo $message['text']; ?></p>
</div>
<?php
}

?>
</div>
<?php
}[/syntax]


private_message.inc.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`.`user_name`
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'],
'user_name' => $row['user_name'],
);
}

return $messages;
}


// 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` (`conversations_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 member 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);

}

// Deletes 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]
Last edited by Z645 on Sun Sep 23, 2012 2:21 am, edited 2 times in total.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: PM System [Part 13]

Post by Temor »

die() kills the script, preventing it from running any more code. Change die() to echo.
Z645
Posts: 33
Joined: Thu Jul 26, 2012 5:08 pm

Re: PM System [Part 13]

Post by Z645 »

Temor wrote:die() kills the script, preventing it from running any more code. Change die() to echo.

I have tried echo(mysql_error()); and echo mysql_error(); Which ever one it is D:
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: PM System [Part 13]

Post by Temor »

So, what is it that you're not able to see? Is the page white-blank or is it just certain parts that are missing?
Z645
Posts: 33
Joined: Thu Jul 26, 2012 5:08 pm

Re: PM System [Part 13]

Post by Z645 »

Temor wrote:So, what is it that you're not able to see? Is the page white-blank or is it just certain parts that are missing?

I can't see the mail part, as in the Subject area and time and such in view_conversation.page.php. I can see the links like Inbox and Logout at the top though.
flobo
Posts: 2
Joined: Thu Oct 11, 2012 8:24 pm

Re: [Solved!]PM System [Part 13]

Post by flobo »

If im correct I had the same issue its a simple typo problem ...

in private_message.inc.php find this line:
[syntax=php]$sql = "INSERT INTO `conversations_messages` (`conversations_id`, `user_id`, `message_date`, `message_text`)[/syntax]

and change ...

[syntax=text]conversations_id[/syntax]

to

[syntax=text]conversation_id[/syntax]

this how ever only fixed it so I could see 1st message, all others (replies) i still can not view
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: [Solved!]PM System [Part 13]

Post by Helx »

That kind of stuff is interchangeable, for example some people may call their database 'test'.
Others may call it 'private_messages'.

It really depends on how closely you want to follow tutorials :)

I generally would use the tutorials as a starting point, I don't think I have ever copied any one of them word-for-word.
Post Reply