[英]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.