2011-11-01 38 views
2

我正在展位預訂系​​統上工作。客戶可以選擇展位和預訂日期。任何有效的方法來循環數據庫數據以單行顯示相同的字段?

enter image description here

每個選定複選框將在預訂ID存儲按下按鈕後預訂(等:3選中複選框= 3預訂ID)。

當我顯示所有預訂時,它會顯示所有的展位和一天。如果我預訂了3天的同一展位,它將分成3排展示。 enter image description here

我想怎麼修改我的編碼,這樣我可以顯示在單行同一展臺,其對應的天,如下所示: enter image description here

這裏是我的編碼:

<?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>"; 
    } 

     } 
    ?> 

回答

1

您的查詢需要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子句中使用的字段。

在您的查詢中,您正在使用隱式連接,這不如使用顯式連接好。在不知道表結構的情況下,我無法將查詢編輯爲顯式連接,但您應該查看並自行轉換。它會爲您節省很多頭痛的問題。

+0

我正要用一個子選擇來回答這個問題,但這樣更有效率! –

+0

@Tim「GROUP BY」和「GROUP_CONCAT()」確實是非常有用的工具。然而,要適度使用。 – Bojangles

+0

謝謝,它的工作原理! – mcheng

相關問題