Foros del Web » Programando para Internet » PHP »

Consulta SQL dinámica > Ayuda / Cómo ?

Estas en el tema de Consulta SQL dinámica > Ayuda / Cómo ? en el foro de PHP en Foros del Web. Hola, tengo la siguiente consulta: Código PHP: $sql  =  "select IdUsuario, Descripcion from usuarios " . $where . " order by rand() limit 1" ;  en donde el $where depende de 5 Lists/Menús que entregan ...
  #1 (permalink)  
Antiguo 03/07/2005, 22:33
Avatar de nachopro  
Fecha de Ingreso: noviembre-2003
Ubicación: Haedo, Bs. As.
Mensajes: 290
Antigüedad: 20 años, 5 meses
Puntos: 2
Consulta SQL dinámica > Ayuda / Cómo ?

Hola, tengo la siguiente consulta:
Código PHP:
$sql "select IdUsuario, Descripcion from usuarios ".$where." order by rand() limit 1"
en donde el $where depende de 5 Lists/Menús que entregan mediante POST sus valores (numéricos).

no sé como generar el where, ya que cuando una option del select list/menu vale 0 no se debe filtar por su columna.. estoy liado con esto..
realicé un script mediocre...
Código PHP:
    $where "where ";
    if (
$_POST['Sexo'] != 0) {
        
$where .= "Sexo = '".$_POST['Sexo']."' ";
    }
    if (
$_POST['Orientacion'] != 0) {
        
$where .= "Orientacion = '".$_POST['Orientacion']."' ";
    }
    if (
$_POST['Edad'] != 0) {
        
$where .= "Edad = '".$_POST['Edad']."' ";
    }
    if (
$_POST['Pais'] != 0) {
        
$where .= "Pais = '".$_POST['Pais']."' ";
    }
    if (
$_POST['Provincia'] != 0) {
        
$where .= "Provincia = '".$_POST['Provincia']."' ";
    } 
que no contempla los "and" o el caso de que todos los $_POST valgan 0

me dan una ayuda?
con ifs es complicado, con switchs no se puede...
tal vez con arrays??

espero respuestas, gracias
  #2 (permalink)  
Antiguo 03/07/2005, 23:40
Avatar de nachopro  
Fecha de Ingreso: noviembre-2003
Ubicación: Haedo, Bs. As.
Mensajes: 290
Antigüedad: 20 años, 5 meses
Puntos: 2
luego de , y se me ocurrió esto:
Código PHP:
    if ($_POST['Sexo'] != 0) {
        if (
$where == "") {
            
$where "where ";
        }
        else {
            
$where .= "and ";
        }
        
$where .= "Sexo = '".$_POST['Sexo']."' ";
    }
    if (
$_POST['Orientacion'] != 0) {
        if (
$where == "") {
            
$where "where ";
        }
        else {
            
$where .= "and ";
        }
        
$where .= "Orientacion = '".$_POST['Orientacion']."' ";
    }
    if (
$_POST['Edad'] != 0) {
        if (
$where == "") {
            
$where "where ";
        }
        else {
            
$where .= "and ";
        }
        
$where .= "Edad = '".$_POST['Edad']."' ";
    }
    if (
$_POST['Pais'] != 0) {
        if (
$where == "") {
            
$where "where ";
        }
        else {
            
$where .= "and ";
        }
        
$where .= "Pais = '".$_POST['Pais']."' ";
    }
    if (
$_POST['Provincia'] != 0) {
        if (
$where == "") {
            
$where "where ";
        }
        else {
            
$where .= "and ";
        }
        
$where .= "Provincia = '".$_POST['Provincia']."' ";
    } 
no era tan complicado..

espero que les pueda llegar a ser de utilidad
  #3 (permalink)  
Antiguo 05/07/2005, 00:52
Avatar de jam1138
/** @package Moderador */
 
Fecha de Ingreso: julio-2004
Ubicación: sèveR led onieR lE
Mensajes: 9.368
Antigüedad: 19 años, 9 meses
Puntos: 102
Bien que lo lograste ... pero se puede simplificar por mucho... una idea: http://www.forosdelweb.com/f18/hacer-consulta-multiple-267358/
y se puede mejorar más...

Igual espero te sea de utilidad.
__________________
٩(͡๏̯͡๏)۶
» Cómo hacer preguntas de manera inteligente «

"100 años después, la revolución no es con armas, es intelectual y digital"
  #4 (permalink)  
Antiguo 05/07/2005, 01:55
Avatar de jam1138
/** @package Moderador */
 
Fecha de Ingreso: julio-2004
Ubicación: sèveR led onieR lE
Mensajes: 9.368
Antigüedad: 19 años, 9 meses
Puntos: 102
Otra: http://www.forosdelweb.com/showpost....47&postcount=2
__________________
٩(͡๏̯͡๏)۶
» Cómo hacer preguntas de manera inteligente «

"100 años después, la revolución no es con armas, es intelectual y digital"
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:34.