如何从数字顺序对从mysqli获取的行数据进行排序,从1开始

问题描述 投票:1回答:1

我在mysqli数据库中有一个带有auto_increment列的表,并希望从该表中获取每个用户的数据,无论用户的名字出现在该表中。我想在html表上显示这个获取的数据,该表应该具有从1开始的序列号列,并且对于html表中的每一行增加+1。我发现很难以数字顺序显示我的表格。

我已经通过谷歌搜索并阅读Stackoverflow上的一些类似问题但无法找到解决方案。

<?php $referralList = "SELECT * FROM referrals where sponsor='$username'";
$listResult = mysqli_query($conn,$referralList)or die(mysqli_error());
echo "<table class='table table-bordered table-hover table-striped'>";                            
echo "<thead><tr><th>&#8470;</th><th>Username</th><th>Phone &#8470;</th><th>Reg Date</th><th>Total Bonus</th><th>Status</th></tr></thead>";
if (mysqli_num_rows($listResult)<1) {
echo "<em style='color:red;'>You have no referral yet, invite people to start earning extra bonuses </em>";
} else {
while($listRow = mysqli_fetch_array($listResult)) {
$id = $listRow['userid'];
$referral = $listRow['username'];
$referralPhone = $listRow['phoneNumber'];
$regD = $listRow['reg_Date'];
$totalbonus = $listRow['totalbonus'];
if ($listRow['status']==0){
$status='<td style="background-color:hsla(0, 100%, 50%, 0.8)">Suspended</td>';
else if ($listRow['status']==2) {
$status='<td style="background-color:hsla(60, 100%, 50%, 0.8)">On Probe</td>'; 
}
else if ($listRow['status']==1) {
$status= '<td style="background-color:hsla(120, 100%, 40%, 0.8); color: white;">Active <i class="fa fa-check"></i></td>';
}
else {                                                  
$status='<td>Unknown</td>';
}
echo '<tbody><tr><td>', "$id", '</td><td>', "$referral", '</td><td>', "$referralPhone", '</td><td>', "$regD",'</td><td>', "$totalbonus", '</td><b>', "$status", '</b></tr></tbody>';
  } 
}
echo "</table>";
?>

我希望html表有一个序列号列,按照从1,2,3,4,5等开始的数字顺序排列行

php html5 mysqli
1个回答
0
投票

我已经用静态数组替换了您的查询,但只要查询正确,它就应该可以工作。请注意,我使用的是foreach循环,这不是必需的,你的while循环也可以正常工作。

<?php 
    $listResult = [
                        ['userid'=>'101','username'=>'abc','phoneNumber'=>'1231231231','reg_Date'=>'03/22/2019','totalbonus'=>'5','status'=>'0'],
                        ['userid'=>'102','username'=>'def','phoneNumber'=>'5675675675','reg_Date'=>'03/22/2019','totalbonus'=>'10','status'=>'1'],
                        ['userid'=>'103','username'=>'xyz','phoneNumber'=>'6756756756','reg_Date'=>'03/22/2019','totalbonus'=>'12','status'=>'1'],
                    ];
    //$listResult = mysqli_query($conn,$referralList)or die(mysqli_error());
    echo "<table class='table table-bordered table-hover table-striped'>";                            
    echo "<thead>
            <tr>
                <th>Serial &#8470;</th>
                <th>&#8470;</th>
                <th>Username</th>
                <th>Phone &#8470;</th>
                <th>Reg Date</th>
                <th>Total Bonus</th>
                <th>Status</th>
            </tr>
        </thead>";
    if (empty($listResult)) {
    echo "<em style='color:red;'>You have no referral yet, invite people to start earning extra bonuses </em>";
    } else {
        $rowCount = 1;
        foreach($listResult as $listRow) {
            $id = $listRow['userid'];
            $referral = $listRow['username'];
            $referralPhone = $listRow['phoneNumber'];
            $regD = $listRow['reg_Date'];
            $totalbonus = $listRow['totalbonus'];
            if ($listRow['status']==0){
                $status='<td style="background-color:hsla(0, 100%, 50%, 0.8)">Suspended</td>';
            }else if ($listRow['status']==2) {
                $status='<td style="background-color:hsla(60, 100%, 50%, 0.8)">On Probe</td>'; 
            }
            else if ($listRow['status']==1) {
                $status= '<td style="background-color:hsla(120, 100%, 40%, 0.8); color: white;">Active <i class="fa fa-check"></i></td>';
            }
            else {                                                  
                $status='<td>Unknown</td>';
            }
            echo '<tbody><tr>**<td>', "$rowCount" ,'</td>**<td>', "$id", '</td><td>', "$referral", '</td><td>', "$referralPhone", '</td><td>', "$regD",'</td><td>', "$totalbonus", '</td><b>', "$status", '</b></tr></tbody>';
            ++$rowCount;
        }
    }
    echo "</table>";
    ?>
© www.soinside.com 2019 - 2024. All rights reserved.