Willian
Willian

Reputation: 25

Retrieving last number (not index) of an a array - PHP and MySQL

I have this simple code below working perfectly. It's retrieving data out of my database nicely.

The thing is, i need to retrieve the last number(not index) that is showing in the "saleNumber" row. I'm gonna use it to increment the number of a form input text automatically.

I've tried to use end and array_slice in dozens of differents ways but could find a solution for this.

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

I appreciate any ideas.

Upvotes: 2

Views: 41

Answers (1)

bishop
bishop

Reputation: 39444

Perhaps I'm missing something obvious, but why not just remember the value as you iterate through the loop, then use it?

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

}

Upvotes: 1

Related Questions