![](/img/trans.png)
[英]Is there any way to set single row fetched database values in textbox in Codeigniter without using loop
[英]Any efficient way to loop database data to display in single row for same field?
我正在使用展位預訂系統。 客戶可以選擇展位和預訂日期。
按下“保留”按鈕后,每個選中的復選框將存儲在預訂ID中(例如:3個復選框= 3個預訂ID)。
當我顯示所有預訂時,它將顯示所有攤位和日期。 如果我預定同一個展位3天,它將分3行顯示。
我想如何修改編碼,以便可以在同一行中顯示相同的展位及其對應的日期,如下所示:
這是我的編碼:
<?php
include("dbconfig.php");
$query4 = "SELECT bookingID,eventinfo.eventTitle,boothAlias,testbook.bstatus,date,day, username FROM eventinfo, testbook WHERE username='$user' AND testbook.eventID = eventinfo.eventID order by date desc";
//$query4="select * from testbook, eventinfo where username= '$user' and testbook.'$event'==eventinfo.'$event' order by eventID asc";
$result4 = mysql_query($query4);
while($row=mysql_fetch_array($result4))
{
$booth=stripslashes($row["boothAlias"]);
$day=stripslashes($row["day"]);
$event= stripslashes($row["eventTitle"]);
$date=stripslashes($row["date"]);
$bstatus=stripslashes($row["bstatus"]);
if ($bstatus==0){
echo "<tr class='record'>";
echo "<td class='reg' width='80'>$booth</td>";
echo "<td class='reg' width='80'>$day</td>";
echo "<td class='reg' width='180'>$event</td>";
echo "<td class='reg' width='150'>$date</td>";
echo "<td class='reg' width='70'><img src='icon_stop.gif'/></td>";
echo "<td class='reg' width='30'><a id='$row[bookingID]' style='color:#ff0000;' class='delbutton' href='#'><img src='icon_delete.gif' border='0'></a></td></tr>";
}else{
echo "<tr class='record'>";
echo "<td class='reg' width='80'>$booth</td>";
echo "<td class='reg' width='80'>$day</td>";
echo "<td class='reg' width='180'>$event</td>";
echo "<td class='reg' width='150'>$date</td>";
echo "<td class='reg' width='70'><img src='icon.approve.gif'/></td>";
echo "<td class='reg' width='30'><a id='$row[bookingID]' style='color:#ff0000;' class='delbutton' href='#'><img src='icon_delete.gif' border='0'></a></td></tr>";
}
}
?>
您的查詢需要GROUP BY
子句,以及GROUP_CONCAT()
聚合函數:
SELECT
bookingID,
eventinfo.eventTitle,
boothAlias,
testbook.bstatus,
date,
GROUP_CONCAT(day SEPARATOR ', ') AS day,
username
FROM eventinfo, testbook
WHERE username='$user'
AND testbook.eventID = eventinfo.eventID
GROUP BY booths, date
ORDER BY date desc
這未經測試,但是應該做您想要的。 如果結果不正確,請更改在GROUP BY
子句中使用的字段。
在您的查詢中,您使用的是隱式連接 ,不如使用顯式連接好。 在不知道您的表結構的情況下,我無法將查詢編輯為顯式聯接,但是您應該查詢並自行進行轉換。 使用較大的查詢將為您省去很多麻煩。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.