Jump to content

issue populating select statement properly


brem13

Recommended Posts

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 . '"';
	}
}

 

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 . '"';

}

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""

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.