Load data from a database when pressing buttons (next and previous) No backspace

0

I have a system that shows "TIPS" of health contained in a database. I would like to add buttons next and previous

The idea is that when you press next or previous one, change the nume_cons and then load the title, the description and the image ( obtain.php )

CURRENTLY THE PROBLEM IS THAT: IT ONLY INCREASES THE VALUES WHEN PRESSING NEXT DOES NOT RETROFECT THE PREVIOUS:

INDEX

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

    //include "funciones/cargar.php";

    if (!isset($_REQUEST["nume_cons"])){
      $_REQUEST["nume_cons"] = 0;
    }else{
      if (!is_numeric($_REQUEST["nume_cons"])){
         $_REQUEST["nume_cons"] = 0; 
     }
    }
    $nume_cons = $_REQUEST["nume_cons"];
    if (!is_numeric($nume_cons)){ 
       $nume_cons = 0;
      $anterior=0;
    }else{
     $anterior = $nume_cons++;    
    echo "anterior";
     }
      $siguiente = $nume_cons--;   
    echo "siguiente";
     //-- obtenemos los datos

    $datos     =    obtenerRegistro($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='index.php?nume_cons=<?=$anterior;?>'"  value="Anterior"></td>
  <td><input type="button"  onclick="location.href='index.php?nume_cons=<?=$siguiente;?>'"  value="Siguiente"></td>
  </tr>


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

Get data:

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

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;
}
?>
    
asked by Victor Alvarado 28.01.2017 в 15:24
source

1 answer

1

First let me tell you that personally, the code to mySQL is very ordered but to my liking it is very inefficient because many queries are made when they could be solved in one. Anyway ... A possible solution to what you need ...

I see that index.php there is a variable $ nume_cons that determines the advice to show. I guess it's a kind of ID.

INDEX.PHP

    include "conexion.php";
    include "funciones/obtener.php";
    //include "funciones/cargar.php";

    if (!isset($_REQUEST["nume_cons])){
      $_REQUEST["nume_cons"] = 0;
    }else{
      if (!is_numeric($_REQUEST["nume_cons"])){
         $_REQUEST["nume_cons"] = 0; 
     }
    }
    $nume_cons = $_REQUEST["nume_cons"];
    if (!is_numeric($nume_cons)){ 
       $nume_cons = 0;
       $anterior = 0;
    }else{
       $anterior = $nume_cons--;    
     }
    $siguiente = $nume_cons++;

     //-- obtenemos los datos
     $datos = obtenerRegistro($nume_cons);
     $titulo = $datos["titulo"];
     $descrip = $datos["descripcion"];
     $imagen = $datos["imagen"];

Now the buttons of the index.php

<td><input type="button" onclick="location.href='funciones/opciones.php?opcion=4&nume_cons=<?=$siguiente;?>'" value="Siguiente"></td>

Then in the rest of the functions

I see that you make many requests to the database when it could be in a query. You also ask for all the fields of the record but you only use one, in the following example of function I call only the requested fields and return them in the form of an array to work more comfortable then

function obtenerRegistro($nume_cons){
    global $cone;
    $consejos= mysqli_query($cone,"SELECT titu_cons,desc_cons,nume_imag FROM CONSEJO WHERE ID=$nume_cons");
    $registro= mysqli_fetch_array($consejos);
    return array("titulo" => $registro["titu_cons"], "descipcion" => $registro["desc_cons"], "imagen" => $registro["nume_imag "]);
}
    
answered by 28.01.2017 / 16:33
source