0

I am trying to create a group chat/message system in PHP and HTML. It has been turning out pretty good, but i wanted to change the system to make it more detailed.

I have been trying to make it for that it displays messages made by other users one way, and messages made by you another, kind of like iMessage for the iPhone.

Here is my PHP code...

<div class="groupchat span9" style="height: 200px; overflow: auto;">

<?php

    /*
    * LETS GET THESE MESSAGES PRINTED
    */

    $qry=mysql_query("SELECT * FROM chat ORDER BY chat.id ASC", $con);

    while($row=mysql_fetch_array($qry)) {

        if(!$row['name']) {
            echo '<div class="span5 place-left">';
                echo '<ul class="replies">';
                    echo '<li class="bg-color-orange">';
                        echo '<b class="sticker sticker-left sticker-color-orange"></b>';
                        echo '<div class="avatar"><img src="../content/images/myface.jpg" /></div>';
                        echo '<div class="reply">';
                            echo '<div class="date">'.$row['time'].'</div>';
                            echo '<div class="author">'.$row['name'].'</div>';
                            echo '<div class="text">'.$row['message'].'</div>';
                        echo '</div>';
                    echo '</li>';
                echo '</ul>';
            echo '</div>';
        }

        if($row['name'] == $_SESSION['name']) {
          echo '<div class="span5 place-right">';
                echo '<ul class="replies">';
                    echo '<li class="bg-color-blue">';
                        echo '<b class="sticker sticker-right sticker-color-blue"></b>';
                        echo '<div class="avatar"><img src="../content/images/myface.jpg" /></div>';
                        echo '<div class="reply">';
                            echo '<div class="date">'.$row['time'].'</div>';
                            echo '<div class="author">'.$row['name'].'</div>';
                            echo '<div class="text">'.$row['message'].'</div>';
                        echo '</div>';
                    echo '</li>';
                echo '</ul>';
            echo '</div>';
        }

    }

?>

<a name="groupchatbottom"></a>

</div>

I'm having a problem trying to get it for that it will display messages that arn't made by you. I appreciate any help! Thank you all for your help in advance!

13
  • 1
    You know you can just do one echo open ' and put all the tags, and rows there, instead of writting echo echo 20 times. -- just a tip Commented Mar 31, 2013 at 19:34
  • Lol I know, I just do it like that for some reason. Commented Mar 31, 2013 at 19:35
  • You should really be using user IDs instead of names. Also, for the love of god, don't use mysql_query: stackoverflow.com/questions/12859942/… Commented Mar 31, 2013 at 19:35
  • 2
    @phpNoOb: Even worse idea. multi-line echoes are EVIL. Use a HEREDOC instead. And if none of the echoed text contains variables or other "dynamic" content, you might as well break out of PHP mode completely. Commented Mar 31, 2013 at 19:37
  • 1
    @TehJamDude: better than the inevitable "zomg I used the wrong quote style and have to escape everything" that invariably hits when you're using php to dump out html (or worse yet, html+javascript). Commented Mar 31, 2013 at 19:42

1 Answer 1

1

Just use an if-else statement:

if($row['name'] == $_SESSION['name']) {
    // post belongs to author
    echo '<div class="author-message">

    </div>';  
}
else{
    echo '<div class="user-message">

    </div>';    
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.