2

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.

0

1 Answer 1

1

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

}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks bishop! That did it! But even now that is working, i don't understand how the $last variable gets only the last number and not an array of numbers.
Each time through the loop, $last is assigned the scalar value of $row['saleNumber']. Had the code read $last[] = $row['saleNumber'], then it would be an array.
Got it! So simple yet i couldn't get it working. Thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.