<?php
include 'preCode.php';
include 'header.php';
echo '<body><div class="standardLayout">';
include 'systemMenu.php';
echo '<h4>All Charges</h4>';
$user = unserialize($_SESSION['user']);
$query = "SELECT * FROM billingItems WHERE userID= ' ".$user->userID. " ' ORDER BY deliveryTimestamp DESC";
$result = mysqli_query($db, $query);
while($row = mysqli_fetch_array($result))
{
echo '<p>';
echo '<a href="billingHistory1.php?deliveryTimestamp=' .$row["deliveryTimestamp"]. '">'.
' Order Delivered on' . '</a>' .$row['deliveryTimestamp'] ;
}
echo '</div></body></html>';
$_SESSION['user'] = serialize($user);
include 'footer.html';
?>
上述文件的输出:
All Charges
Order Delivered on2015-05-06 13:26:50
Order Delivered on2015-05-06 13:26:50
Order Delivered on2015-05-06 13:26:50
Order Delivered on2015-05-03 22:11:23
Order Delivered on2015-05-03 22:11:23
Order Delivered on2015-05-03 22:11:23
Order Delivered on2015-05-03 22:11:23
如果您将看到输出,那么您会注意到前三个链接相同,后四个相同。
我需要将多个链接减少到一个。
只想为每个链接显示两个链接。
为此,我需要在while循环中使用if语句。如果deliveredTimestamp
相同,则不显示它,否则显示它。请帮忙。
答案 0 :(得分:3)
为什么不尝试将唯一的deliveryTimestamps添加到数组中,然后从那些链接中呈现链接?
$timestamps = array();
while($row = mysqli_fetch_array($result)){
if ( !in_array($row['deliveryTimestamp'], $timestamps)) {
array_push($timestamps, $row['deliveryTimestamp']);
}
}
foreach ($timestamps as $timestamp) {
echo '<p>';
echo '<a href="billingHistory1.php?deliveryTimestamp='.$timestamp.'">'.
' Order Delivered on' . '</a>' .$timestamp ;
echo '</p>';
}
答案 1 :(得分:0)
您可以使用分组依据来减少记录数量
使用此查询
$query = "SELECT * FROM billingItems
WHERE userID= ' ".$user->userID. " '
GROUP BY deliveryTimestamp
ORDER BY deliveryTimestamp DESC";