By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

setting different query parameters from multiple dropdown box

100+
P: 114
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.class='$q1'";
}
elseif(!$q1=="" || !$q2=="" || $q3==""){
$qry = "and tblTenderinfo.class='$q1' and tbltenderinfo.category='$q2'";
}
elseif(!$q1=="" || $q2=="" || !$q3==""){
$qry = "and tblTenderinfo.class='$q1' and tblclient.category='$q3'";
}
elseif($q1=="" || !$q2=="" || $q3=="") {
$qry = "and tbltenderinfo.category='$q2'";
}
elseif($q1=="" || $q2=="" || !$q3=="") {
$qry = "and tblclient.category='$q3'";
}

/*$qry = "and tblTenderinfo.class='$q1' and tbltenderinfo.category='$q2' and tblclient.category='$q3'";*/

?>
<h3>Tender Information for Class <?php echo $q1; ?> Contractors</h3>
<?php
$q = "SELECT tblTenderinfo.tenderId, tblTenderinfo.tenderDesc, tblTenderinfo.client,
tblTenderinfo.submdate,tblclient.clientagency from tblTenderinfo inner join tblclient
on tblTenderinfo.client=tblclient.clientid where tblTenderinfo.submdate>='".$today."' $qry ORDER BY tblTenderinfo.submdate ASC";[/PHP]
Sep 19 '08 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
Hi.

You could use implode to make this easier.

Like:
Expand|Select|Wrap|Line Numbers
  1. // Set up and an array for the implode function
  2. $constraints = array();
  3.  
  4. // Check each input
  5. if(isset($_GET['var1'])) {
  6.   $safe = mysql_real_escape_string($_GET['var1']);
  7.   $constraints[] = "`col1` = '$safe'";
  8. }
  9. if(isset($_GET['var2'])) {
  10.   $safe = mysql_real_escape_string($_GET['var2']);
  11.   $constraints[] = "`col2` = '$safe'";
  12. }
  13. // etc...
  14.  
  15. // Create the WHERE clause using the implode function
  16. $whereString = implode(" AND ", $constraints);
  17.  
  18. // Create the SQL query
  19. $query = "SELECT stuff FROM myTbl WHERE $whereString";
  20.  
  21. // etc..
  22.  
  23.  
  24.  
Sep 19 '08 #2

100+
P: 114
thankyou somuch, it worked.
Sep 19 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.