[英]How to display date at new posts in log
我正在构建一个应用程序,其中一部分是对话日志,类似于您在whatsapp中查看消息的方式。 我面临的问题是系统无法适应不同的日期,也无法按以下顺序展示日期:
2018-03-16
项目1
item2
2018-03-11
项目3
项目4
我正在使用smarty将数据对象拉到屏幕上:
后端
// Fetch info about the support ticket's conversations
$query = "
SELECT
conversation_content,
posted_at,
is_internal,
IF(emp_id IS NULL, 0, 1) AS belongsToEmployee,
EmployeeTBL.EmpFirstname,
EmployeeTBL.EmpInsertion,
EmployeeTBL.EmpLastname
FROM
ticket_conversation
INNER JOIN tickets ON ticket_conversation.ticket_id=tickets.id
LEFT JOIN EmployeeTBL ON ticket_conversation.emp_id=EmployeeTBL.EmpID
WHERE
ticket_conversation.ticket_id = :id
ORDER BY
ticket_conversation.posted_at
DESC
";
$binds = array(':id' => $_GET['id']);
$ticketConversationList = $db->select($query, $binds);
// Set date container
$ticketDates = [];
// Loop through all the tickets and add their date as a ticket entry
foreach($ticketConversationList as $ticket){
$ticketDate = date('d-m-Y',strtotime($ticket['posted_at']));
$ticketDates[$ticketDate][] = $ticket;
}
//var_dump($ticketDates);
// Count all conversations
$conversationCount = count($ticketConversationList);
// Assign the variables
$smarty->assign("ticketDates", $ticketDates);
$smarty->assign("ticketConversationList", $ticketConversationList);
$smarty->assign("conversationCount", $conversationCount);
在前端,我正在使用提供的变量来遍历对象数组。
前端
<div class="form-group">
<label>{$ticketChatHistoryLabel}</label>
<hr style="border-color: transparent;">
{foreach from=$ticketConversationList key=index item=ticketConversation}
{if $index != 0}
{$index = $index - 1}
{/if}
{$dateOfConversation = date('d-m-Y',strtotime($ticketConversation.posted_at))}
{$dateOfPreviousConversation = date('d-m-Y',strtotime($ticketConversationList[$index].posted_at))}
{$dateOfConversation = date('d-m-Y',strtotime($ticketConversation.posted_at))}
{$timeOfConversation = date('H:i',strtotime($ticketConversation.posted_at))}
{if $dateOfPreviousConversation < $dateOfConversation || $index == 0}
<div>
<div class="btn btn-default" style="margin-left: 44%; margin-bottom: 20px;">{$dateOfConversation}</div>
</div>
{/if}
<div class="holder
{if $ticketConversation.belongsToEmployee == 1}
pull-right
{else}pull-left
{/if}" style="border-radius: 0; border: 1px solid; padding: 5px; width: 90%;
{if $ticketConversation.belongsToEmployee != 0 && $ticketConversation.is_internal == 0}
background-color: #c0eed5;
{elseif $ticketConversation.belongsToEmployee != 0 && $ticketConversation.is_internal == 1}
background-color: #eeeeee;
{/if}">
{$ticketConversation.conversation_content}
<i class="{if $ticketConversation.belongsToEmployee}
pull-right
{else}pull-left
{/if}" style="color: #999999">
{$ticketHistoryPostedAt}{$dateOfConversation} {$atLabel} {$timeOfConversation} {$hoursLabel}
</i>
{if $ticketConversation.belongsToEmployee}
<i class="pull-left" style="color: #999999">
{$ticketHistoryPostAuthor}{$ticketConversation.EmpFirstname} {$ticketConversation.EmpInsertion} {$ticketConversation.EmpLastname}
</i>
{/if}
</div>
<div class="clearfix"></div>
{/foreach}
<div>
<h4 style="text-align: center; color: #afafaf;">- {$endOfTicketConversation} -</h4>
</div>
</div>
目前,这是输出:
然后,我切换
{if $index != 0}
{$index = $index - 1}
{/if}
我将绿色响应数据设置为2018-03-15(自发布之日起的昨天)。 结果如下:
解决
由于我们为-1,因此需要伪造0张支票
{foreach from = $ ticketConversationList key = index item = ticketConversation}
{$fake = $index != 0}
{if $index != 0}
{$index = $index - 1}
{/if}
{$dateOfConversation = date('d-m-Y',strtotime($ticketConversation.posted_at))}
{$dateOfPreviousConversation = date('d-m-Y',strtotime($ticketConversationList[$index].posted_at))}
{$dateOfConversation = date('d-m-Y',strtotime($ticketConversation.posted_at))}
{$timeOfConversation = date('H:i',strtotime($ticketConversation.posted_at))}
{if (( $index == 0 && !$fake )|| $dateOfPreviousConversation != $dateOfConversation) }
<div>
<div class="btn btn-default" style="margin-left: 44%; margin-bottom: 20px;">{$dateOfConversation}</div>
</div>
{/if}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.