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!

FearGannicus
  • 205
  • 5
  • 17
  • 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 – samayo Mar 31 '13 at 19:34
  • Lol I know, I just do it like that for some reason. – FearGannicus Mar 31 '13 at 19:35
  • You should really be using user IDs instead of names. Also, for the love of god, don't use `mysql_query`: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – element119 Mar 31 '13 at 19:35
  • 2
    @phpNoOb: Even worse idea. multi-line echoes are EVIL. Use a [HEREDOC](http://php.net/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. – Marc B Mar 31 '13 at 19:37
  • or seperate logic from template – Royal Bg Mar 31 '13 at 19:38
  • @autibyte I'm not sure why? I taught myself how to use PHP and MYSQL so i don't really keep myself updated. What should i be using then? – FearGannicus Mar 31 '13 at 19:38
  • @autibyte you want to use `ID` as they are guaranteed to be unique - if you create them that way. Names can be ambiguous. – What have you tried Mar 31 '13 at 19:39
  • @MarcB That looks incredibly confusing :/ – FearGannicus Mar 31 '13 at 19:41
  • 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). – Marc B Mar 31 '13 at 19:42
  • @TehJamDude, read the referred question, it largely explains why **not** to use it and what to use **instead**. – Havelock Mar 31 '13 at 19:43
  • @MarcB I'll look over it and see if i can grasp it or not. I would accept the answer bellow, but it won't let me :/ – FearGannicus Mar 31 '13 at 19:44
  • @TehJamDude There's nothing confusing in breaking out of the PHP code as suggested and using plain HTML where you don't have any dynamic content... – Havelock Mar 31 '13 at 19:45
  • @TehJamDude: there's a delay on accepting answers (10? 15? minutes). give it a bit. – Marc B Mar 31 '13 at 19:46

1 Answers1

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>';    
}
What have you tried
  • 11,018
  • 4
  • 31
  • 45