Ver Mensaje Individual
  #86 (permalink)  
Antiguo 07/10/2005, 13:10
Avatar de Saruman
Saruman
 
Fecha de Ingreso: mayo-2003
Ubicación: Panama city, Panama, Panama
Mensajes: 1.154
Antigüedad: 20 años, 11 meses
Puntos: 5
Cargar SELECT con datos de la Base de Datos

En este ejemplo presento como cargar datos de una base de datos a dos select, y que estos cambien automáticamente.


Código:
 
<%
 'Función para conectarse a la Base de Datos
 
 'strStringConection = 0 (SQL Server 2000)  1 (MS Access)
 'strDBPath = ruta donde está la BD
 'strDBName = nombre de la BD (en el caso que sea SQL Server 2000)
 'strDBUserId = nombre del usuario (en el caso que sea SQL Server 2000)
 'strDBPassword = password de la BD
 
 Function DBOpenConection()
  Dim Master
  Dim strSringConection
  
  select case strDBType
   case 0
    strStringConection = "Provider=sqloledb;" & _ 
          "Data Source=" & strDBPath & ";" & _
          "Persist Security Info=False;Trusted_Connection=false;" & _
          "Initial Catalog=" & strDBName & ";" & _
          "User Id=" & strDBUserId & ";" & _
          "Password=" & strDBPassword
   case 1
    strStringConection = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
          "Data Source=" & Server.MapPath(strDBPath) & ";" & _
          "Jet OLEDB:Database Password=" & strDBPassword
  end select
  
  Set Master = Server.CreateObject("ADODB.Connection")
  Master.Open strStringConection
  Set DBOpenConection = Master
 End Function
 
 
 set Master = DBOpenConection()
 
 
 provincia = request("provincia")
 if provincia = "" then provincia = 0
 
 'TABLA PROVINCIAS:
  'codigo = numerico
  'nombre = texto
 
 'TABLA DISTRITOS
  'codigo = numerico
  'nombre = texto
  'provincia_codigo = numerico   --   este campo va relacionado con codigo de provincia en la tabla PROVINCIAS
 
 
 sSQL = "select * from PROVINCIA order by codigo"
 set RSProvincias = Master.Execute(sSQL)
 
 sSQL = "select * from DISTRITOS where provincia_codigo=" & provincia & " order by nombre"
 set RSDistritos = Master.Execute(sSQL)
%>
Código PHP:
<table border="0" cellspacing="2" cellpadding="2" class="TablaSimple">
  <
tr>
    <
td width="80">Provincia:</td>
    <
td>
 <
select name="provincia" onChange="load_distritos(this.value)">
<%
 if 
RSProvincias.bof=false and RSProvincias.eof=false then
  
if provincia 0 then response.Write(vbtab "<option value=""0"" selected>Seleccione una Provincia</option>" vbcrlf)
  while 
not RSProvincias.eof
   
if clng(RSProvincias("codigo")) = provincia then
    response
.Write(vbtab "<option value=""" RSProvincias("codigo") & """ selected>" RSProvincias("nombre") & "</option>" vbcrlf)
   else
    
response.Write(vbtab "<option value=""" RSProvincias("codigo") & """>" RSProvincias("nombre") & "</option>" vbcrlf)
   
end if
   
RSProvincias.movenext
  wend
  
  RSProvincias
.movefirst
 
else
  
response.Write(vbtab "<option value=""0"" selected>No Existe ninguna Provincia</option>" vbcrlf)
 
end if
%>
    </
select>
 </
td>
  </
tr>
  <
tr>
   <
td>Distrito:</td>
 <
td>
 <
select name="distrito">
<%
 if 
RSDistritos.bof=false and RSDistritos.eof=false then
  
if distrito 0 then response.Write(vbtab "<option value=""0"" selected>Seleccione un Distrito</option>" vbcrlf)
  while 
not RSDistritos.eof
   
if clng(RSDistritos("codigo")) = distrito then
    response
.Write(vbtab "<option value=""" RSDistritos("codigo") & """ selected>" RSDistritos("nombre") & "</option>" vbcrlf)
   else
    
response.Write(vbtab "<option value=""" RSDistritos("codigo") & """>" RSDistritos("nombre") & "</option>" vbcrlf)
   
end if
   
RSDistritos.movenext
  wend
 
else
  
response.Write(vbtab "<option value=""0"" selected>Seleccione un Distrito</option>" vbcrlf)
 
end if
%>
 </
select>
 </
td>
  </
tr>
</
table>
<
script language="javascript">
 function 
load_distritos(provincia) {
<%
 
0
 
while not RSProvincias.eof
  sSQL 
"select * from distritos where provincia_codigo=" RSProvincias("codigo") & " order by nombre"
  
set RSDistritos Master.Execute(sSQL)
  
  
0
  response
.Write(vbtab vbtab "if (provincia == " RSProvincias("codigo") & ") {") & vbcrlf
  response
.Write(vbtab vbtab vbtab "f.distrito.options.length = 0;") & vbcrlf
  response
.Write(vbtab vbtab vbtab "sub" " = new Option(""Seleccione un Distrito"",""0"",""defauldSelected"");") & vbcrlf
  response
.Write(vbtab vbtab vbtab "f.distrito.options[" "] = sub" ";") & vbcrlf
  J 
1
  
if RSDistritos.bof=false and RSDistritos.eof=false then
   
while not RSDistritos.eof
    distrito_codigo 
RSDistritos("codigo")
    
distrito_nombre RSDistritos("nombre")
    
response.Write(vbtab vbtab vbtab "sub" " = new Option(""" distrito_nombre """,""" distrito_codigo ""","""");") & vbcrlf
    response
.Write(vbtab vbtab vbtab "f.distrito.options[" "] = sub" ";") & vbcrlf
    J 
1
    RSDistritos
.movenext
   wend
  
else
   
response.Write(vbtab vbtab vbtab "sub" " = new Option(""No Existe ningún Distrito en esta Provincia"",""0"","""");") & vbcrlf
   response
.Write(vbtab vbtab vbtab "f.distrito.options[" "] = sub" ";") & vbcrlf
  end 
if
  
  
set RSDistritos nothing
  
  response
.Write(vbtab vbtab "}") & vbcrlf
  
  I 
1
  RSProvincias
.movenext
 wend
 
 set RSProvincias 
nothing
%>
 }
</script> 
Saludos
__________________
Saruman

One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them.