elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Introducción a Git (Primera Parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP (Moderador: #!drvy)
| | | |-+  Parse Error Subvariable como index
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Parse Error Subvariable como index  (Leído 1,922 veces)
Miseryk

Desconectado Desconectado

Mensajes: 225


SI.NU.SA U.GU.DE (2NE1 - D-Unit)


Ver Perfil
Parse Error Subvariable como index
« en: 2 Febrero 2017, 16:47 pm »

Código
  1. <?php
  2. $pdo = Database::connect();
  3.  
  4. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  5.  
  6. $sql = "SELECT * FROM Users where ID = ?";
  7. $q = $pdo->prepare($sql);
  8. $q->execute(array($id));
  9.  
  10. $data = $q->fetch(PDO::FETCH_ASSOC);
  11.  
  12. Database::disconnect();
  13.  
  14. $Columnas = array
  15. (
  16. array("User"),
  17. array("Password"),
  18. );
  19.  
  20. foreach ($Columnas as $value)
  21. {
  22. echo "<div class='control-group'>";
  23. echo " <label class='control-label'>$value[0]</label>";
  24. echo " <div class='controls'>";
  25. echo " <label class='checkbox'>";
  26. echo " <?php echo $data['" . $value[0] . "'];?>"; ---------->ACA NO FUNCIONA
  27. echo " </label>";
  28. echo " </div>";
  29. echo "</div>";
  30. }
  31. ?>
  32.  

Mi lógica es hacer que que tome las columnas para hacer un html y que a su vez tome el valor correspondiente desde
$data, pero me muestra un mensaje de error:

"Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)"

Alguno tiene alguna idea de como hacer funcionar éso? en realidad no son 2 campos boludos, son como 10 y con esa lógica no tengo que andar metiendo mucho html.

También probé así:

Código
  1. echo " <label class='checkbox'>";
  2. echo " $data['$value[0]']";
  3. echo " </label>";
  4.  


En línea

Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It’s never too late to change our luck
So, don’t let them steal your light
Don’t let them break your stride
There is light on the other side
And you’ll see all the raindrops falling behind
Make it out tonight
it’s a revolution

CL!!!
xiruko


Desconectado Desconectado

Mensajes: 438


Ver Perfil
Re: Parse Error Subvariable como index
« Respuesta #1 en: 2 Febrero 2017, 16:59 pm »

Hola,

Cuando quieres imprimir un array, usa comillas dobles " para la cadena y usa {} para englobar la variable que es un array.

Código
  1. echo "<label class='checkbox'>{$data[$value[0]]}</label>";

Saludos!


En línea

Miseryk

Desconectado Desconectado

Mensajes: 225


SI.NU.SA U.GU.DE (2NE1 - D-Unit)


Ver Perfil
Re: Parse Error Subvariable como index
« Respuesta #2 en: 2 Febrero 2017, 17:07 pm »

Hola,

Cuando quieres imprimir un array, usa comillas dobles " para la cadena y usa {} para englobar la variable que es un array.

Código
  1. echo "<label class='checkbox'>{$data[$value[0]]}</label>";

Saludos!

JOJO esa solución es mágica, funciona perfectamente, inclusive alterando el orden los campos!

Muchísimas gracias!! voy a buscar información sobre éso.
En línea

Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It’s never too late to change our luck
So, don’t let them steal your light
Don’t let them break your stride
There is light on the other side
And you’ll see all the raindrops falling behind
Make it out tonight
it’s a revolution

CL!!!
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines