How to change between lists or pages of records with buttons of NEXT or PREVIOUS in PHP?

0

I have a system to show health tips. I would like the next or previous click to change to the next tip that is contained in the database.

I have already managed to generate a value that is a number less or more than the ID of each council but I do not know how to change it where the ID is.

Index.php

 <?php
    include "conexion.php";
    include "funciones/obtener.php";

        $datos     =    obtenerPrimerRegistro();
        $nume_cons =    $datos["nume_cons"];
        $titu_cons =    $datos["titu_cons"];
        $desc_cons =    $datos["desc_cons"];

    ?>
    <!DOCTYPE HTML>

    <html>

    <head>

    <meta utfset="utf-8">

    <link rel="stylesheet" type="text/css" href="css/estilos.css">

    </head>

    <title>TuSalud</title>

    <body>

    <h1> TuSalud... Un consejo a la vez </h1>

    <table>
      <tr>
            <td colspan="5">    <input  type="text"     id="nume_cons"  name="nume_cons" value="<?php echo $nume_cons;?>"></td>
      </tr>

      <tr>
            <td colspan="5">    <input  type="text"     id="titu_cons"  name="titu_cons" value="<?php echo $titu_cons;?>"></td>
      </tr>

      <tr>                                                                                         
            <td colspan="5">    <img src="<?php echo obtenerImag($nume_cons);?>"> </td>
      </tr>

      <tr>
            <td colspan="5">    <input  type="text"     id="desc_cons"  name="desc_cons" value="<?php echo $desc_cons;?>"></td>
      </tr>


      <tr>



      <td><input type="button"  onclick="location.href='funciones/contador.php?contador=1&nume_cons=<?=$nume_cons;?>'"  value='Anterior'></td>
      <td><input type="button"  onclick="location.href='funciones/contador.php?contador=2&nume_cons=<?=$nume_cons;?>'"  value="Siguiente"></td>

      <td><input type="button"  onclick="location.href='funciones/opciones.php?opcion=1&nume_cons=<?=$nume_cons;?>'"    value="Descargar"></td>
      <td><input type="button"  onclick="location.href='funciones/opciones.php?opcion=2&nume_cons=<?=$nume_cons;?>'"    value="Favorito"></td>
      <td><input type="button"  onclick="location.href='funciones/opciones.php?opcion=3&nume_cons=<?=$nume_cons;?>'"    value="???"></td>
      </tr>


    </table>
    </body>
    </html>

Get.php

<?php
require_once "./conexion.php";

function obtenerPrimerRegistro(){
    global $cone;
    $consejos= mysqli_query($cone,"SELECT nume_cons,titu_cons,desc_cons FROM CONSEJO");
    $registro= mysqli_fetch_array($consejos);
    return array("nume_cons"=>$registro["nume_cons"],"titu_cons" => $registro["titu_cons"], "desc_cons" => $registro["desc_cons"]);
}

function obtenerRegistro($nume_cons){
    global $cone;
    $consejos= mysqli_query($cone,"SELECT titu_cons,desc_cons FROM CONSEJO WHERE nume_cons=$nume_cons");
    $registro= mysqli_fetch_array($consejos);
    return array("titu_cons" => $registro["titu_cons"], "desc_cons" => $registro["desc_cons"]);
}

function obtenerImag($nume_cons){
    global $cone;
    $imagen= mysqli_query($cone,"SELECT nume_imag FROM CONSEJO WHERE nume_cons=$nume_cons");
    $img= mysqli_fetch_array($imagen);
    $nume_imag=$img["nume_imag"];
    $ruta_imag="./recursos/imagenes/imagen".$nume_imag.".jpg";
    return $ruta_imag;
}
?>

contador.php     

$contador=$_REQUEST["contador"];
$nume_cons=$_REQUEST["nume_cons"];

if ($contador == 1){
$new=$nume_cons-1;
echo $new;
echo "<script> location.href='../index.php?nume_cons=$new' </script>"; 

} 
else if ($contador==2){
$new=$nume_cons+1;
echo $new;
echo "<script> location.href='../index.php?nume_cons=$new' </script>";  
}




?>
    
asked by Victor Alvarado 29.01.2017 в 03:10
source

1 answer

1

I'll give you an example that is very easy to implement, since sometimes I get lost in your code.

To create our button anterior and siguiente , you could create a URL using the GET method, in that parameter we will pass the number of ID , or my advice is that you add a column called for example pagina , where you enter the corresponding page number, so you get an ordered page.

In my example I will work with this column added to the Database, in your case you will have to add to your table CONSEJO a column called pagina with the corresponding page number.

Example:

<?php
    //Requeremos conexión.
    require_once'conexion.php';

    //Inilizamos paginación ///

    //Obtenemos 'Numero' paginación via 'GET'.
    if (isset($_GET['pagina'])) { 
        $pagina  = $_GET['pagina'];     
    } else { //Por defecto inicamos la página en 1, es decir, registro numero 1.
        $pagina=1;
    }

    //Cantidad de registro a mostrar en paginación.
    $cantidad_reg=1;    
    //Localizacion SQL.
    $ubicacion = ($pagina-1) * $cantidad_reg;

    //Sentencia SQL, mostramos consejo.
    $consejos= mysqli_query($cone,"SELECT nume_cons,titu_cons,desc_cons,nume_imag FROM CONSEJO ORDER BY pagina LIMIT $ubicacion,$cantidad_reg");
    $registro= mysqli_fetch_array($consejos);

    //Obtenemos datos a mostrar para la páginación.        
    $nume_cons = $registro['nume_cons'];
    $titu_cons = $registro['titu_cons'];
    $desc_cons = $registro['desc_cons'];
    $nume_imag = $registro['nume_imag'];  
?>
    <!DOCTYPE HTML>    
    <html>    
    <head>    
    <meta utfset="utf-8">    
    <link rel="stylesheet" type="text/css" href="css/estilos.css">    
    </head>    
    <title>TuSalud</title>    
    <body>

    <h1> TuSalud... Un consejo a la vez </h1>

    <!--  Mostramos datos para paginación -->
    <?php echo $nume_cons;?>
    <?php echo $titu_cons;?>
    <img src="<?php echo $nume_imag; ?>" />
    <?php echo $desc_cons; ?>

    <?php //Creamos botoneras anterior / siguiente

        //SQL
        $consejos= mysqli_query($cone,"SELECT * FROM CONSEJO");     
        //Total registros existentes en Base de Datos.
        $total_filas = mysqli_num_rows($consejos);
        //Cantidad a mostrar en paginación.
        $cantidad_reg=1;
        //Calculamos total paginas. 
        $total_pagina = ceil($total_filas / $cantidad_reg); 

        //Calculamos botones anterior / siguiente
        $prev = $pagina - 1;
        $next = $pagina + 1;

        //Boton 'Anterior'
        if ($prev > 0) { 
            echo "<a href='index.php?pagina=$prev'>anterior</a>"; 
        }

        //Opcional, visualizar el total de paginas, es decir, podrias crear algo similar a  < 1 2 3 4 > .       
        for ($i=1; $i<=$total_pagina; $i++) { 
            echo "<a href='index.php?pagina=$i'>$i</a>"; 
        }

        //Boton 'Siguiente'
        if ($pagina < $total_pagina ) {
            echo "<a href='index.php?pagina=$next'>siguiente</a>"; 
        }  

        mysqli_close($cone);//Cerramos conexion.

    ?>
</body>
</html>
    
answered by 29.01.2017 / 23:05
source