gpt4 book ai didi

php - 在 While 循环中的表中拆分 ID

转载 作者:行者123 更新时间:2023-11-29 12:04:41 24 4
gpt4 key购买 nike

我有以下数据库表:

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 高于 Nominee 3?

最佳答案

首先,我建议按 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>";
}

关于php - 在 While 循环中的表中拆分 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31719431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com