Reputation: 4045
Im writing a small private message script in PHP
Currently i have a foreach loop to loop through all the messages between 2 users.
I am upgrading the system so that one users messages are right aligned and the other users messages are left aligned... but im unsure of how to accomplish this.
At the moment i have this ( example )
<li>I am user 1's message</li>
<li>I am another of user 1's message</li>
<li>I am user 2's message</li>
<li>user 1's message</li>
<li>user 2</li>
<li>user 2</li>
and so on and so forth.
What i want to do is add a css class 'class="one"' to all of user ones messages and then 'class="two"' to all of user twos messages, so it looks like the following
<li class="one">I am user 1's message</li>
<li class="one">I am another of user 1's message</li>
<li class="two">I am user 2's message</li>
<li class="one">user 1's message</li>
<li class="two">user 2</li>
<li class="two">user 2</li>
The messages are in random order and the ID's wont always be one and two, but for the moment there will only be 2 people that messages go to.
The PHP i currently have is
foreach ($messages as $message) {
echo '<li>' . $message->reply . '</li>';
}
Any help would be greatly appreciated.
Cheers,
Upvotes: 0
Views: 66
Reputation: 1794
i hope it will work
$user_id = 'the unique UID of user one or user two';
foreach( $messages as $message ) {
echo '<li class="'.( ( $user_id == $message->user_id_fk ) ? 'one' : 'two' ).'">'.$message->reply.'</li>';
}
Upvotes: 2
Reputation: 6052
So when you add a new message to the list, keep track of the source, and add the css class accordingly.
If this is the message you are going to send to the server, so this is user 1, so class="one", if you got this message from the server, so this is user2, so class="two"
Upvotes: 0