简体   繁体   English

使用PHP将SQL Join语句放入HTML表中的单个字段

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

so i'm creating a schedule system and i'm trying to display the information for the appointment and also, userID's for those who have booked a possition. 因此,我创建了一个日程安排系统,并尝试显示约会的信息,以及显示已预订的用户的用户ID。

I have created a table for Students, Classes and ClassAssosication where the ClassID and UserID are PK And FK from the other tables. 我创建了一个针对Students,Classs和ClassAssosication的表,其中ClassID和UserID是其他表中的PK和FK。 I have created a join statement = 我创建了一个连接语句=

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;

Where it retrieves the UserID and ClassName for those who have booked a place for Class with the ID = 1 . 它在哪里为那些预订了ID = 1的Class的人检索UserID和ClassName。

I am trying to create a PHP/HTML table where in a field for example, Monday 9AM, i can print out the join statement, for example, Methodology, 1 ,2 (user Id's). 我正在尝试创建一个PHP / HTML表,在该表中的某个字段(例如,星期一9AM)中,我可以打印出连接语句,例如,Methodology,1,2(用户ID)。

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

I need this done for multiple classes but trying to attempt one for now. 我需要为多个类完成此操作,但现在尝试尝试一个。 I am unsure of how to do this, so any help will be appreciated. 我不确定如何执行此操作,因此将不胜感激。 Thank you. 谢谢。

Your query as written will return a row for each ClassName/UserId pair - if there are two people in the methodology class there will be two rows in your query result. 您编写的查询将为每个ClassName / UserId对返回一行-如果方法类中有两个人,则查询结果中将有两行。

You can choose to combine them in a loop in php, or you can alter your query to group things together. 您可以选择将它们组合成一个php循环,也可以更改查询以将它们组合在一起。 If you think of it as "I want to group all the UserIds for a given classname", it suggest how to use the GROUP BY clause in your SQL: 如果您将其视为“我想将给定类名的所有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;

Now there will be one row for each ClassName, but you still need to tell MYSQL how to handle the multiple UserIds for each row. 现在每个ClassName都有一行,但是您仍然需要告诉MYSQL如何处理每一行的多个UserId。 In your case, you want to have the UserIds joined together in a comma-separated list. 在您的情况下,您希望将UserIds以逗号分隔的列表形式连接在一起。 Happily, there's a special section in the manual just for the functions you use with GROUP BY . 幸运的是,手册中有一个特殊的部分专门针对您与GROUP BY使用的功能。 In this case GROUP_CONCAT gives the desired result: 在这种情况下, 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;

Now you will get a result set with one row for each ClassName , with two columns: 现在,您将获得一个结果集,其中每个ClassName一行包含两列:

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

Then you can write a simple php loop to take each row in the query and generate a table row in your html. 然后,您可以编写一个简单的php循环,以获取查询中的每一行并在html中生成一个表行。

If you look at the manual for GROUP_CONCAT , you can see that you can also set the order that the userIds are grouped in, which might be useful. 如果查看GROUP_CONCAT的手册,您会发现您还可以设置userId的分组顺序,这可能会很有用。

The php loop is pretty simple once you have your query results; 一旦获得查询结果,php循环就非常简单。 you just need to create a table and then add a row for each result: 您只需要创建一个表,然后为每个结果添加一行:

/*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>';

That's a very simple example, but hopefully demystifies the process somewhat. 这是一个非常简单的示例,但是希望可以使这一过程变得神秘。

Your particular case looks like the query is actually to get the contents of one cell in your table; 您的特殊情况看起来查询实际上是获取表中一个单元格的内容。 you can either do a much grander query that groups by time and day of week, or you can do little queries and store the results in nested arrays that model how you want your table to look. 您可以执行更宏大的查询(按时间和星期几进行分组),也可以执行少量查询并将结果存储在嵌套数组中,这些数组模拟您希望表的外观。 The html output is structurally the same, but you would have a php loop for each table cell. html输出在结构上是相同的,但是每个表单元格都有一个php循环。

To have table with each cell showing the result of a separate query, consider something like the following pseudocode: 要使每个单元格都有一个表显示单独的查询结果,请考虑以下伪代码:

$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

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM