Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/04/2012, 10:21
Sirrohan
 
Fecha de Ingreso: julio-2010
Mensajes: 275
Antigüedad: 13 años, 9 meses
Puntos: 21
Select dependiente desde una misma tabla

Saludos amigos foreros!!

Tengo una situación con unas tablas de Postgres mal diseñadas (no las diseñé yo) que no está asociadas a una tabla padre sino desde una tabla (como una vulgar tabla en excel), el detalle está en que he tenido ciertas dificultades para diseñar estos select, lo que se quiere es obtener los registros filtrados por las opciones seleccionadas (en los select's obviamente), estos registros deben aparecer en un checkbox. He intentado hacerlos con php y ajax, pero no lo estoy haciendo de forma correcta.

Este es mi código
una clase con las funciones básicas de postgres
Código PHP:
Ver original
  1. <?
  2. class Cls_conexion
  3. {
  4.  
  5.     function conectar_bd()
  6.     {
  7.     $this->conexion = pg_connect("host='$this->host' dbname='$this->dbname' user='$this->user' password='$this->clave'");
  8.  
  9.     }
  10.     function obtener_objetos($recurso)
  11.     {
  12.         return pg_fetch_object($recurso);
  13.     }
  14.     function obtener_array($recurso)
  15.     {
  16.         return pg_fetch_array($recurso);
  17.     }
  18.     function num_columnas($query)
  19.     {
  20.         return pg_num_rows($query);
  21.     }
  22.     function desconectar_bd()
  23.     {
  24.         pg_close();
  25.     }
  26.     function consultar($sql)
  27.     {
  28.          return pg_query($sql);
  29.     }      
  30. }
  31.  ?>

Un archivo principal en php

Código PHP:
Ver original
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Untitled Document</title>
  5. <?
  6.     include("../Clases/Cls_conexion.php");
  7.     $conexion= new Cls_conexion();
  8.  
  9. ?>
  10. <script src="../js/cambiarmargen.js" language="javascript"></script>
  11. </head>
  12.  
  13. <body>
  14. <?php
  15.     $conexion->conectar_bd();  
  16.     echo $_GET['margen'];
  17.     ?>
  18.     <table>
  19.         <tr>       
  20.         <td>
  21.         <select name="selectmargen" id="selectmargen" onchange="cambiarmargen(selectmargen.value)">
  22.         <option value="">Seleccione...</option>
  23.         <?
  24.         $consulta=$conexion->consultar("select margen from gen_componente group by margen");
  25.         while($registro = $conexion->obtener_objetos($consulta)){
  26.         ?>
  27.             <option value="<?= $registro->margen; ?>"><? echo $registro->margen;?> </option>
  28.            
  29.         <?
  30.         }
  31.         ?>
  32.         </select>          
  33.  
  34.  
  35.         </td>
  36.     </tr>
  37.  
  38.     <tr>       
  39.         <td>
  40.         <select name="selectTramo" id="selectTramo" onchange="cambiarTramo(selectTramo.value)">
  41.         <option value="">Seleccione...</option>
  42.         <?
  43.         $margen=$_GET['margen'];
  44.         $sql="  select a.idtramo,b.nom_tramo,a.id_componente,a.componente
  45.                 from gen_componente as a, gen_tramo as b
  46.                 where a.idtramo=b.id_tramo and a.margen='$margen'";
  47.         $consulta=$conexion->consultar($sql);
  48.         while($registro = $conexion->obtener_objetos($consulta)){
  49.         ?>
  50.             <option value="<?= $registro->idtramo; ?>"><? echo $registro->nom_tramo;?> </option>
  51.            
  52.         <?
  53.         }
  54.         ?>
  55.         </select>          
  56.  
  57.  
  58.         </td>
  59.     </tr>
  60.     <tr>       
  61.         <td>
  62.         <select name="selectTramo" id="selectTramo" onchange="cambiarTramo(selectTramo.value)">
  63.         <option value="">Seleccione...</option>
  64.         <?
  65.         $margen=$_GET['margen'];
  66.         $tramo=$_GET['tramo'];
  67.         $sql="  select a.idtramo,b.nom_tramo,a.id_componente,a.componente
  68.                 from gen_componente as a, gen_tramo as b, gen_estructura as c
  69.                 where a.idtramo=b.id_tramo and a.idestructura=c.id_estructura and a.margen='$margen'";
  70.         $consulta=$conexion->consultar($sql);
  71.         while($registro = $conexion->obtener_objetos($consulta)){
  72.         ?>
  73.             <option value="<?= $registro->idtramo; ?>"><? echo $registro->nom_tramo;?> </option>
  74.            
  75.         <?
  76.         }
  77.         ?>
  78.         </select>          
  79.  
  80.  
  81.         </td>
  82.     </tr>
  83.  
  84.  
  85.     <tr>       
  86.         <td>
  87.         <div style="padding: 4px;
  88.                 width:200px;
  89.                 height:500px;
  90.                 overflow:auto; ">
  91.         <?
  92.         if($_GET['margen']=='')
  93.         {
  94.             $consulta=$conexion->consultar("select * from gen_componente where TRUE");
  95.             while($registro = $conexion->obtener_objetos($consulta)){
  96.                 ?><input type="checkbox" name="seleccionchckbx[]" id="seleccionchckbx" value="<?= $registro->id_componente; ?>">
  97.                 <? echo $registro->componente?><br /></input><?
  98.                 }
  99.  
  100.         }
  101.         else
  102.         {
  103.             if($_GET['margen']=='SUR')
  104.             {
  105.                 $consulta=$conexion->consultar("select * from gen_componente where margen='SUR'");
  106.                     while($registro = $conexion->obtener_objetos($consulta)){
  107.                 ?><input type="checkbox" name="seleccionchckbx[]" id="seleccionchckbx" value="<?= $registro->id_componente; ?>">
  108.                 <? echo $registro->componente?><br /></input><?
  109.                 }
  110.  
  111.             }
  112.             if($_GET['margen']=='NORTE')
  113.             {
  114.                     $consulta=$conexion->consultar("select * from gen_componente where margen='NORTE'");
  115.                 while($registro = $conexion->obtener_objetos($consulta)){
  116.                 ?><input type="checkbox" name="seleccionchckbx[]" id="seleccionchckbx" value="<?= $registro->id_componente; ?>">
  117.                 <? echo $registro->componente?><br /></input><?
  118.                 }
  119.  
  120.             }
  121.  
  122.         }
  123.  
  124.         $conexion->desconectar_bd();
  125.         ?>
  126.         </div>
  127.         <? echo $_GET['valor1'];?>
  128.  
  129.         </td>
  130.     </tr>
  131.     </table>
  132. </body>
  133. </html>

y un archivo en javascript donde envio los valores al formulario con ajax

Código Javascript:
Ver original
  1. function nuevoAjax(){
  2.         var xmlhttp=false;
  3.         try{
  4.                 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  5.         }catch(e){
  6.                 try {
  7.                         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  8.                 }catch(E){
  9.                         xmlhttp = false;
  10.                 }
  11.         }
  12.        
  13.         if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  14.                 xmlhttp = new XMLHttpRequest();
  15.         }
  16.    
  17.         return xmlhttp;
  18. }
  19.  
  20. function cambiarmargen(valor){
  21.         ajax=nuevoAjax();
  22.         ajax.open("GET", "formulario2.php?margen"+valor,true); 
  23. }

Los select deberían estar dispuestos de la forma Margen -> Tramo -> Estructura -> Eje

Para eso tengo 4 tablas gen_componente, gen_tramo, gen_estructura, gen_eje.

Código SQL:
Ver original
  1. CREATE TABLE gen_componente
  2. (
  3.   margen CHARACTER VARYING(32),
  4.   idtramo INTEGER,
  5.   idejeinicio INTEGER,
  6.   idejefin INTEGER,
  7.   idestructura INTEGER,
  8.   componente CHARACTER VARYING(64),
  9.   id_componente serial NOT NULL,
  10.   CONSTRAINT gen_componente_pkey PRIMARY KEY (id_componente)
  11. )
  12. CREATE TABLE gen_estructura
  13. (
  14.   id_estructura serial,
  15.   nom_estructura CHARACTER VARYING(32),
  16.   nom_seccion CHARACTER VARYING(32),
  17.   CONSTRAINT gen_estructura_pkey PRIMARY KEY (id_estructura)
  18. )
  19. CREATE TABLE gen_tramo
  20. (
  21.   id_tramo INTEGER NOT NULL,
  22.   nom_tramo CHARACTER VARYING(64),
  23.   CONSTRAINT gen_tramo_pkey PRIMARY KEY (id_tramo)
  24. )
  25. CREATE TABLE gen_eje
  26. (
  27.   eje CHARACTER VARYING(32),
  28.   id serial NOT NULL,
  29.   CONSTRAINT gen_eje_pkey PRIMARY KEY (id)
  30. )

Casi todos van a llegar a la conclusión de que es un problema de diseño de la base de dates (lo sé) pero tengo que hacerlo de esta forma (más larga y tediosa).

Gracias.