brem13 Posted May 19, 2010 Share Posted May 19, 2010 im trying to get a mysql select statement to work properly, im trying to have it search between 2 weights, like weight >=80 AND weight <= 200. i'm having trouble with the if statements to get it to work properly, any help? $vars = array('city'=>'city','hair'=>'hair','eye'=>'eyes','weight'=>'weight1','weight'=>'weight2','bodyType'=>'bodyType','ethnicity'=>'ethnicity','lookingFor'=>'lookingFor','pic'=>'pic'); // Setup an array to hold whatever is going in the db $sqlArray = array(); // Loop through each variable (using the double $), if not empty... it will be added to the query foreach($vars as $mysql=>$input) { if(!empty($$input) && $$input!='Any'){ $sqlArray[] = $mysql . ' = "' . $$input . '"'; if($$input=="weight1") $sqlArray[] = $mysql . ' >= "' . $$input . '"'; elseif($$input=="weight2") $sqlArray[] = $mysql . ' <= "' . $$input . '"'; } } Link to comment https://forums.phpfreaks.com/topic/202267-issue-populating-select-statement-properly/ Share on other sites More sharing options...
brem13 Posted May 19, 2010 Author Share Posted May 19, 2010 ok, i changed the code a bit and it sortof works, but the sql statement is wrong still, its showing up as "select * from members where weight <= "135"" where i'm trying to get it to say "select * from members where weight >= "80" AND weight <= "135"" i changed the code to this foreach($vars as $mysql=>$input) { if($input=="weight1") $sqlArray[] = $mysql . ' >= "' . $$input . '"'; elseif($input=="weight2") $sqlArray[] = $mysql . ' <= "' . $$input . '"'; //elseif(!empty($$input) && $$input!='Any') // $sqlArray[] = $mysql . ' = "' . $$input . '"'; } Link to comment https://forums.phpfreaks.com/topic/202267-issue-populating-select-statement-properly/#findComment-1060618 Share on other sites More sharing options...
brem13 Posted May 19, 2010 Author Share Posted May 19, 2010 i changed the code yet again, but to no avail here is the array $vars = array('city'=>'city','hair'=>'hair','eye'=>'eyes','weight'=>'weight1','weight'=>'weight2','bodyType'=>'bodyType','ethnicity'=>'ethnicity','lookingFor'=>'lookingFor','pic'=>'pic'); and here is the if statements foreach($vars as $mysql=>$input) { //if($input=="weight1") // $sqlArray[] = $mysql . ' >= "' . $$input . '"'; //elseif($input=="weight2") // $sqlArray[] = $mysql . ' <= "' . $$input . '"'; if($input != "weight1" && $input != "weight2") $the = " = "; elseif($input=="weight1") $the = " >= "; elseif($input=="weight2") $the = " <= "; if(!empty($$input) && $$input!='Any') $sqlArray[] = $mysql . $the . '"' . $$input . '"'; } im trying to get the select statement to read for example "select * from members where eye = "Blue" and weight >= "80" and weight <= "130"" but its only showing this statement "select * from members where eye = "Blue" and weight <= "130"" Link to comment https://forums.phpfreaks.com/topic/202267-issue-populating-select-statement-properly/#findComment-1060643 Share on other sites More sharing options...
brem13 Posted May 19, 2010 Author Share Posted May 19, 2010 ok so i noticed that its not working because of the double 'weight' field in the array, its only accepting the second one and not the first because 'weight' is used twice, so its only taking the second one, how do i fix that? Link to comment https://forums.phpfreaks.com/topic/202267-issue-populating-select-statement-properly/#findComment-1060678 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.