PHP获取上次数据ID

时间:2016-07-08 16:33:15

标签: php html

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

的functions.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;'";
    }
}

的index.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>

2 个答案:

答案 0 :(得分:0)

你应该在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
    }
}

编辑:最好从零开始!

答案 1 :(得分:0)

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>`

null