我试图在while循环中执行此操作,但是我不知道如何根据示例表获取交易的名称。

数据库中的事务表

 ID   Name      Date of transaction   Amount
 1    James     01/01/2018            150
 2    James     03/02/2018            30
 3    John      03/03/2018            800
 4    John      04/08/2018            20
 5    James     05/15/2018            2000


上面是我的交易表,我想要以下格式的数据

Name    1st Payment   2nd Payment     3rd Payment
James   150           30              2000
John    800           20


我只知道如何显示数据库表,但是我不知道如何在此处添加它,例如使其基于名称。这是我的示例代码:

$sample = mysqli_query($db,"SELECT * FROM report");
while($samp = mysqli_fetch_array($sample)) {
echo"<tr>";
echo"<td>".$samp['name']."</td>";
echo"<td>".$samp['transaction_date']."</td>";
echo"<td>".$samp['amount']."</td>";
echo"<tr>";
}

最佳答案

这不是基于性能的合适代码,我的建议是您应该优化查询

但是此代码将解决您的问题。
    

$sample = mysqli_query($db,"SELECT * FROM report GROUP BY Name");
while($samp = mysqli_fetch_array($sample)) {
    echo"<tr>";
    echo"<td>".$samp['name']."</td>";
    $result=mysqli_query($db,"SELECT * FROM report where name = '".$samp['name']."'");
    while($samp2 = mysqli_fetch_array($result)) {
        echo"<td>".$samp2['amount']."</td>";
    }
    echo"</tr>";
}

09-05 21:56