【问题标题】:Retrieving last number (not index) of an a array - PHP and MySQL检索数组的最后一个数字(不是索引) - PHP 和 MySQL
【发布时间】:2018-03-10 20:14:17
【问题描述】:

下面这个简单的代码可以完美运行。 它很好地从我的数据库中检索数据。

问题是,我需要检索“saleNumber”行中显示的最后一个数字(不是索引)。 我将使用它来自动增加表单输入文本的数量。

我已尝试以多种不同方式使用 end 和 array_slice,但可以找到解决方案。

        <?php
        while($row = mysqli_fetch_assoc($result)){
        echo "<td class='contentTd'> $row[saleNumber] </td>
              <td class='contentTd'> $row[saleValue] </td>
              <td class='contentTd'> $row[paymentMethod] </td>
              <td class='contentTd'> $row[sellerName] </td>";
        };

我很欣赏任何想法。

【问题讨论】:

    标签: php mysql arrays while-loop


    【解决方案1】:

    也许我遗漏了一些明显的东西,但为什么不只是记住循环中的值,然后使用它呢?

    <?php
    $last = null;
    while($row = mysqli_fetch_assoc($result)){
        $last = $row['saleNumber'];
        echo "<td class='contentTd'> $row[saleNumber] </td>
              <td class='contentTd'> $row[saleValue] </td>
              <td class='contentTd'> $row[paymentMethod] </td>
              <td class='contentTd'> $row[sellerName] </td>";
    };
    if (null !== $last) {
        $next = $last + 1; // or whatever
        echo "<td class='contentTd'> $next </td>...";
    
    }
    

    【讨论】:

    • 谢谢主教!做到了!但即使现在它正在工作,我也不明白 $last 变量如何只获取最后一个数字而不是数字数组。
    • 每次循环,$last 都被分配了$row['saleNumber']标量值。如果代码读取$last[] = $row['saleNumber'],那么它将是一个数组。
    • 知道了!如此简单,但我无法让它工作。再次感谢!
    猜你喜欢
    • 2012-11-25
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 2019-11-20
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    相关资源
    最近更新 更多