Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Display last, 2nd last and 3rd last value from a database and display in a specific order

Assume i have the following in my database, db connection established:

A user form that has and input enter product with a submit button, which inserts into the database.

id and product, with a table named table. Filled with information.

Then i select the last id by doing the following:

           $results = mysqli_query($con, "SELECT * FROM table ORDER BY id DESC LIMIT 1");
           while($row = mysqli_fetch_array($results))
            
           {         
           $first_product = $row["product"]; 
           }

It outputs the last id in that table.

THE PROGRESS BAR

I want to add a feature when you click submit it displays the last id of the product in a progress bar, once i click next, then it displays the last id plus the 2nd last id.

The trick is, i want the first output to change to the 2nd last id, and the 2nd output to change to the last id in the table, same as when you click next again, the first output changes to the 3rd last id in the table, the second output changes to the 2nd last, and the third output changes to the last id in the table

So for example

The database would have the following

Click Submit:

Output:

Gibson Guitar (Last entry in table)

Click Next

Gibson Guitar (2nd Last entry in table) Drums (Last entry in table)

Click Next

Gibson Guitar (3rd Last entry in table) Drums (2nd Last entry in table) Bass (Last entry in table)

I know how to call the rows in a table by using ORDER BY id DESC LIMIT 1,1 and so on. But i do not know how to do it to show it as above.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

$query = "SELECT * FROM table ORDER BY id DESC LIMIT 3";
about 4 years ago · Juan Pablo Isaza Report

0

I would suggest using ORDER BY id ASC.

Then all you do is the following:

<?php 
                    
           $results = mysqli_query($con, "SELECT * FROM table ORDER BY id ASC LIMIT 1");
           while($row = mysqli_fetch_array($results))
            
           {
           $first_product = $row["product"]; 
           }
?>

<?php 
                    
           $results = mysqli_query($con, "SELECT * FROM table ORDER BY id ASC LIMIT 2");
           while($row = mysqli_fetch_array($results))
            
           {
           $second_product = $row["product"]; 
           }
?>


<?php 
                    
           $results = mysqli_query($con, "SELECT * FROM table ORDER BY id ASC LIMIT 3");
           while($row = mysqli_fetch_array($results))
            
           {
           $third_product = $row["product"]; 
           }
?>


<?php echo $first_product; ?>
<?php echo $second_product; ?>
<?php echo $third_product; ?>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!