0
Hello, I’m developing a messaging app (chat) and it’s almost finished and working.
It turns out that the messages are all aligned from left to right, regardless of the message user.
Example:
User 1 Mensagem
Usuario 2 Mensagem
User 1 Mensagem
Usuario 2 Mensagem
I intend to change this and position user message 1 left and position user message 2 right. (Whastapp style).
User 1 Mensagem
Mensagem
Usuario 2
User 1 Mensagem
Mensagem
Usuario 2
Below is the code of how I’m retrieving the message.
<?php
$this->db->select('tickets_historico.*,usuarios.nome_usuario');
$this->db->join('usuarios','usuarios.id = tickets_historico.usuario_id', 'left');
$this->db->where('ticket_id = 1');
$this->db->order_by('dt_cadastro','desc');
$historico = $this->db->get('tickets_historico')->result_array()
?>
Below is the code of how I am displaying the message.
<div class="direct-chat-msg">
<?php //Se houver comentários, imprime os comentários
if(count($historico) > 0)
{ foreach ($historico as $row)
{?>
<?php $id = $row['usuario_id']; ?>
<?php $image_url = base_url() . 'upload/imagens_usuarios' . '/' . $id . "_thumbnail" . '.jpg'; ?>
<div class="direct-chat-info clearfix">
<span class="direct-chat-name pull-left"><?=$row['nome_usuario']?></span>
<span class="direct-chat-timestamp pull-right"><?= date('d/m/Y h:i A',strtotime($row['dt_cadastro']))?></span>
</div>
<img class="direct-chat-img" src="<?php echo $image_url; ?>" alt="message user image">
<div class="direct-chat-text">
<?=$row['mensagem'];?>
</div><br>
<?php }
}
else //Quando não há nenhum comentário
{
echo "<p>Atualmente, não há comentários.</p>";
}
?>
</div>
Summarizing the code above:
if(count($historico) > 0)
{ foreach ($historico as $row)
{?>
<p><strong><?=$row['nome_usuario']?></strong> Disse em <?= date('d/m/Y h:i A',strtotime($row['dt_cadastro']))?><br>
<?=$row['mensagem'];?></p><hr>
<?php }
}
else //Quando não há nenhum comentário
{
echo "<p>Atualmente, não há comentários.</p>";
}
?>
OFF
: Just a hint: don’t do that<?php }
for God’s sake. When to mix yourPHP
withHTML
use<?php foreach($do as $it): ?>
to open and<?php endforeach; ?>
to close, the same goes forif
and anything else that needs to open and close.– William Novak
Obrigad @Williamnovak, annotated tip
– Wagner Fillio
Opinion Award of the Year for @Williamnovak... And why "for God’s sake"? D
– ShutUpMagda
Looks like work for CSS, @Wagnerfilho. Why not try something like Like Hangout Chat?
– ShutUpMagda
@Shutupmagda, this, CSS, I’m trying this way:
<?php $class_nome = ($this->session->userdata('id') == $row['usuario_id']) ? "direct-chat-nome esquerda" : "direct-chat-nome direita";?>
– Wagner Fillio
<span class="<?php echo $class_nome;?>"><?=$row['nome_usuario']?></span>
– Wagner Fillio