Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi Dimensional Array Problem

Status
Not open for further replies.

meeble

Programmer
Joined
Sep 24, 2002
Messages
137
Location
GB
Hello,

I have the code:

# This is a recursive function, which either adds a value into a global array
# for use in the select statement, or recurses if the value is an
# array itself and does the same thing.
function DisassembleArray($key, $val)
{
global $queries;
if((!is_array($val)) && ($val != ""))
{
$queries[] = "$key = '$val'";
} else
{
foreach($val as $foo => $real_val)
{
DisassembleArray($key, $real_val);
}
}
}

# These are the names of the form fields, and they also correspond
# to field names in the table that we want to look through.
$valid_fields = array("residence", "workextime", "level", "desiredloc", "salarydes", "salarycur");

$queries = array();
foreach($_POST as $key => $val)
{
if(!in_array($key, $valid_fields)) { continue; }
DisassembleArray($key, $val);
}

# Start of the query.
$query = "SELECT * from details where fname LIKE '%$firstname%' AND lname LIKE '%$lastname%' AND emailaddress LIKE '%$emailaddress%'";
if(count($queries) > 0)
# If something's been selected, we append it to the query.
{
$query .= " and " . implode(" or ", $queries);
}
$query .= " order by lname desc";




On the form page I entered the search details into the following dropdown boxes:

residence: Wrexham, Yorkshire
workextime: 1 year, 2 years
level: Director, Senior Manager

This produces the SQL output on the next page...

"SELECT * from details where fname LIKE '%%%' AND lname LIKE '%%%' AND emailaddress LIKE '%%%' and residence = 'Wrexham' or residence = 'Yorkshire' or workextime = '1 year' or workextime = '2 years' or level = 'Director' or level = 'Senior Manager' order by lname desc"

when what I ACTUALLY want is:

"SELECT * from details where fname LIKE '%%%' AND lname LIKE '%%%' AND emailaddress LIKE '%%%' AND (residence = 'Wrexham' or residence = 'Yorkshire') AND (workextime = '1 year' or workextime = '2 years') AND (level = 'Director' or level = 'Senior Manager') order by lname desc"

I can't seem to change the code above to get this.

Any ideas please?
 
couldn't you replace

$query .= " and " . implode(" or ", $queries);
with
$query .= " and (" . implode(" or ", $queries).")";

'... and then it wouldn't compile?'
t_avatar.jpg
 
This gives:

SELECT * from details where fname LIKE '%%%' AND lname LIKE '%%%' AND emailaddress LIKE '%%%' and (residence = 'Wrexham' or residence = 'Yorkshire' or workextime = '1 year' or workextime = '2 years' or level = 'Director' or level = 'Senior Manager') order by lname desc

which is not what I want. I want:

SELECT * from details where fname LIKE '%%%' AND lname LIKE '%%%' AND emailaddress LIKE '%%%' AND (residence = 'Wrexham' or residence = 'Yorkshire') AND (workextime = '1 year' or workextime = '2 years') AND (level = 'Director' or level = 'Senior Manager') order by lname desc

Any ideas,

Cheers

 
Sorry misread the question, i thought that the queries to got inside the bracket was held in a single array element
ie
queries[1] = "residence = 'Wrexham' or residence = 'Yorkshire'"

Will two entries to the array with the same key always be together?
ie
queries[1] = "residence = 'Wrexham'"
queries[2] = "residence = 'Yorkshire'"
queries[3] = "workextime = '1 year'"
queries[4] = "workextime = '2 years'"

and not
queries[1] = "residence = 'Wrexham'"
queries[2] = "workextime = '1 year'"
queries[3] = "residence = 'Yorkshire'"
queries[4] = "workextime = '2 years'"

t_avatar.jpg

'... and then it wouldn't compile?'
 
Yes, unless you know a better way of doing things.

Cheers

James
 
I've tried to recreate something to achieve this
Code:
<?
$queries = array();
$queries[0] = &quot;residence = 'Wrexham'&quot;;
$queries[] = &quot;residence = 'Yorkshire'&quot;;
$queries[] = &quot;workextime = '1 year'&quot;;
$queries[] = &quot;workextime = '2 years'&quot;;
$queries[] = &quot;level = '3'&quot;;

$currtype = &quot;empty&quot;;
$bracketopen = false;
$qcount = sizeof($queries);
for ($i=0;$i<$qcount;$i++){
	$newtype = substr($queries[$i],0,strpos($queries[$i],'=')-1);
	if ($newtype == $currtype){
		$query .= &quot; or &quot;;
	}else{
		if ($bracketopen){
			$query .= &quot;) and (&quot;;
			$bracketopen = true;
		}else{
			$query .= &quot; and (&quot;;
			$bracketopen = true;
		}
	}
	$query .= $queries[$i];
	$currtype = $newtype;
}
if ($bracketopen){
	$query .= &quot;)&quot;;
	$bracketopen = false;
}
echo $query;
?>

example outout here

Dunno if this helps, I only threw it together to resolve this, caution it only works if array elements with the same key are together.

Maybe someone else will have an easier solution.

t_avatar.jpg

'... and then it wouldn't compile?'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top