简体   繁体   English

在While循环中的表中拆分ID

[英]Split ID's Up In Tables In While Loop

I have the following database table: 我有以下数据库表:

AWARD_ID   |  NOMINEE_ID   |  VOTER_ID  |  MULTI_CODE
------------------------------------------------------
5          | 3             |  1         |  9326
5          | 4             |  1         |  9326
5          | 5             |  3         |  8746

I need to display these results in tables grouped by MULTI_CODE, so for example: 我需要在按MULTI_CODE分组的表中显示这些结果,例如:

So it would look like 所以看起来像

<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>

Here is my SQL + PHP so far: 到目前为止,这是我的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>

With this I get this: 有了这个我得到:

 8746

 9326

 Nominee: 3

 9326

 Nominee: 4 Nominee: 5

Why am I getting the 9326 above the Nominee 3? 为什么我使9326超过提名3?

First of all, I suggest to order your results by MULTI_CODE to have the correct sorting when looping through the results. 首先,我建议您按MULTI_CODE对结果进行排序,以便在遍历结果时进行正确的排序。

Then, in your loop you always print the headline with the multi code, regardless being different to the previous one. 然后,在循环中,始终与多代码一起打印标题,无论与前一个代码是否不同。

Please have a look at this code. 请看一下这段代码。 It isn't tested, so please be aware that there might be errors in it: 它未经测试,因此请注意其中可能存在错误:

$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>";
}

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

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