inputs with for-loop with database info PHP

Viewed 78

I need help, the problem is that I have a input page (the modified user profile) where I have a section of addresses and I need to do a loop where each group of inputs I put the information of each of the directions. Because now for example if I have 3 directions, in the 3 groups of inputs I get the same repeated information...

Here is a part of the code, I don't put all it because is too large, I only put the important code of this issue.

Thanks to all :)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" lang="es-es">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="css/bootstrap.min.css" rel="stylesheet" />
  <link rel="stylesheet" type="text/css" href="font-awesome-4.5.0/css/font-awesome.min.css"/>
  <link href="css/css.css" rel="stylesheet" type="text/css" media="all" />
  <link rel="stylesheet" type="text/css" href="css/icSquared_v1.0.css"/>
  <title>Modificación de Direcciones</title>
 </head>
<body>
<div>
<?php
 include 'fn.php';
 iniciarSesion();
 
 $email = $_SESSION['user'];
 $conexion = conectar();
 
 $sql = "SELECT * FROM DIRECCION WHERE email_c='".$email."'";
 
 if($resultado = $conexion -> query($sql)){
  while($fila = mysqli_fetch_array($resultado)){
   $ide = $fila[0];
   $via = $fila[1];
   $nombre = $fila[2];
   $numero = $fila[3];
   $piso = $fila[4];
   $poblacion = $fila[6];
   $direcciones = mysqli_num_rows($resultado);
   desconectar($conexion);
   
   $sql = "SELECT * FROM POBLACION WHERE idPoblacion='".$poblacion."'";
   $conexion = conectar();
   
   if($resultado = $conexion -> query($sql)){
    if($fila = mysqli_fetch_array($resultado)){
     $codigoPostal = $fila[1];
     $nombrePob = $fila[2];
     $comunidad = $fila[3];
     $pais = $fila[5];
     
     desconectar($conexion);
    }
   }else{
    desconectar($conexion);
   }
 }else{
  deconectar($conexion);
 }
?>
  <div class="col-sm-8">
   <div id="admin_centro" style="margin-top:2%">
    <form action="action.php?accion=updateCenterDir" method="post">
   <?php for($i=0; $i<$direcciones; $i++){ ?>
     <div class="col-sm-2" style="margin:2% 6% 0 5%;">
      <div class="form-group">
       <input type="hidden" name="ide" value="<?php echo $ide; ?>" />
       <h4 class="text-success text-right">Dirección <?php echo $i+1 ?></h4>
       <label>Tipo de via:</label>
       <input type="text" name="via" class="form-control" value="<?php echo $via; ?>" required />
      </div>
      <div class="form-group">
       <label>Nombre: </label>
       <input type="text" name="nombre" class="form-control" value="<?php echo $nombre; ?>" required />
      </div>
      <div class="form-group">
       <label>Numero: <label>
       <input type="number" name="num" class="form-control" value="<?php echo $numero; ?>" required min="0"/>
      </div>
      <div class="form-group">
       <label>Piso: <label>
       <input type="text" name="piso" class="form-control" value="<?php echo $piso; ?>" />
      </div>
      <div class="form-group">
       <label>Codigo Postal:  <label>
       <input type="text" name="cp" class="form-control" value="<?php echo $codigoPostal; ?>" required />
      </div>
      <div class="form-group">
       <label>Pais: <label>
       <?php desplegable_pais(-1);?>
      </div>
      <div class="form-group">
       <label>Poblacion: <label>
       <?php echo $nombrePob." - ".$comunidad." - ".$pais; ?>
       <!-- FALTA SELECT DEPENDIENTE-->
      </div>
      <input class="btn btn-success" type="submit" id="enviar" value="Modificar"/>
     </div>
   <?php } ?>
    </form>
   </div>
  </div>
 </div>
</div>
</body>
<script src="js/jquery-1.12.1.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/scriptLogin.js"></script>
</html>

elseif($action == "updateCenterDir"){
    $ide = $_POST['ide'];
    $via = $_POST['via'];
    $nombre = $_POST['nombre'];
    $numero = $_POST['num'];
    $piso = $_POST['piso'];
    $cp = $_POST['cp'];
    $poblacion = $_POST['poblacion'];

    $conexion = conectar();
    $sql = "UPDATE DIRECCION SET via='".$via."', nombre='".$nombre."', numero=".$numero.", piso='".$piso."', codigoPostal='".$cp."', poblacion='".$poblacion."' WHERE idInstalacion=".$ide;

    if($resultado = $conexion -> query($sql)){
        header("Location:mod_infoDir.php?miss=1");
        desconectar($conexion);
    }else{
        header("Location:mod_infoDir.php?miss=2");
        desconectar($conexion);
    }
}
1 Answers
Related