我正在构建一个应用程序,其中一部分是对话日志,类似于你在whatsapp中看到消息的方式。我面临的问题是系统无法适应不同的日期,也不能按照以下顺序展示:
2018年3月16日
物品1
ITEM2
2018年3月11日
项目3
ITEM4
我正在利用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(昨天发布此消息时)。结果如下:
答案 0 :(得分:0)
<强>分辨强>
要求假0检查,因为我们<-1>
{foreach from = $ ticketConversationList key = index item = ticketConversation}
public static void main(String[] args) {
String s = "world";
System.out.println("hello "+ s != null ? s: "null");
}