Php 在不同的行而不是同一行上循环显示时
我有三张桌子:Php 在不同的行而不是同一行上循环显示时,php,mysql,while-loop,html-table,Php,Mysql,While Loop,Html Table,我有三张桌子: Student - UPN, Name, Year, House Seclusion_Status - ID, Arrived, FTE, Rebuild, DateTimeAdded, Staff, Student_UPN (fk), Comment Period_Rating - ID, Slot_ID, Rating, Date, Seclusion_ID (fk) 每个学生可以在隔离状态表中有许多条目,然后在期间分级表中也有许多条目,该表与隔离ID为的隔离状态表链接 我
Student - UPN, Name, Year, House
Seclusion_Status - ID, Arrived, FTE, Rebuild, DateTimeAdded, Staff, Student_UPN (fk), Comment
Period_Rating - ID, Slot_ID, Rating, Date, Seclusion_ID (fk)
每个学生可以在隔离状态表中有许多条目,然后在期间分级表中也有许多条目,该表与隔离ID为的隔离状态表链接
我正在运行以下查询,以根据日期从“隔离”状态返回一条记录,然后返回“期间”评级表中与“隔离”状态记录相关的所有记录
$sql="SELECT * FROM Seclusion_Status
INNER JOIN Students ON Seclusion_Status.Student_UPN=Students.UPN
JOIN Period_Rating ON Seclusion_Status.ID=period_rating.Seclusion_ID
WHERE period_rating.Date = '$start'
ORDER BY Seclusion_Status.DateTimeAdded ASC";
$result=mysql_query($sql);
然后,我使用while循环遍历结果:
while($rows=mysql_fetch_array($result)){
<tbody>
<tr>
<!--Write out the Student name and year group, and set the colour based on their House/College-->
<?php if($rows['House'] == 'Acer') {
echo '<td width="150px" bgcolor="#003399">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' . '</td>';
}else if($rows['House'] == 'Clarus') {
echo '<td width="150px" bgcolor="#FF0000">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' . '</td>';
}else if($rows['House'] == 'Fortis') {
echo '<td width="150px" bgcolor="#02A10C">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' . '</td>';
}else{
echo '<td width="150px" bgcolor="#D2D904">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' . '</td>';
}
?>
<!--Write Out the Staff Name-->
<td><p><?php echo $rows['Staff']; ?> </p></td>
<!--Write out the comment and the incident type in brackets-->
<td width="210px"><p><?php echo $rows['Comment']; ?> (<?php echo $rows['Incident']; ?>) </p></td>
<!--Start writing out the ratings for Period 1-->
<form action="P1Append.php?PrimaryID=<?php echo $rows['PrimaryID']; ?>&startdate=<?php echo $start; ?>" method="post">
<?php
if ($rows['Slot_ID'] == P1)
{
if (empty($rows['Rating']))
{
echo '<td><select onchange="this.form.submit()" style=" width:30px; height:30px;font-size:12pt; background-color:white;" type="text" name="P1" id="P1" maxlength="15" size="1"><option disabled selected></option><option>G</option><option>A</option><option>R</option></td>';
}else if ($rows['Rating'] == G)
{
echo '<td bgcolor="#02A10C">' . $rows['Rating'] . '</td>';
}else if ($rows['Rating'] == A)
{
echo '<td bgcolor="#ff9900">' . $rows['Rating'] . '</td>';
}else if ($rows['Rating'] == R)
{
echo '<td bgcolor="#FF0000">' . $rows['Rating'] . '</td>';
}
}else
{
echo '<td><img src="images\add.png" width="20px"></td>';
}
?>
</form>
<!--Start writing out the ratings for Period 2-->
<form action="P2Append.php?PrimaryID=<?php echo $rows['PrimaryID']; ?>&startdate=<?php echo $start; ?>" method="post">
<?php
if ($rows['Slot_ID'] == P2)
{
if (empty($rows['Rating']))
{
echo '<td><select onchange="this.form.submit()" style=" width:30px; height:30px;font-size:12pt; background-color:white;" type="text" name="P2" id="P2" maxlength="15" size="1"><option disabled selected></option><option>G</option><option>A</option><option>R</option></td>';
}else if ($rows['Rating'] == G)
{
echo '<td bgcolor="#02A10C">' . $rows['Rating'] . '</td>';
}else if ($rows['Rating'] == A)
{
echo '<td bgcolor="#ff9900">' . $rows['Rating'] . '</td>';
}else if ($rows['Rating'] == R)
{
echo '<td bgcolor="#FF0000">' . $rows['Rating'] . '</td>';
}
}else
{
echo '<td><img src="images\add.png" width="20px"></td>';
}
?>
</form>
然后在表格中显示结果。我遇到的问题是,在period_rating表中有多个条目的地方,每个学生都被复制到不同的行,而不是同一行
这是我用来从while循环中写出数据的代码:
while($rows=mysql_fetch_array($result)){
<tbody>
<tr>
<!--Write out the Student name and year group, and set the colour based on their House/College-->
<?php if($rows['House'] == 'Acer') {
echo '<td width="150px" bgcolor="#003399">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' . '</td>';
}else if($rows['House'] == 'Clarus') {
echo '<td width="150px" bgcolor="#FF0000">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' . '</td>';
}else if($rows['House'] == 'Fortis') {
echo '<td width="150px" bgcolor="#02A10C">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' . '</td>';
}else{
echo '<td width="150px" bgcolor="#D2D904">' . $rows['Name'] . ' <p>(' . $rows['Year'] . ')</p>' . '</td>';
}
?>
<!--Write Out the Staff Name-->
<td><p><?php echo $rows['Staff']; ?> </p></td>
<!--Write out the comment and the incident type in brackets-->
<td width="210px"><p><?php echo $rows['Comment']; ?> (<?php echo $rows['Incident']; ?>) </p></td>
<!--Start writing out the ratings for Period 1-->
<form action="P1Append.php?PrimaryID=<?php echo $rows['PrimaryID']; ?>&startdate=<?php echo $start; ?>" method="post">
<?php
if ($rows['Slot_ID'] == P1)
{
if (empty($rows['Rating']))
{
echo '<td><select onchange="this.form.submit()" style=" width:30px; height:30px;font-size:12pt; background-color:white;" type="text" name="P1" id="P1" maxlength="15" size="1"><option disabled selected></option><option>G</option><option>A</option><option>R</option></td>';
}else if ($rows['Rating'] == G)
{
echo '<td bgcolor="#02A10C">' . $rows['Rating'] . '</td>';
}else if ($rows['Rating'] == A)
{
echo '<td bgcolor="#ff9900">' . $rows['Rating'] . '</td>';
}else if ($rows['Rating'] == R)
{
echo '<td bgcolor="#FF0000">' . $rows['Rating'] . '</td>';
}
}else
{
echo '<td><img src="images\add.png" width="20px"></td>';
}
?>
</form>
<!--Start writing out the ratings for Period 2-->
<form action="P2Append.php?PrimaryID=<?php echo $rows['PrimaryID']; ?>&startdate=<?php echo $start; ?>" method="post">
<?php
if ($rows['Slot_ID'] == P2)
{
if (empty($rows['Rating']))
{
echo '<td><select onchange="this.form.submit()" style=" width:30px; height:30px;font-size:12pt; background-color:white;" type="text" name="P2" id="P2" maxlength="15" size="1"><option disabled selected></option><option>G</option><option>A</option><option>R</option></td>';
}else if ($rows['Rating'] == G)
{
echo '<td bgcolor="#02A10C">' . $rows['Rating'] . '</td>';
}else if ($rows['Rating'] == A)
{
echo '<td bgcolor="#ff9900">' . $rows['Rating'] . '</td>';
}else if ($rows['Rating'] == R)
{
echo '<td bgcolor="#FF0000">' . $rows['Rating'] . '</td>';
}
}else
{
echo '<td><img src="images\add.png" width="20px"></td>';
}
?>
</form>
当前返回的内容是:
Name | Student_UPN | Slot_ID | Rating
---------------------------------------------
John | 112 | P1 | G
John | 112 | P2 | R
John | 112 | P3 | G
我想要的是:
Name | Student_UPN | P1 Rating | P2 Rating | P3 Rating
---------------------------------------------
John | 112 | G | R | R
希望这更有意义
所以在链接的问题之后,我更新了我的查询,但是它仍然不起作用
$sql="SELECT *
MAX(case when period_rating.Slot_ID = 'P1' THEN Rating ELSE Null END) 'P1',
MAX(case when period_rating.Slot_ID = 'P2' THEN Rating ELSE Null END) 'P2',
MAX(case when period_rating.Slot_ID = 'LF' THEN Rating ELSE Null END) 'LF',
MAX(case when period_rating.Slot_ID = 'BR' THEN Rating ELSE Null END) 'BR',
MAX(case when period_rating.Slot_ID = 'P3' THEN Rating ELSE Null END) 'P3',
MAX(case when period_rating.Slot_ID = 'P4' THEN Rating ELSE Null END) 'P4',
MAX(case when period_rating.Slot_ID = 'LC' THEN Rating ELSE Null END) 'LC',
MAX(case when period_rating.Slot_ID = 'P5' THEN Rating ELSE Null END) 'P5',
MAX(case when period_rating.Slot_ID = 'P6' THEN Rating ELSE Null END) 'P6',
MAX(case when period_rating.Slot_ID = 'DT' THEN Rating ELSE Null END) 'DT'
FROM Seclusion_Status
INNER JOIN Students
ON Seclusion_Status.Student_UPN=Students.UPN
INNER JOIN Period_Rating
ON Seclusion_Status.ID=period_rating.Seclusion_ID
WHERE period_rating.Date = '$start'
GROUP BY Seclusion_Status.Student_UPN
ORDER BY Seclusion_Status.DateTimeAdded ASC";
您似乎在正确的轨道上,但是,根据您提供的表结构,max()函数中的case语句对我来说似乎没有意义:
MAX(case when period_rating.Rating = 'P1' THEN Student END) 'P1'
Slot\u ID
字段中,而不是在Rating字段中Student
字段。根据问题中的描述,您希望返回列中的评级
字段null
一个总体注释:如果您在输出中的
Name
和Student\u UPN
字段后面,那么用这两列替换选择列表中的*
,并将它们也列在分组列表中。好的,因此我最终通过使用以下查询到达了那里:
SELECT seclusion_status.ID, seclusion_status.Arrived, seclusion_status.FTE, seclusion_status.Rebuild, seclusion_status.Text, seclusion_status.DateTimeAdded, seclusion_status.Staff, seclusion_status.Student_UPN, seclusion_status.Incident, seclusion_status.Comment, students.Name, students.UPN, students.Year, students.House, period_rating.ID, period_rating.Slot_ID, period_rating.Rating, period_rating.Date, period_rating.Seclusion_ID,
GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P1' THEN period_rating.Rating ELSE NULL END)) AS Period1_Rating,
GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P2' THEN period_rating.Rating ELSE NULL END)) AS Period2_Rating,
GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'LF' THEN period_rating.Rating ELSE NULL END)) AS LF_Rating,
GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'BR' THEN period_rating.Rating ELSE NULL END)) AS BR_Rating,
GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P3' THEN period_rating.Rating ELSE NULL END)) AS Period3_Rating,
GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P4' THEN period_rating.Rating ELSE NULL END)) AS Period4_Rating,
GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'LC' THEN period_rating.Rating ELSE NULL END)) AS LC_Rating,
GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P5' THEN period_rating.Rating ELSE NULL END)) AS Period5_Rating,
GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'P6' THEN period_rating.Rating ELSE NULL END)) AS Period6_Rating,
GROUP_CONCAT((CASE period_rating.Slot_ID WHEN 'DT' THEN period_rating.Rating ELSE NULL END)) AS DT_Rating
FROM Seclusion_Status
INNER JOIN Students
ON Seclusion_Status.Student_UPN=Students.UPN
INNER JOIN Period_Rating
ON Seclusion_Status.ID=period_rating.Seclusion_ID
WHERE period_rating.Date = '$start'
GROUP BY period_rating.Seclusion_ID
ORDER BY Seclusion_Status.DateTimeAdded ASC
你能提供样品源数据和样品期望输出吗?现在我真的无法想象你在追求什么。我提供这个的最好方式是什么?基本上,对于表中的每一行,我需要以下列:学生姓名、教职员工、评论、第1期、第2期、第3期等。第1期应采用ID为P1的期间评分表中的数据,期间2,其中ID为P2etc@Shadow我现在用一些样本数据编辑了原始问题,希望能显示出我正在尝试做什么??现在它变得更有意义了。这种技术称为动态数据透视或动态交叉制表(交叉表)查询。这已经在这里讨论了好几次了,很可能是《谢谢指点》的副本。我已经更新了原始问题中的查询,但仍然没有得到任何输出(尽管没有显示错误消息)。else分支中的null是什么意思?Khm,您可以阅读案例手册:如果您没有收到任何错误消息,但没有检索到任何记录,这意味着您的查询不会返回任何行。它可能是内部联接或where条件的结果。因为我看不到你的数据,所以我不能帮你。开始删除where条件,然后将内部连接更改为左/右连接,以查看向南的位置。我会首先使用mysql管理应用程序(例如像phpmyadmin这样的sg)运行sql语句,看看它是否有效。当你对sql进行排序时,然后将sql移动到你的php代码中。我确实阅读了手册,但当我在谷歌上搜索MySQL案例时,它把我带到了另一个没有提到其他内容的页面,这就是为什么我感到困惑的原因。