簡體   English   中英

使用PHP將SQL Join語句放入HTML表中的單個字段

[英]SQL Join statement into a single field in a HTML Table using PHP

因此,我創建了一個日程安排系統,並嘗試顯示約會的信息,以及顯示已預訂的用戶的用戶ID。

我創建了一個針對Students,Classs和ClassAssosication的表,其中ClassID和UserID是其他表中的PK和FK。 我創建了一個連接語句=

SELECT classes.ClassName, students.UserID
    FROM classassociation
    JOIN students
        ON classassociation.UserID = Students.UserID
    JOIN classes
        ON classassociation.ClassID = classes.ClassID
    WHERE classassociation.ClassID = 1;

它在哪里為那些預訂了ID = 1的Class的人檢索UserID和ClassName。

我正在嘗試創建一個PHP / HTML表,在該表中的某個字段(例如,星期一9AM)中,我可以打印出連接語句,例如,Methodology,1,2(用戶ID)。

Day     |        9AM       | 10AM
Monday  | Methodology, 1,2 |
Tuesday |                  |

我需要為多個類完成此操作,但現在嘗試嘗試一個。 我不確定如何執行此操作,因此將不勝感激。 謝謝。

您編寫的查詢將為每個ClassName / UserId對返回一行-如果方法類中有兩個人,則查詢結果中將有兩行。

您可以選擇將它們組合成一個php循環,也可以更改查詢以將它們組合在一起。 如果您將其視為“我想將給定類名的所有UserId分組”,則建議如何在SQL中使用GROUP BY子句:

SELECT classes.ClassName, students.UserID
FROM classassociation
JOIN students
    ON classassociation.UserID = Students.UserID
JOIN classes
    ON classassociation.ClassID = classes.ClassID
WHERE classassociation.ClassID = 1
GROUP BY ClassName;

現在每個ClassName都有一行,但是您仍然需要告訴MYSQL如何處理每一行的多個UserId。 在您的情況下,您希望將UserIds以逗號分隔的列表形式連接在一起。 幸運的是,手冊中有一個特殊的部分專門針對您與GROUP BY使用的功能。 在這種情況下, GROUP_CONCAT會提供所需的結果:

SELECT classes.ClassName, GROUP_CONCAT(students.UserID SEPARATOR ',') AS UersIds
FROM classassociation
JOIN students
    ON classassociation.UserID = Students.UserID
JOIN classes
    ON classassociation.ClassID = classes.ClassID
WHERE classassociation.ClassID = 1
GROUP BY ClassName;

現在,您將獲得一個結果集,其中每個ClassName一行包含兩列:

ClassName   | UserIds
---------------------
Methodology | 1,2

然后,您可以編寫一個簡單的php循環,以獲取查詢中的每一行並在html中生成一個表行。

如果查看GROUP_CONCAT的手冊,您會發現您還可以設置userId的分組順序,這可能會很有用。

一旦獲得查詢結果,php循環就非常簡單。 您只需要創建一個表,然后為每個結果添加一行:

/*assume your mysql query has returned an array of objects named $result */

// in your html document, create your table with its header
print '<table><thead><tr><th>Class Name</th><th>UserIds</th></tr></thead>';
print '<tbody>';
// now loop through your query results and put stuff into the table
// of course you can monkey around with the table format and what goes in each cell
foreach($result as $row) {
    print '<tr><td>'.$row->ClassName.'</td><td>'.$row->UserIds.'</td></tr>';
}
print '</tbody></table>';

這是一個非常簡單的示例,但是希望可以使這一過程變得神秘。

您的特殊情況看起來查詢實際上是獲取表中一個單元格的內容。 您可以執行更宏大的查詢(按時間和星期幾進行分組),也可以執行少量查詢並將結果存儲在嵌套數組中,這些數組模擬您希望表的外觀。 html輸出在結構上是相同的,但是每個表單元格都有一個php循環。

要使每個單元格都有一個表顯示單獨的查詢結果,請考慮以下偽代碼:

$days = array('Monday', 'Tuesday',...'Friday');
$times = array('9', '10'...);

$weekResults = array();
foreach($days as $day) {
    $weekResults[$day] = array();
    foreach($times as $time) {
        $weekResults[$day][$time] = doQuery($day, $time);
    }
}

// now you have nested arrays with a result set for each table cell. 
// Rendering the table, you just use the same loops:

renderTableHeader(); // all the <table><thead> stuff
foreach($weekResults as $day) {
    print '<tr>'
    foreach($day as $time) {
        print '<td>';
        // output the data from your query like above
        // if the formatting is complex, you can even put another table inside the <td>.
        print '</td>';
    }
    print '</tr>';
}
renderTableFooter(); // close tbody and table tags

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM