Ver Mensaje Individual
  #2 (permalink)  
Antiguo 17/01/2012, 04:11
Avatar de repara2
repara2
 
Fecha de Ingreso: septiembre-2010
Ubicación: München
Mensajes: 2.445
Antigüedad: 13 años, 7 meses
Puntos: 331
Respuesta: Crear json con arbol de directorios

A ver si esto te sirve:


Código PHP:
Ver original
  1. <?php
  2.  
  3. /**
  4.  
  5. CREATE TABLE ’temp’ (
  6.  
  7. ‘id’ int(11) NOT NULL AUTO_INCREMENT,
  8.  
  9. ‘descripcion’ varchar(128) DEFAULT NULL,
  10.  
  11. ‘dependencia’ int(11) DEFAULT NULL,
  12.  
  13. PRIMARY KEY (‘id’)
  14.  
  15. ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
  16.  
  17. CREATE TABLE ’temp’ (
  18.  
  19. ‘id’ DOUBLE ,
  20.  
  21. ‘descripcion’ VARCHAR (384),
  22.  
  23. ‘dependencia’ DOUBLE
  24.  
  25. );
  26. INSERT INTO ’temp’ (‘id’, ’descripcion’, ’dependencia’) VALUES(’1′,’General’,’0′);
  27. INSERT INTO ’temp’ (‘id’, ’descripcion’, ’dependencia’) VALUES(’2′,’Reservas’,’0′);
  28.  
  29. INSERT INTO ’temp’ (‘id’, ’descripcion’, ’dependencia’) VALUES(’3′,’Facturacion’,’0′);
  30. INSERT INTO ’temp’ (‘id’, ’descripcion’, ’dependencia’) VALUES(’4′,’Reservas_Online’,’2′);
  31.  
  32. INSERT INTO ’temp’ (‘id’, ’descripcion’, ’dependencia’) VALUES(’5′,’Reservas_Offline’,’2′);INSERT INTO ’temp’ (‘id’, ’descripcion’, ’dependencia’) VALUES(’6′,’Cobros’,’3′);
  33. INSERT INTO ’temp’ (‘id’, ’descripcion’, ’dependencia’) VALUES(’7′,’Pagos’,’3′);
  34. */
  35.  
  36. mysql_connect(“localhost”, “INS_bd217754&#8243;, “rewqazxcv”);
  37.  
  38. mysql_select_db(“bd217754&#8243;);
  39.  
  40. function getMenu($xElement)
  41.  
  42. {
  43.  
  44. $xResponse = array();
  45.  
  46. $sql = “SELECT * FROM temp WHERE dependencia =$xElement’;;
  47.  
  48. if (!$query = mysql_query($sql))
  49.  
  50. $xResponse = array(“Error en la select:. mysql_error());
  51.  
  52. while ($fetch = mysql_fetch_assoc($query)) {
  53.  
  54. if ($fetch['dependencia'])
  55.  
  56. if (array_key_exists($fetch['dependencia'], $xResponse)) {
  57.  
  58. $xResponse[$fetch['dependencia']][$fetch['id']]['descripcion'] = $fetch['descripcion'];
  59.  
  60. $xResponse[$fetch['dependencia']][$fetch['id']]['id'] = $fetch['id'];
  61.  
  62. $xResponse[$fetch['dependencia']][$fetch['id']]['dependencias'] = (count(getMenu($fetch['id'])) ? getMenu($fetch['id']) : “Ninguna”);
  63.  
  64. } else {
  65.  
  66. $xResponse[$fetch['id']]['descripcion'] = $fetch['descripcion'];
  67.  
  68. $xResponse[$fetch['id']]['id'] = $fetch['id'];
  69.  
  70. $xResponse[$fetch['id']]['dependencias'] = (count(getMenu($fetch['id'])) ? getMenu($fetch['id']) : “Ninguna”);
  71.  
  72. }
  73.  
  74. }
  75.  
  76.  
  77. return $xResponse;
  78.  
  79. }
  80.  
  81. echo<pre>;
  82.  
  83. print_r($test = getMenu(3));
  84.  
  85. echo</pre>;
  86.  
  87. ?>
__________________
Fere libenter homines, id quod volunt, credunt.