Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2001
    residenza
    Napoli, Italia
    Messaggi
    1,074

    ricerca con quattro input diversi

    Ho un form di ricerca, nel quale l'utente può scegliere sino a quattro input di ricerca, in modo da affinare al massimo la ricerca stessa.
    Dopo aver controllato che almeno uno dei campi di input non sia vuoto, effettuo la ricerca, così
    Codice PHP:
    $result mysql_query("SELECT * FROM `news` WHERE `title` LIKE '%$title%' && `tag` LIKE '%$tag%' && `text` LIKE '%$text%' && `location` LIKE '%$location%' ORDER BY `news_id` DESC"); 
    però invariabilmente non mi restituisce alcun risultato.
    Se, invece di && scrivo || allora funziona...
    Ma così - ovviamente - non posso ottenere la combinazione tra due o più imput, se l'utente ne immette più di uno...
    Come si può risolvere?
    La cosa strana e che per un pò sembrava funzionare!!!
    (Ma questa è la metafisica della programmazione...)
    metatad
    graphic & web design

  2. #2
    devi fare dei controlli se le variabili della query esistono non puoi scrivere una richiesta al db in questo modo ne con && o || perchè il risultato è sempre sbagliato.

    Al posto dei like inserisci una variabile, poi esegui dei controlli sui campi del motore e inserisci il valore della variabile a seconda del risultato ottenuto.


  3. #3
    Secondo me devi creare tot query secondo queasto schema

    0 0 0 0
    1 0 0 0
    1 1 0 0
    1 1 1 0
    1 1 1 1
    0 1 0 0
    0 1 1 0
    0 1 1 1
    0 0 1 0
    0 0 1 1
    0 0 0 1
    1 0 1 0
    1 0 0 1
    1 0 1 1
    1 1 0 1
    etc. etc.

    I parametri son 4, le query le devi costruire in base ai valori dei campi, se son vuoi (0) oppure no (1) con degli IF.

    E' lunga ma non conosco altre vie....... ma io non faccio testo, ti ho scritto solo come ho fatto io.

    Ciao.

  4. #4
    Utente di HTML.it
    Registrato dal
    Sep 2001
    residenza
    Napoli, Italia
    Messaggi
    1,074
    scusa, non ho mica capito...
    cosa significa:
    Al posto dei like inserisci una variabile, poi esegui dei controlli sui campi del motore e inserisci il valore della variabile a seconda del risultato ottenuto.
    metatad
    graphic & web design

  5. #5
    Utente di HTML.it
    Registrato dal
    Sep 2001
    residenza
    Napoli, Italia
    Messaggi
    1,074
    Già, l'ipotesi di creare 15 diverse condizioni l'avevo considerata, ma speravo ci fosse un'altro modo...
    Comunque, anche così (sgrunt! ) non funziona!
    Ho fatto
    Codice PHP:
    if ($title !== "" && $text == "" && $tag == "" && $location == "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `title` LIKE '%$title%' ORDER BY `news_id` DESC");    
    } else if (
    $text !== "" && $title == "" && $tag == "" && $location == "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `text` LIKE '%$text%' ORDER BY `news_id` DESC");
    } else if (
    $tag !== "" && $text == "" && $title == "" && $location == "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `tag` LIKE '%$tag%' ORDER BY `news_id` DESC");    
    } else if (
    $location !== "" && $text == "" && $tag == "" && $title == "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `location` LIKE '%$location%' ORDER BY `news_id` DESC");
    } else if (
    $title !== "" && $text !== "" && $tag == "" && $location == "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `title` LIKE '%$title%' && `text` LIKE '%$text%' ORDER BY `news_id` DESC");
    } else if (
    $title !== "" && $text == "" && $tag !== "" && $location == "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `title` LIKE '%$title%' && `tag` LIKE '%$tag%' LIKE '%$location%' ORDER BY `news_id` DESC");
    } else if (
    $title !== "" && $text == "" && $tag == "" && $location !== "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `title` LIKE '%$title%' && `location` LIKE '%$location%' ORDER BY `news_id` DESC");
    } else if (
    $title == "" && $text !== "" && $tag !== "" && $location == "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `tag` LIKE '%$tag%' && `text` LIKE '%$text%' ORDER BY `news_id` DESC");
    } else if (
    $title == "" && $text !== "" && $tag == "" && $location !== "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `location` LIKE '%$location%' && `text` LIKE '%$text%' ORDER BY `news_id` DESC");
    } else if (
    $title == "" && $text == "" && $tag !== "" && $location !== "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `tag` LIKE '%$tag%' && `location` LIKE '%$location%' ORDER BY `news_id` D?????iESC");
    } else if (
    $title !== "" && $text !== "" && $tag !== "" && $location == "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `title` LIKE '%$title%' && `tag` LIKE '%$tag%' && `text` LIKE '%$text%' ORDER BY `news_id` DESC");
    } else if (
    $title !== "" && $text !== "" && $tag == "" && $location !== "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `title` LIKE '%$title%' && `text` LIKE '%$text%' && `location` LIKE '%$location%' ORDER BY `news_id` DESC");
    } else if (
    $title !== "" && $text == "" && $tag !== "" && $location !== "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `title` LIKE '%$title%' && `tag` LIKE '%$tag%' && `location` LIKE '%$location%' ORDER BY `news_id` DESC");
    } else if (
    $title == "" && $text !== "" && $tag !== "" && $location !== "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `tag` LIKE '%$tag%' && `text` LIKE '%$text%' && `location` LIKE '%$location%' ORDER BY `news_id` DESC");    
    } else if (
    $title !== "" && $text !== "" && $tag !== "" && $location !== "") {
        
    $result mysql_query("SELECT * FROM `news` WHERE `title` LIKE '%$title%' && `tag` LIKE '%$tag%' && `text` LIKE '%$text%' && `location` LIKE '%$location%' ORDER BY `news_id` DESC");    

    ma non funge...
    metatad
    graphic & web design

  6. #6
    nn ho capito esattamente il problema...
    ma se facessi una cosa di questo tipo?

    Codice PHP:
    $arr = array();

    if (!
    emtpy($title)) $arr[] .= " `title` LIKE '%$title%'";
    if (!
    emtpy($tag)) $arr[] .= " `tag` LIKE '%$tag%'";
    if (!
    emtpy($text)) $arr[] .= " 'text` LIKE '%$text%'";
    if (!
    emtpy($location)) $arr[] .= " `location` LIKE '%$location%'";
    //puoi aggiungere quante condizioni desideri...sulla base dello schema precedente...

    if (count($arr)>0){ //verifico se è stata impostata almeno una condizione
       
    $where '';
       foreach(
    $arr as $i => $condizione){
          if (
    $i>0$where .= ' && '//dalla seconda condizione in poi aggiunge &&
          
    $where .= $condizione;
       }

       
    $query "SELECT * FROM `news` WHERE ".$where." ORDER BY `news_id` DESC";
       
    //qui fai tutte le operazioni...esegui la query, leggi i record estratti ecc


    }else{ //nessuna delle condizioni è stata impostata
       
    die('Devi impostare almeno una condizione');

    probabilmente ho fatto degli errori di sintassi...
    però in teoria dovrebbe fungere...

    ciauz

  7. #7
    piccolo Orrore... :-D

    Codice PHP:
    $arr = array(); 

    if (!
    emtpy($title)) $arr[] = " `title` LIKE '%$title%'"
    if (!
    emtpy($tag)) $arr[] = " `tag` LIKE '%$tag%'"
    if (!
    emtpy($text)) $arr[] = " `text` LIKE '%$text%'"
    if (!
    emtpy($location)) $arr[] = " `location` LIKE '%$location%'"
    //puoi aggiungere quante condizioni desideri...sulla base dello schema precedente... 

    if (count($arr)>0){ //verifico se è stata impostata almeno una condizione 
       
    $where ''
       foreach(
    $arr as $i => $condizione){ 
          if (
    $i>0$where .= ' && '//dalla seconda condizione in poi aggiunge && 
          
    $where .= $condizione
       } 

       
    $query "SELECT * FROM `news` WHERE ".$where." ORDER BY `news_id` DESC"
       
    //qui fai tutte le operazioni...esegui la query, leggi i record estratti ecc 


    }else{ //nessuna delle condizioni è stata impostata 
       
    die('Devi impostare almeno una condizione'); 


Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.