Foros del Web » Programando para Internet » PHP »

inconveniente para crear array json

Estas en el tema de inconveniente para crear array json en el foro de PHP en Foros del Web. hola amigos del foro tengo inconveniente para crear array json el primer dato no me llega id_evento @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código PHP: Ver original <? $servidor ...
  #1 (permalink)  
Antiguo 22/02/2012, 18:30
 
Fecha de Ingreso: septiembre-2010
Mensajes: 1.853
Antigüedad: 13 años, 6 meses
Puntos: 6
inconveniente para crear array json

hola amigos del foro

tengo inconveniente para crear array json

el primer dato no me llega id_evento

Código PHP:
Ver original
  1. <?
  2. $servidor = 'localhost';
  3. $bd = 'calendario';
  4. $usuario = 'postgres';
  5. $contrasenia = 'hoe798cs';
  6.    
  7. global $servidor, $bd, $usuario, $contrasenia;
  8. $db = new PDO('pgsql:host=' . $servidor . ';dbname=' . $bd, $usuario, $contrasenia);
  9.  
  10. $consulta = $db->prepare("SELECT * FROM eventos");
  11. $consulta->execute();
  12.  
  13. // Initializes a container array for all of the calendar events
  14. $jsonArray = array();
  15. while($fila = $consulta->fetch(PDO::FETCH_ASSOC))
  16.     {
  17.  $id =$fila['id_evento'];
  18.  $title =$fila['titulo'];
  19.  $start =$fila['inicio'];
  20.  $end =$fila['fin'];
  21.  $allDay=$fila['prueba'];
  22.  
  23.  // Stores each database record to an array
  24.  $buildjson = array('id' =>'$id_evento','title' => "$title", 'start' => "$start", 'end' => "$end", 'allDay' => "$allDay");
  25.  // Adds each array into the container array
  26.  array_push($jsonArray, $buildjson);
  27. }
  28. // Output the json formatted data so that the jQuery call can read it
  29. echo json_encode($jsonArray);
  30. ?>
}

Código SQL:
Ver original
  1. CREATE TABLE eventos
  2. (
  3.   id_evento serial NOT NULL,
  4.   titulo CHARACTER VARYING(64),
  5.   prueba BOOLEAN,
  6.   inicio TIMESTAMP WITH TIME zone,
  7.   fin TIMESTAMP WITH TIME zone,
  8.   CONSTRAINT pk_eventos PRIMARY KEY (id_evento)
  9. )
  #2 (permalink)  
Antiguo 22/02/2012, 18:34
 
Fecha de Ingreso: septiembre-2010
Mensajes: 1.853
Antigüedad: 13 años, 6 meses
Puntos: 6
Respuesta: inconveniente para crear array json

salida en pantalla


[{"id":"$id_evento","title":"Estudiar","start":"201 2-02-22 00:00:00-05","end":"2012-02-22 00:00:00-05","allDay":"1"}]

me impreme el $id_evento y deberia ser el 1 que es el evento
  #3 (permalink)  
Antiguo 22/02/2012, 18:36
 
Fecha de Ingreso: septiembre-2010
Mensajes: 1.853
Antigüedad: 13 años, 6 meses
Puntos: 6
Respuesta: inconveniente para crear array json

amigos ya encontre el problema

$buildjson = array('id' => "$id",'title' => "$title", 'start' => "$start", 'end' => "$end", 'allDay' => "$allDay");
  #4 (permalink)  
Antiguo 22/02/2012, 18:38
Avatar de andresdzphp
Colaborador
 
Fecha de Ingreso: julio-2011
Ubicación: $this->Colombia;
Mensajes: 2.749
Antigüedad: 12 años, 8 meses
Puntos: 793
Respuesta: inconveniente para crear array json

Te recomiendo que no uses array_push, simplemente usa:

Código PHP:
Ver original
  1. $jsonArray[]  = array('id' => $id, 'title' => $title, 'start' => $start, 'end' => $end, 'allDay' => $allDay);

y no hace falta encerrar las variables en comillas.

Saludos.
__________________
Si sabemos como leer e interpretar el manual será mucho más fácil aprender PHP. En lugar de confiar en ejemplos o copiar y pegar - PHP
  #5 (permalink)  
Antiguo 22/02/2012, 18:47
 
Fecha de Ingreso: septiembre-2010
Mensajes: 1.853
Antigüedad: 13 años, 6 meses
Puntos: 6
Respuesta: inconveniente para crear array json

gracias por responder andresdzphp lo probare
  #6 (permalink)  
Antiguo 22/02/2012, 18:55
 
Fecha de Ingreso: septiembre-2007
Ubicación: PyRoot
Mensajes: 1.515
Antigüedad: 16 años, 6 meses
Puntos: 188
Respuesta: inconveniente para crear array json

json_encode: parametro un array devuelvo un obj json
json_decode: parametro un obj json y devuelve un array
__________________
Si quieres agradecer el triangulo obscuro de la parte derecha debes presionar +.
  #7 (permalink)  
Antiguo 22/02/2012, 18:58
Avatar de andresdzphp
Colaborador
 
Fecha de Ingreso: julio-2011
Ubicación: $this->Colombia;
Mensajes: 2.749
Antigüedad: 12 años, 8 meses
Puntos: 793
Respuesta: inconveniente para crear array json

Si quieres manejar el json con los mismos nombres de tu tabla, podrías hacer:

Código PHP:
Ver original
  1. echo json_encode($consulta->fetchAll(PDO::FETCH_ASSOC));

Así te evitas el bucle y todo lo demás. En caso extremo usaría un foreach con fetchAll
__________________
Si sabemos como leer e interpretar el manual será mucho más fácil aprender PHP. En lugar de confiar en ejemplos o copiar y pegar - PHP

Etiquetas: inconveniente, json, sql, usuarios
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:22.