PHP - 在多个表中显示SQL结果

时间:2013-07-18 13:26:01

标签: php sql html-table odbc

我有一个包含学生房间分配的数据库表。每个学生都有一个特定的大厅,楼层和公寓。我需要在特定的表格中显示每个学生,以便结果看起来像地板布局。以下是一个例子。学生证需要在正确的公寓位。每间公寓可能有几张身份证。现在它只是将它们列在页面下面。

Apartment 102 Apartment 101

Apartment 104 Apartment 103

Apartment 106 Apartment 105

$query = "select res.ID_NUM as ID, res.APARTMENT
From Residents res
Where res.sess_cde = '$pulledsession'
and res.ROOM_ASSIGN_STS = 'A'
and res.BLDG_CDE = '$pulledhall'
and res.FLOOR = '$pulledfloor'";


$result = odbc_exec($connect, $query);


echo "<table style='padding:25;'>
<tr>
<th>Apartment</th>
<th>ID</th>
</tr>";
while(odbc_fetch_row($result)){ 
$ID = odbc_result($result,ID);
$APARTMENT = odbc_result($result,APARTMENT);

if ($APARTMENT == $pulledfloor.'01')
{  
echo "<tr   >";
echo "<td>" . $pulledfloor.'01' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'02')
{  
echo "<tr>";
echo "<td>" . $pulledfloor.'02' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'03')  
{ 
echo "<tr>";
echo "<td>" . $pulledfloor.'03' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'04')
{  
echo "<tr>";
echo "<td>" . $pulledfloor.'04' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'05')
{  
echo "<tr>";
echo "<td>" . $pulledfloor.'05' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'06')
{  
echo "<tr>";
echo "<td>" . $pulledfloor.'06' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
}
echo "</table>";

1 个答案:

答案 0 :(得分:0)

您还需要检索房间号码。之后,一种方法是将结果加载到关联的数组中:

$rooms[$row['roomNumber']] = $resName; // Example

一旦你构建了数组,你就可以手动将它回显到一个表中(构建完整的表并用

回显每个表)
echo "<tr><td>".$rooms['102']."</td><td>".$rooms['101']."</td></tr>";

或类似的,或通过在循环中递增两个房间号来动态地进行。

如果你在一个房间里有多个学生,那么就可以更深入地了解这个阵列:

$rooms[$row['roomNumber']][] = $resName;

然后在每个单元格中使用一个循环来回显它。