データベースにイベントと有効期限が保存されているテーブルがあります。私は何をしたいのですか?私のウェブサイト上のすべてのイベントを表示するが、現在のイベントを最初に表示してから、期限切れのイベントを表示する。正しい順序でmysqliデータベースの結果が表示されない
私は、次のSQL
$sql = "SELECT noticeID, notice, noticeDesc, noticeIMG, noticeDate,
expiryDate FROM tbl_notices GROUP BY noticeID ,expired ORDER BY
expiryDate ASC";
を試してみましたが、それが原因私のクエリでexpiryDate注文されたASCに最初の期限切れの結果を返します。この
noticeID => int(4)
notice => VARCHAR(100)
noticeDesc => text
noticeDate => timestamp
noticeIMG => VARCHAR(40)
expiryDate => datetime
expired => int(1) 0 for current or 1 for expired
私のPHPコードが
$sql = "SELECT noticeID, notice, noticeDesc, noticeIMG, noticeDate,
expiryDate FROM tbl_notices GROUP BY noticeID ,expired ORDER BY expiryDate ASC";
$result = mysqli_query($conn,$sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$id = $row['noticeID'];
$title = htmlspecialchars($row['notice']);
$urltitle = strtolower($title);
$urltitle = str_replace(" ","-",$urltitle);
$date = $row['noticeDate'];
$datetime1 = new DateTime("2010-06-20");
$expiryDate = $row['expiryDate'];
$link = "announcements.php";
$body = strip_tags($row['noticeDesc']);
$filename = $row['noticeIMG'];
if($filename != "")
{
$imgLink = "images/".$filename;
}
else
{
$imgLink = "images/announcement-default.jpg";
}?>
<div class="news-container-sm">
<img class="pin" src="images/thumbtack_pushpin_2_thumb.png" alt="News Pin" title="News Pin">
<div class="news-img"><img class="fluid" src="<?php echo $imgLink?>" alt="<?php echo $title?>" title="<?php echo $title?>" /></div>
<div class="fluid news-headline"><?php echo $title?>
<span class="fluid news-date"><?php echo "expires in ".humanTiming(strtotime($expiryDate));?></span>
</div>
<div class="fluid news-desc"><?php echo $body;?></div>
</div><?php
}
}
else
{
echo "No Announcements!";
}
$conn->close();?>
あるよう
マイtbl_notice構造が見えますが、私は期限切れのレコードを返すことを言うように最初の昇順にソートされてexpiryDateは、最初になるので期限切れのレコードを最後までどのようにプッシュしますか?
は、効果的にすべての現在の通知をソートして表示し、期限切れの通知を並べ替えて表示します。これが意味クエリの 任意のヘルプが
多くのおかげ ルーク
最初に 'expired'フィールドで注文します:' ORDER BY は期限切れ、expiryDate ASC'です。その後、期限切れではない0の値は期限切れレコードの前になります。 – jeroen
ありがとうございました! – BBLJ84