skip to Main Content

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.

2

Answers


  1. $query = "SELECT * FROM table ORDER BY id DESC LIMIT 3";
    
    Login or Signup to reply.
  2. 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; ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search