PHP 获取最后一个数据 ID


PHP Get Last Data ID

我希望最后一个数据有margin-bottom: 0px;

函数.php

$SQL_7 = mysqli_query($Connection, "SELECT * FROM users");
$row_number = mysqli_num_rows($SQL_7);
$i = 1;
while ($Fetch_7 = mysqli_fetch_array($SQL_7)) {                 
    // If last row remove margin bottom
    if ($i < $row_number) {
        $get_margin = "style='margin-bottom: 0px;'";
    }
}

索引.php

<div class='margin_bottom_10' <?php echo $get_margin; ?> >
    Test
</div>

 <div id='data1' class='margin_bottom_10'>Test</div>
<div id='data2' class='margin_bottom_10'>Test</div>
<div id='data3' class='margin_bottom_10' style='margin-bottom: 0px;'>Test</div>
您应该在

while 循环中递增$i。 $i++向变量添加一个。您希望此值与您的行计数匹配,因为这将是最后一条记录。如果您有 10 个用户,经过 10 次循环后$i将等于 10,然后您的子句将添加样式标记。

<?php
$SQL_7 = mysqli_query($Connection, "SELECT * FROM users");
$row_number = mysqli_num_rows($SQL_7);
$i = 0;
while ($Fetch_7 = mysqli_fetch_array($SQL_7)) {                 
    // increment i
    $i++;
    // If last row remove margin bottom
    if ($i == $row_number) {
        $get_margin = "style='margin-bottom: 0px;'"; // Do show
    } else {
        $get_margin = ""; // Dont show
    }
}

编辑:最好从零开始!

increment $i每个

步骤的时间以及何时$i= num_of_row断言$get_margin需要margin

    `$SQL_7 = mysqli_query($Connection, "SELECT * FROM users");
    $row_number = mysqli_num_rows($SQL_7);
    $i = 1;
    while ($Fetch_7 = mysqli_fetch_array($SQL_7)) {                 
        // If last row remove margin bottom
        if ($i ==  $row_number) 
            $get_margin = "style='margin-bottom: 0px;'";

            $i++;
    }
    ?>
    <div id='data1' class='margin_bottom_10'>Test</div>
    <div id='data2' class='margin_bottom_10'>Test</div>
    <div id='data3' class='margin_bottom_10' <?php  echo $get_margin; ?>>Test</div>`