Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/10/2004, 15:02
Avatar de MaBoRaK
MaBoRaK
 
Fecha de Ingreso: abril-2003
Ubicación: La Paz - Bolivia
Mensajes: 2.003
Antigüedad: 21 años
Puntos: 35
Para los que quieran combos desplegables dependientes. con mySQL y Javascript

loading......


Por si a alguien le sirve creo que vi post sobre esto.
Tomé el ejemplo de las FAQ de Javascript sobre como cambiar la ista de un select de acuerdo a otro.
Solo que esta vez sacando datos de las Bases de Datos.
Código PHP:
<?php
$bdconex 
mysql_connect("166.144.10.20""root""");
if(isset(
$_GET['ok']))
{
$os mysql_query("SHOW DATABASES");
$fnd=1;
/*Buscar los valores pasados porque solo los muestra en números
ejemplo: BD= laBDnumero44 --> 44
su tabla: latabla7 --> 7
Osea BD 44 tabla 7
*/
while ($find mysql_fetch_array($os))
{
mysql_select_db($find[0]);
$findTBL mysql_query("SHOW TABLES");
$ftbl=1;
//Comenzar a comparar valores de las cajas
while ($tbb mysql_fetch_array($findTBL))
{
//Si la TABLA es = a la de la caja salimos
if($ftbl == $_GET['latabla'])
{
$TABLA=$tbb[0];
break;
}
$ftbl++;
}
//Si la BD es = a la de la caja salimos
if($fnd == $_GET['laBD'])
{
$BD=$find[0];
break;
}
$fnd++;
}
//[MOSTRAR LO QUE SE ELIGIÓ]
echo"<u><b>BD elegida:</b></u>".$BD;
echo 
"<br><u><b>TABLA Elegida:</b></u>".$TABLA;
//[/MOSTRAR LO QUE SE ELIGIÓ]
}
else
{}
?>
<html>
 <head>
  <script language="JavaScript">
   var alaBDes = new Array(
<?php
$az 
mysql_query("SHOW DATABASES");
echo
"\"\"";
//Generar el Array de BD´s para la Primera Caja
while ($bd mysql_fetch_array($az)) {
echo 
",\"".$bd[0]."\"";
}
echo
");
var aTABLITAS0 = new Array(\"\");"
;
$ay mysql_query("SHOW DATABASES");
$a=1;
//Generar los Arrays de acuerdo a la cantidad de BD´s
while ($bd mysql_fetch_array($ay)) {
echo
"aTABLITAS$a = new Array(";
mysql_select_db($bd[0]);
$tb mysql_query("SHOW TABLES");
$a2=0;
echo
"\"\"";
//GEnerar el contenido de los Arrays de cada BD
while ($b mysql_fetch_array($tb)) {
echo 
",\"".$b[0]."\"";
$a2++;
}
mysql_free_result($tb);
echo
");

"
;
$a++;
}
echo
"var aTABLITAS = new Array(
aTABLITAS0"
;

$x=1;

$ah mysql_query("SHOW DATABASES");
while (
$b mysql_fetch_array($ah)) {
echo
",aTABLITAS$x
"
;
$x++;
}
echo
");";



?>

function opcion(oCntrl, iPos, sTxt, sVal){
     var selOpcion=new Option(sTxt, sVal);
     eval(oCntrl.options[iPos]=selOpcion);
   }

function cambia(oMster, oCntrl){
    var nSelected = oMster.selectedIndex;
    while (oCntrl.length) oCntrl.remove(0);
    for(var i = 0; i < aTABLITAS[nSelected].length; i++)
     opcion(oCntrl,  i, aTABLITAS[nSelected][i], String(i));
   }

function llena(oCntrl){
    while (oCntrl.length) oCntrl.remove(0);
    for(var i = 0; i < alaBDes.length; i++)
     opcion(oCntrl,  i, alaBDes[i], String(i));
   }


</script>


 </head>
 <body>
<?php
echo"<form name=frm action=".$_SERVER['PHP_SELF']." method=GET>";
?>
   laBD
   <select name="laBD" onchange="cambia(this, document.frm.latabla)">
    <option value=" ">&nbsp;</option>
   </select>
   &nbsp;&nbsp;&nbsp;
   latabla
   <select name="latabla">
    <option value=" ">&nbsp;</option>
   </select>
<input type=submit name='ok' value='Comprobar'>
</form>
  <script language="JavaScript">
   llena(document.frm.laBD);
</script>
 </body>
</html>
Espero no sea tarde.



connection closed.
__________________

Maborak Technologies

Última edición por MaBoRaK; 14/10/2004 a las 19:46