setting different query parameters from multiple dropdown box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raaman rai
    New Member
    • Oct 2007
    • 114

    setting different query parameters from multiple dropdown box

    i have 3 drop dropdown box which is used for searching my database. Either one of them can be selected to perform the search but if none of them is selected it will give an error. Well in reference to this i want the sql query to be based on the values selected by the users. A user might select value from one dropdown box and leave the others or a user can also select values from all three dropdown box. So in this case the query parameters for my query should change accordingly. How is it done in PHP? Infact i tried doing it but i was not successful. Please refer to the code below:[PHP] <?php
    $today=date('Y-m-d', mktime());

    $q1=$_SESSION['c1'];
    $q2=$_SESSION['c2'];
    $q3=$_SESSION['c3'];

    if(!$q1=="" || $q2=="" || $q3=="") {
    $qry = "and tblTenderinfo.c lass='$q1'";
    }
    elseif(!$q1=="" || !$q2=="" || $q3==""){
    $qry = "and tblTenderinfo.c lass='$q1' and tbltenderinfo.c ategory='$q2'";
    }
    elseif(!$q1=="" || $q2=="" || !$q3==""){
    $qry = "and tblTenderinfo.c lass='$q1' and tblclient.categ ory='$q3'";
    }
    elseif($q1=="" || !$q2=="" || $q3=="") {
    $qry = "and tbltenderinfo.c ategory='$q2'";
    }
    elseif($q1=="" || $q2=="" || !$q3=="") {
    $qry = "and tblclient.categ ory='$q3'";
    }

    /*$qry = "and tblTenderinfo.c lass='$q1' and tbltenderinfo.c ategory='$q2' and tblclient.categ ory='$q3'";*/

    ?>
    <h3>Tender Information for Class <?php echo $q1; ?> Contractors</h3>
    <?php
    $q = "SELECT tblTenderinfo.t enderId, tblTenderinfo.t enderDesc, tblTenderinfo.c lient,
    tblTenderinfo.s ubmdate,tblclie nt.clientagency from tblTenderinfo inner join tblclient
    on tblTenderinfo.c lient=tblclient .clientid where tblTenderinfo.s ubmdate>='".$to day."' $qry ORDER BY tblTenderinfo.s ubmdate ASC";[/PHP]
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    You could use implode to make this easier.

    Like:
    [code=php]
    // Set up and an array for the implode function
    $constraints = array();

    // Check each input
    if(isset($_GET['var1'])) {
    $safe = mysql_real_esca pe_string($_GET['var1']);
    $constraints[] = "`col1` = '$safe'";
    }
    if(isset($_GET['var2'])) {
    $safe = mysql_real_esca pe_string($_GET['var2']);
    $constraints[] = "`col2` = '$safe'";
    }
    // etc...

    // Create the WHERE clause using the implode function
    $whereString = implode(" AND ", $constraints);

    // Create the SQL query
    $query = "SELECT stuff FROM myTbl WHERE $whereString";

    // etc..


    [/code]

    Comment

    • raaman rai
      New Member
      • Oct 2007
      • 114

      #3
      thankyou somuch, it worked.

      Comment

      Working...