Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

0

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda