我有以下数据库表:
AWARD_ID | NOMINEE_ID | VOTER_ID | MULTI_CODE
------------------------------------------------------
5 | 3 | 1 | 9326
5 | 4 | 1 | 9326
5 | 5 | 3 | 8746
我需要在按MULTI_CODE分组的表格中显示这些结果,例如:
所以它看起来像
<h1>Multi Code: 9326</h1>
<table>
<tr>
<td>Nominee: 3</td><td>Nominee: 4</td>
</tr>
</table>
<h1>Multi Code: 8746</h1>
<table>
<tr>
<td>Nominee: 5</td>
</tr>
</table>
到目前为止,这是我的SQL + PHP:
$nomineedetails = mysqli_query($con,"SELECT AWARD_ID, NOMINEE_ID, VOTER_ID, MULTI_CODE
FROM b_awards_votes WHERE AWARD_ID = '5'");
$multi_code = -1;
while($row = mysqli_fetch_array($nomineedetails))
{
$awardID = $row['AWARD_ID'];
$nomineeID = $row['NOMINEE_ID'];
$voterID = $row['VOTER_ID'];
$multiCode = $row['MULTI_CODE'];
print "<h2>$multiCode</h2>";
if ($multi_code != $multiCode) {
print "<table><tr>";
$multi_code = $multiCode;
}
print "<td>Nominee: $nomineeID</td>";";
}
</tr></table>
有了这个,我明白了:
8746
9326
Nominee: 3
9326
Nominee: 4 Nominee: 5
为什么我将9326高于被提名人3?
答案 0 :(得分:1)
首先,我建议您通过MULTI_CODE订购结果,以便在循环结果时获得正确的排序。
然后,在你的循环中,你总是用多码代码打印标题,不管与前一张代码有什么不同。
请查看此代码。它未经过测试,因此请注意可能存在错误:
$nomineedetails = mysqli_query($con,"SELECT AWARD_ID, NOMINEE_ID, VOTER_ID, MULTI_CODE
FROM b_awards_votes WHERE AWARD_ID = '5' ORDER BY MULTI_CODE ASC");
$multi_code = -1;
while($row = mysqli_fetch_array($nomineedetails))
{
$awardID = $row['AWARD_ID'];
$nomineeID = $row['NOMINEE_ID'];
$voterID = $row['VOTER_ID'];
$multiCode = $row['MULTI_CODE'];
if ($multi_code != $multiCode) {
if($multi_code !== -1) {
print "</table>";
}
print "<h2>$multiCode</h2>";
print "<table>";
$multi_code = $multiCode;
}
print "<tr><td>Nominee: $nomineeID</td></tr>";
}