Jump to content

SELECT * FROM 1 and or 2 columns?


xcandiottix

Recommended Posts

I have 2 variables whose content is created from 2 drop down menus. The data is passed to the next page where i use $_GET and check if the data is valid. If it is I want to search my database for the first drop down's content, then if the second drop down was set, search for that content with in the first drop downs content to display a final result. I can not just use AND / OR in my Select statement because that will eliminate either when there is or isn't a drop down. Any suggestions?

if($_GET['cat'] !== "VOID"){
$cat = $_GET['cat'];
}
if($_GET['lev'] !== "VOID"){
$lev = $_GET['cat'];
}

$con = mysql_connect("");
mysql_select_db("");

$sql="SELECT * FROM application_directory WHERE db_Appcategory = '$cat' AND/OR db_Applevel = '$lev'"; <-- Problem is here
$result = mysql_query($sql);

 

Link to comment
https://forums.phpfreaks.com/topic/208970-select-from-1-and-or-2-columns/
Share on other sites

Something like this should work

 

$where = "";
if((isset($_GET['cat']) && $_GET['cat'] !== "VOID") && (isset($_GET['lev']) && $_GET['lev'] !== "VOID")) {
$where .= " db_Appcategory = '".mysql_real_escape_string($_GET['cat'])."' AND db_Applevel = '".mysql_real_escape_string($_GET['lev'])."'";
}
if((isset($_GET['lev']) && $_GET['lev'] !== "VOID") && $_GET['cat'] === "VOID") {
$where .= " db_Applevel = '".mysql_real_escape_string($_GET['lev'])."'";
}
if((isset($_GET['cat']) && $_GET['cat'] !== "VOID") && $_GET['lev'] === "VOID") {
$where .= " db_Appcategory = '".mysql_real_escape_string($_GET['cat'])."'";
}
$sql="SELECT * FROM application_directory WHERE $where";
$result = mysql_query($sql);

I actually have 4 drop downs but was just trying to understand concept so I could make 4 work. I wrote this ridiculous mess:

$counter = 0;

$con = mysql_connect("");
mysql_select_db("");

if($_GET['cat'] !== "VOID"){
$cat = $_GET['cat'];
$counter+=.1;
}
if($_GET['lev'] !== "VOID"){
$lev = $_GET['lev'];
$counter+=.01;
}
if($_GET['rat'] !== "VOID"){
$rat = $_GET['rat'];
$counter+=.001;
}
if($_GET['pop'] !== "VOID"){
$pop = $_GET['pop'];
$counter+=.0001;
}

echo $counter;

if($counter == .1000){$sql="SELECT * FROM application_directory WHERE db_Appcategory = '$cat'";}
if($counter == .0100){$sql="SELECT * FROM application_directory WHERE db_Applevel = '$lev'";}
if($counter == .0010){$sql="SELECT * FROM application_directory WHERE db_Apprating = '$rat'";}
if($counter == .0001){$sql="SELECT * FROM application_directory WHERE db_Apppopularity = '$pop'";}
if($counter == .1100){$sql="SELECT * FROM application_directory WHERE db_Appcategory = '$cat' AND db_Applevel = '$lev'";}
if($counter == .1010){$sql="SELECT * FROM application_directory WHERE db_Appcategory = '$cat' AND db_Apprating = '$rat'";}
if($counter == .1001){$sql="SELECT * FROM application_directory WHERE db_Appcategory = '$cat' AND db_Apppopularity = '$pop'";}
if($counter == .0110){$sql="SELECT * FROM application_directory WHERE db_Applevel = '$lev' AND db_Apprating = '$rat'";}
if($counter == .0011){$sql="SELECT * FROM application_directory WHERE db_Apprating = '$rat' AND db_Apppopularity = '$pop'";}
if($counter == .1110){$sql="SELECT * FROM application_directory WHERE db_Appcategory = '$cat' AND db_Applevel = '$lev' AND db_Apprating = '$rat'";}
if($counter == .0111){$sql="SELECT * FROM application_directory WHERE db_Applevel = '$lev' AND db_Apprating = '$rat' AND db_Apppopularity = '$pop'";}
if($counter == .1111){$sql="SELECT * FROM application_directory WHERE db_Appcategory = '$cat' AND db_Applevel = '$lev' AND db_Apprating = '$rat' AND db_Apppopularity = '$pop'";}

 

Which seems like a sloppy hack ... but your work seems a lot better! I'll just get it to work with 4.

 

Thanks!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.