【问题标题】:How to sort row data fetched with mysqli in a numerical order, starting from 1如何按从 1 开始的数字顺序对使用 mysqli 获取的行数据进行排序
【发布时间】:2019-03-22 18:14:43
【问题描述】:

我在 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 html mysqli


【解决方案1】:

我已将您的查询替换为静态数组,但只要您的查询正确,它就应该可以工作。请注意,我使用的是 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>";
    ?>

【讨论】:

  • 非常感谢@Yogesh,它真的成功了。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
相关资源
最近更新 更多