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