简体   繁体   中英

How to sort row data fetched with mysqli in a numerical order, starting from 1

I have a table with an auto_increment column in mysqli database, and want to fetch data from this table for each user wherever the user's name appear on this table. I want to display this fetched data on an html table which should have serial number column that starts from 1 and increases by +1 for every row in the html table. I'm finding it hard displaying my table in a numerical order.

I have searched through google and read through some similar issues on Stackoverflow but could not find solution.

<?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>";
?>

I want the html table to have a serial number column that arranges the row in a numerical order starting from 1, 2, 3, 4, 5, etc

I have replaced your query with static array, but it should work as long as your query is correct. Note that I am using foreach loop, this is not necessary your while loop will also work just as fine.

<?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>";
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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