Page 1 of 1

Private message system

Posted: Fri Feb 29, 2008 6:36 pm
by Zavin
I am having a little problem with my pms. For example if I type:
Line 1
Line 2
It comes out:
Line 1 Line 2
I have looked at the code over and over and I'm probably over looking the obvious. Could someone look at my code and tell me what I am over looking? Below is the code for the 2 files I use for the message system.
pms.php

Code: Select all

<?
 
include 'header.php';
 
 
 
if ($_GET['delete'] != ""){
 
  $deletemsg = $_GET['delete'];
 
  $result = mysql_query("DELETE FROM `pms` WHERE `id`='".$deletemsg."'");
 
  echo Message("Message Deleted!");
 
}
 
 
 
if ($_GET['deleteall'] == "true"){
 
  $result = mysql_query("DELETE FROM `pms` WHERE `to`='".$user_class->username."'");
 
  echo Message("Message Deleted!");
 
}
 
 
 
 
 
if ($_POST['newmessage'] != ""){
 
  $to = $_POST['to'];
 
  $from = $user_class->id;
 
  $timesent = time();
 
  $subject = strip_tags($_POST['subject']);
 
  $msgtext = strip_tags($_POST['msgtext']);
 
 
 
  $checkuser = mysql_query("SELECT `username` FROM `grpgusers` WHERE `username`='".$to."'");
 
  $username_exist = mysql_num_rows($checkuser);
 
    if($username_exist > 0){
 
      $result= mysql_query("INSERT INTO `pms` (`to`, `from`, `timesent`, `subject`, `msgtext`)".
 
      "VALUES ('$to', '$from', '$timesent', '$subject', '$msgtext')");
 
      echo Message("Message successfully sent to $to");
 
    } else {
 
      echo Message('I am sorry but the Username you specified does not exist...');
 
    }
 
 
 
}
 
?>
 
<tr><td class="contenthead">Mailbox</td></tr>
 
<tr><td class="contentcontent">
 
<table width='100%'>
 
                        <tr>
 
                            <td colspan='25%'>Time Recieved</td>
 
                            <td width='25%'>Subject</td>
 
                            <td width='25%'>From</td>
                            
                            <td width='25%'>Viewed</td>
 
                            <td>Delete</td>
 
                        </tr>
 
<?
 
$result = mysql_query("SELECT * from `pms` ORDER BY `timesent` DESC");
 
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
 
    if (strtoupper($row['to']) == strtoupper($user_class->username)) {
 
     $from_user_class = new User($row['from']);
 
     $subject = ($row['subject'] == "") ? "No Subject" : $row['subject'];
     
     if ($row['viewed']=="1"){
         $viewed="No";
     }else{
         $viewed="Yes";
     }
 
    echo "
 
                        <tr>
 
                            <td colspan='25%'>".date(F." ".d.", ".Y." ".g.":".i.":".sa,$row['timesent'])."</td>
 
                            <td width='25%'><a href='viewpm.php?id=".$row['id']."'>".$subject."</a></td>
 
                            <td width='25%'>".$from_user_class->formattedname."</td>
                            
                            <td width='25%'>".$viewed."</td>
 
                            <td><a href='pms.php?delete=".$row['id']."'>Delete</a></td>
 
                        </tr>
 
    
 
";
 
}
 
}
 
if ($_GET['reply'] != ""){
 
    $result2 = mysql_query("SELECT * from `pms` WHERE `id`='".$_GET['reply']."'");
 
    $worked2 = mysql_fetch_array($result2);
 
    $from_user_class = new User($worked2['from']);
 
}
 
?>
 
<br /><center><a href='pms.php?deleteall=true'>Delete All PMs In Your Inbox</a></center>
 
</table>
 
</td></tr>
 
<tr><td class="contenthead">New Message</td></tr>
 
<tr><td class="contentcontent">
 
            <table width='100%'>
 
                <form method='post'>
 
                <tr>
 
 
 
                    <td width='15%'>Send To:</td>
 
                    <td width='85%'><input type='text' name='to' value='' size='10' maxlength='75'> [username]
 
                </tr>
 
                <tr>
 
 
 
                    <td width='15%'>Subject:</td>
 
                    <td width='85%'><input type='text' name='subject' size='70' maxlength='75' value='<? echo ($_GET['reply'] != "") ? "Re: ".$worked2['subject'] : "";  ?>'></td>
 
                </tr>
 
                <tr>
 
                    <td width='15%'>Message:</td>
 
                    <td width='85%' colspan='3'><textarea name='msgtext' cols='53' rows='7'></textarea></td>
 
                </tr>
 
 
 
                <tr>
 
                    <td width='100%' colspan='4' align='center'><input type='submit' name='newmessage' value='Send'></td>
 
                </tr>
 
                </form>
 
            </table>
 
</td></tr>
 
<?
 
 
 
include 'footer.php';
 
?>
viewpm.php

Code: Select all

<?
include 'header.php';
?>
<tr><td class="contenthead">Mailbox</td></tr>
<tr><td class="contentcontent">
<table width='100%'>
<?
$result = mysql_query("SELECT * from `pms` WHERE `id`='".$_GET['id']."'");
$row = mysql_fetch_array($result);
     $from_user_class = new User($row['from']);
if ($_GET['id'] != ""){
    if (strtoupper($row['to']) == strtoupper($user_class->username)) {
    echo "
                        <tr>
                            <td width='15%'>Subject:</td>
                            <td width='45%'>".$row['subject']."</td>
                            <td width='15%'>Sender:</td>
 
                            <td width='25%'>".$from_user_class->formattedname."</td>
                        </tr>
                        <tr>
                            <td>Recieved:</td>
                            <td colspan='3'>".date(F." ".d.", ".Y." ".g.":".i.":".sa,$row['timesent'])."</td>
                        </tr>
                        <tr>
 
                            <td class='textm'>Message:<br>".wordwrap($row['msgtext'])."
                            </td>
                        </tr>
                        <tr>
                        <td colspan='4' align='center'><a href='pms.php?delete=".$row['id']."'>Delete</a> | <a href='pmsreply.php?reply=".$row['id']."'>Reply</a></td>
                        </tr>
                        <tr>
                            <td colspan='4' align='center'><a href='pms.php'>Back To Mailbox</a></td>
                        </tr>
 
";
    $result2 = mysql_query("UPDATE `pms` SET `viewed` = '2' WHERE `id`='".$row['id']."'");
    }
}
?>
 
</table>
</td></tr>
<?
include 'footer.php';
?>

Re: Private message system

Posted: Fri Feb 29, 2008 8:22 pm
by Sekka
I think on line 27 of viewpm.php you need to run nl2br() on the private message to convert the new lines to <br>'s.

Re: Private message system

Posted: Sat Mar 01, 2008 8:37 am
by Zavin
Thanks that got it. Now I have a new problem. I replaced .wordwrap() on line 27 of viewpm.php with .nl2br(). Now the message does an auto break after 15 characters.
If I type:
1 2 3 4 5 6 7 8 9 0
It comes out:
1 2 3 4 5 6 7 8
9 0

Re: Private message system

Posted: Sat Mar 01, 2008 8:45 am
by Sekka
You can do wordwrap() and nl2br().

Code: Select all

nl2br (wordwrap ($my_variable));

Re: Private message system

Posted: Sat Mar 01, 2008 9:42 am
by Zavin
I've tried line 27 of viewpm.php as

Code: Select all

<td class='textm'>Message:<br>".wordwrap(nl2br($row['msgtext']))."
and

Code: Select all

<td class='textm'>Message:<br>".nl2br(wordwrap($row['msgtext']))."
and I still get the same results.

Re: Private message system

Posted: Sat Mar 01, 2008 12:58 pm
by Sekka
The width of the table could be line breaking the text, but I am not sure.

nl2br() would certainly not line break unless there was one there in the first place.

Re: Private message system

Posted: Sat Mar 01, 2008 1:41 pm
by Zavin
Yep, problem is in the table. Thanks. Sometimes you get caught up in the php code and don't think to look at the easier stuff. :banghead: