chelnov63 Posted March 24, 2009 Share Posted March 24, 2009 I have a search form which is as follows: <form action="reports_filter.php" method="post" enctype="multipart/form-data"> <select name="company" id="company"> <option value="Any" selected="selected">Any</option> <option value="hot">Primark</option> <option value="warm">Nike</option> <option value="cool">Puma</option> </select> <select name="temperature" id="temperature"> <option value="Any" selected="selected">Any</option> <option value="hot">Hot (1-3 month opportunity)</option> <option value="warm">Warm (3-6 month opportunity)</option> <option value="cool">Cool (6-12 month opportunity)</option> <option value="cold">Cold (no interest)</option> </select> </form> $company = $_POST["company"]; $temperature = $_POST["temperature"]; $result_contacts = mysql_query("SELECT * FROM contacts WHERE company = '$company' AND temperature= $temperature"); now when they select from the dropdown lists, all works fine.. but not sure what to do if they choose 'ANY' in the drop down lists, how would I do the search then.. any help is appreciated..thanks Link to comment https://forums.phpfreaks.com/topic/150854-sql-search-question/ Share on other sites More sharing options...
kickstart Posted March 24, 2009 Share Posted March 24, 2009 Hi Something like this I think:- $company = $_POST["company"]; $temperature = $_POST["temperature"]; $result_contacts = mysql_query("SELECT * FROM contacts WHERE ".(($company == 'Any') ? "" : "company = '$company'")." AND ".(($temperature == 'Any') ? "" : "temperature= '$temperature'"); All the best Keith Link to comment https://forums.phpfreaks.com/topic/150854-sql-search-question/#findComment-792461 Share on other sites More sharing options...
chelnov63 Posted March 24, 2009 Author Share Posted March 24, 2009 aah thanks mate but its giving me a Parse error: parse error in C:\htdocs\reports\reports_filter.php on line 15 [\b] for that line Link to comment https://forums.phpfreaks.com/topic/150854-sql-search-question/#findComment-792462 Share on other sites More sharing options...
chelnov63 Posted March 24, 2009 Author Share Posted March 24, 2009 the logic seems slighlty off example: echo $company; // any echo $temperature; //any $result_contacts = "SELECT * FROM contacts WHERE ".(($company == 'Any') ? "" : "company = '$company'")." AND ".(($temperature == 'Any') ? "" : "temperature= '$temperature'"); echo $result_contacts; echo $result_contacts; gives "SELECT * FROM contacts WHERE AND " Link to comment https://forums.phpfreaks.com/topic/150854-sql-search-question/#findComment-792465 Share on other sites More sharing options...
ninedoors Posted March 24, 2009 Share Posted March 24, 2009 Don't use "", use % which is the wildcard character. It should return a match to anything in the company and temperture column. Link to comment https://forums.phpfreaks.com/topic/150854-sql-search-question/#findComment-792486 Share on other sites More sharing options...
chelnov63 Posted March 24, 2009 Author Share Posted March 24, 2009 thanks mate..i'll give it a shot Link to comment https://forums.phpfreaks.com/topic/150854-sql-search-question/#findComment-792496 Share on other sites More sharing options...
kickstart Posted March 24, 2009 Share Posted March 24, 2009 Hi Sorry, my fault for doing it in a hurry. $company = $_POST["company"]; $temperature = $_POST["temperature"]; $whereClause = ""; $whereClause .= (($company == 'Any') ? "" : (($whereClause == "") ? " WHERE " : " AND ")."company = '$company'") $whereClause .= (($temperature == 'Any') ? "" : (($whereClause == "") ? " WHERE " : " AND ")."temperature = '$temperature'") $result_contacts = mysql_query("SELECT * FROM contacts $whereClause"); You could use a like clause and a %, but suspect it would be less efficient (which is probably not important with a select that is only occasionally executed). All the best Keith Link to comment https://forums.phpfreaks.com/topic/150854-sql-search-question/#findComment-792553 Share on other sites More sharing options...
chelnov63 Posted April 9, 2009 Author Share Posted April 9, 2009 thanks for all your help guys..sorry about the late reply... i ended up thinking of another way to do it, as the above was just a small percentage of the search criteria.. there are about 15 different criterias to choose from.. i ended up doing it like this.. so for e.g for temperature and company criteria i did: $tempParams = array(); $companyParams = array(); $i = 0; $j = 0; foreach ($temperature as $key) { $tempParams[$i] = "temperature LIKE '$key' "; $i++; } foreach ($company_value as $key_value) { $companyParams[$j] = "company_value LIKE '$key_value'"; $j++; } $temperatureSQL = join($tempParams,' OR '); $companySQL = join($companyParams,' OR '); $result = mysql_query("SELECT id, company, first_name, surname, email, job_title, address1, address2,address3,address4,postcode FROM contacts JOIN companies ON(companies.company = contacts.company) WHERE (".$temperatureSQL.") AND (".$companySQL. ") ORDER BY company"); Link to comment https://forums.phpfreaks.com/topic/150854-sql-search-question/#findComment-805314 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.