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
<? class Cls_conexion { function conectar_bd() { $this->conexion = pg_connect("host='$this->host' dbname='$this->dbname' user='$this->user' password='$this->clave'"); } function obtener_objetos($recurso) { } function obtener_array($recurso) { } function num_columnas($query) { } function desconectar_bd() { } function consultar($sql) { } } ?>
Un archivo principal en php
Código PHP:
Ver original
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <? include("../Clases/Cls_conexion.php"); $conexion= new Cls_conexion(); ?> <script src="../js/cambiarmargen.js" language="javascript"></script> </head> <body> <?php $conexion->conectar_bd(); echo $_GET['margen']; ?> <table> <tr> <td> <select name="selectmargen" id="selectmargen" onchange="cambiarmargen(selectmargen.value)"> <option value="">Seleccione...</option> <? $consulta=$conexion->consultar("select margen from gen_componente group by margen"); while($registro = $conexion->obtener_objetos($consulta)){ ?> <option value="<?= $registro->margen; ?>"><? echo $registro->margen;?> </option> <? } ?> </select> </td> </tr> <tr> <td> <select name="selectTramo" id="selectTramo" onchange="cambiarTramo(selectTramo.value)"> <option value="">Seleccione...</option> <? $margen=$_GET['margen']; $sql=" select a.idtramo,b.nom_tramo,a.id_componente,a.componente from gen_componente as a, gen_tramo as b where a.idtramo=b.id_tramo and a.margen='$margen'"; $consulta=$conexion->consultar($sql); while($registro = $conexion->obtener_objetos($consulta)){ ?> <option value="<?= $registro->idtramo; ?>"><? echo $registro->nom_tramo;?> </option> <? } ?> </select> </td> </tr> <tr> <td> <select name="selectTramo" id="selectTramo" onchange="cambiarTramo(selectTramo.value)"> <option value="">Seleccione...</option> <? $margen=$_GET['margen']; $tramo=$_GET['tramo']; $sql=" select a.idtramo,b.nom_tramo,a.id_componente,a.componente from gen_componente as a, gen_tramo as b, gen_estructura as c where a.idtramo=b.id_tramo and a.idestructura=c.id_estructura and a.margen='$margen'"; $consulta=$conexion->consultar($sql); while($registro = $conexion->obtener_objetos($consulta)){ ?> <option value="<?= $registro->idtramo; ?>"><? echo $registro->nom_tramo;?> </option> <? } ?> </select> </td> </tr> <tr> <td> <div style="padding: 4px; width:200px; height:500px; overflow:auto; "> <? if($_GET['margen']=='') { $consulta=$conexion->consultar("select * from gen_componente where TRUE"); while($registro = $conexion->obtener_objetos($consulta)){ ?><input type="checkbox" name="seleccionchckbx[]" id="seleccionchckbx" value="<?= $registro->id_componente; ?>"> <? echo $registro->componente?><br /></input><? } } else { if($_GET['margen']=='SUR') { $consulta=$conexion->consultar("select * from gen_componente where margen='SUR'"); while($registro = $conexion->obtener_objetos($consulta)){ ?><input type="checkbox" name="seleccionchckbx[]" id="seleccionchckbx" value="<?= $registro->id_componente; ?>"> <? echo $registro->componente?><br /></input><? } } if($_GET['margen']=='NORTE') { $consulta=$conexion->consultar("select * from gen_componente where margen='NORTE'"); while($registro = $conexion->obtener_objetos($consulta)){ ?><input type="checkbox" name="seleccionchckbx[]" id="seleccionchckbx" value="<?= $registro->id_componente; ?>"> <? echo $registro->componente?><br /></input><? } } } $conexion->desconectar_bd(); ?> </div> <? echo $_GET['valor1'];?> </td> </tr> </table> </body> </html>
y un archivo en javascript donde envio los valores al formulario con ajax
Código Javascript:
Ver original
function nuevoAjax(){ var xmlhttp=false; try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch(E){ xmlhttp = false; } } if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); } return xmlhttp; } function cambiarmargen(valor){ ajax=nuevoAjax(); ajax.open("GET", "formulario2.php?margen"+valor,true); }
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
CREATE TABLE gen_componente ( margen CHARACTER VARYING(32), idtramo INTEGER, idejeinicio INTEGER, idejefin INTEGER, idestructura INTEGER, componente CHARACTER VARYING(64), id_componente serial NOT NULL, CONSTRAINT gen_componente_pkey PRIMARY KEY (id_componente) ) CREATE TABLE gen_estructura ( id_estructura serial, nom_estructura CHARACTER VARYING(32), nom_seccion CHARACTER VARYING(32), CONSTRAINT gen_estructura_pkey PRIMARY KEY (id_estructura) ) CREATE TABLE gen_tramo ( id_tramo INTEGER NOT NULL, nom_tramo CHARACTER VARYING(64), CONSTRAINT gen_tramo_pkey PRIMARY KEY (id_tramo) ) CREATE TABLE gen_eje ( eje CHARACTER VARYING(32), id serial NOT NULL, CONSTRAINT gen_eje_pkey PRIMARY KEY (id) )
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.