Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2008
    Messaggi
    57

    Select solo su alcuni campi

    Ciao ragazzi, in una pagina ho una form con alcuni campi che descrivono un vestito (tipo,colore,nome,taglia ecc...), e possibile fare in modo che l'utente inserisca solo alcuni campi (lasciando gli altri vuoti) e facendo in modo di utilizzare i campi inseriti per generare una query?
    Per esempio voglio dare la possibilità all'utente di cercare tutti gli abiti "rossi" di tipo "t-shirt".
    Nell'esempio di cui sopra l'utente riempirebbe solo in campo "colore" e "tipologia" lasciando gli altri in bianco.
    Nella pagina php che agisce sul form vorrei poter fare una query con i soli campi "colore" e "tipologia".
    Grazie mille in anticipo!

  2. #2
    Utente di HTML.it L'avatar di homerbit
    Registrato dal
    Dec 2005
    residenza
    Roma
    Messaggi
    1,380
    l'obbligatorietà di compilazione\selezione dei campi è a tua discrezione, sei tu che stabilisci quali campi debbano essere obbligatoriamente compilati, con opportuni controlli (javascript e\o php)
    Ottenuti i campi mediante $_POST[] crei la query
    If you think your users are idiots, only idiots will use it. DropBox

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2008
    Messaggi
    57
    la richiestra in realtà era proprio su come fare la query se alcuni campi sono vuoti.
    Se con il form mando tutti i campi tramite POST nella pagina php li ricevo tutti, alcuni vuoti e alcuni "pieni".
    Come faccio ad inserire i pieni in una query?
    Ho provato a fare una cosa del genere ma ho problemi con la condizione WHERE:
    Codice PHP:
    <?php
    //Retreive information from form
     
    $kind=$_POST["kind"]; 
    $position=$_POST["position"]; 
    $size=$_POST["size"]; 
    $price=$_POST["price"]; 
    $event=$_POST["event"];
     
    $weather=$_POST["weather"];
     
    $name=$_POST["name"]; 
    $pur_date=$_POST["pur_date"]; 
    $color=$_POST["color"]; 
                    if (!empty(
    $kind)){
                                                    
    $campi="kind"." ";        
                    
    $valorekind=$kind;                   
         
    $where="kind="."'".$kind."'"."AND ";       
                     }          
      if (!empty(
    $position)){          
                             
    $campi=$campi."position"." "
                           
    $valoreposition=$position;             
                       
    $where=$where."position="."'".$position."'"." AND "
                           }         
                   if (!empty(
    $size)){  
                           
    $campi=$campi."size"." ";  
                          
    $valoresize=$size;                
                       
    $where="size="."'".$size."'"." AND ";          
                  }             
               if (!empty(
    $price)){     
                                               
    $campi=$campi."price"." ";  
                          
    $valoreprice=$price;                   
         
    $where="price="."'".$price."'"." AND ";            
                }                     
       if (!empty(
    $event)){         
                             
    $campi=$campi."event"." "
                           
    $valoreevent=$event;      
                           
    $where="event="."'".$event."'"." AND ";  
                          }                     
       if (!empty(
    $weather)){            
                                        
    $campi=$campi."weather"." ";    
                        
    $valoreweather=$weather;                   
         
    $where="weather="."'".$weather."'"." AND ";                
            }             
               if (!empty(
    $name)){                         
                     
    $campi=$campi."name"." ";                      
                   
    $valorename=$name;                
                  
    $where="name="."'".$name."'"." AND ";   
                         }                   
         if (!empty(
    $pur_date)){              
                           
    $campi=$campi."pur_date"." ";  
                          
    $valorepur_date=$pur_date;              
                          
    $where="pur_date="."'".$pur_date."'"." AND ";      
                      }                    
        if (!empty(
    $color)){                   
                          
    $campi=$campi."color"." ";   
                         
    $valorecolor=$color;                  
                      
    $where=$where."color="."'".$color."'"." AND ";   
                         }                     
       
    $query"SELECT ".$campi."FROM clothes WHERE ".$where ;echo($query);?>
    Ultima modifica di idnob; 10-11-2013 a 17:24

  4. #4
    Utente di HTML.it
    Registrato dal
    Feb 2008
    Messaggi
    57
    Ho risolto così:
    Codice PHP:
    <?phpecho("<link rel='stylesheet' type='text/css' href='table.css'>");
    //Retreive information from form
     
    $kind=$_POST["kind"]; $position=$_POST["position"]; $size=$_POST["size"]; $price=$_POST["price"]; $event=$_POST["event"]; $weather=$_POST["weather"]; $name=$_POST["name"]; $pur_date=$_POST["pur_date"]; $color=$_POST["color"]; $where="1=1";                if (!empty($kind)){                                                $campi="kind"." ";                        $valorekind=$kind;                        $where=$where." AND kind="."'".$kind."'";                        }            if (!empty($position)){                                                $campi=$campi."position"." ";                        $valoreposition=$position;                        $where=$where." AND position="."'".$position."'";                        }                        if (!empty($size)){                                                $campi=$campi."size"." ";                        $valoresize=$size;                        $where=$where." AND size="."'".$size."'";                        }                        if (!empty($price)){                                                $campi=$campi."price"." ";                        $valoreprice=$price;                        $where=$where." AND price="."'".$price."'";                        }                        if (!empty($event)){                                                $campi=$campi."event"." ";                        $valoreevent=$event;                        $where=$where." AND event="."'".$event."'";                        }                        if (!empty($weather)){                                                $campi=$campi."weather"." ";                        $valoreweather=$weather;                        $where=$where." AND weather="."'".$weather."'";                        }                        if (!empty($name)){                                                $campi=$campi."name"." ";                        $valorename=$name;                        $where=$where." AND name="."'".$name."'";                        }                        if (!empty($pur_date)){                                                $campi=$campi."pur_date"." ";                        $valorepur_date=$pur_date;                        $where=$where." AND pur_date="."'".$pur_date."'"." AND ";                        }                        if (!empty($color)){                                                $campi=$campi."color"." ";                        $valorecolor=$color;                        $where=$where." AND color="."'".$color."'";                        }                        $query"SELECT *FROM clothes WHERE ".$where ;                        //$query= "SELECT ".$campi."FROM clothes WHERE ".$where ;//echo($query);//Connection        $link = mysql_connect('localhost', 'idnob', 'Zenemecca5689');        if (!$link) { die('Could not connect: ' . mysql_error()); }        //Select DB        mysql_select_db(my_idnob) or die ("Error in selection database ".mysql_error());$result=mysql_query($query) or die (mysql_error());echo("<table>");echo("    <tr> <td>Name</td> <td>Kind</td> <td>Position</td> <td>Size</td> <td>Price</td> <td>Event</td> <td>Weather</td> <td>Purchase Date</td> <td>Color</td> <td>Image</td></tr>");
    //if (empty($result)){echo ("No clothes for the specified details.");}//else{while($row = mysql_fetch_array($result))          {              echo "<tr>";            echo "<td>" . $row['name'] . "</td>";              echo "<td>" . $row['kind'] . "</td>";              echo "<td>" . $row['position'] . "</td>";              echo "<td>" . $row['size'] . "</td>";              echo "<td>" . $row['price'] . "</td>";                        // or group by position              echo "<td>" . $row['event'] . "</td>";                        // or left bottom, center middle, right top              echo "<td>" . $row['weather'] . "</td>";              echo "<td>" . $row['pur_date'] . "</td>";              echo "<td>" . $row['color'] . "</td>";              echo "<td><img src='upload/" . $row['image'] . "' height='70' width='70' alt='".$row['name']."'></td>";                            echo "</tr>";          }        echo "</table>";        echo "</div>";//}

    ?>

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 © 2025 vBulletin Solutions, Inc. All rights reserved.