Hi All,
I want to add a conditional statement to my search script, which adds a certain field to my sql query depending on the variable from a drop-down form via an Ajax request. I think the best way to do this is with an associative array matching the drop down, and a conditional within a foreach loop, which will select the appropriate field if it matches. I'm not 100% how to implement this, and at the moment my script breaks at the associative array with a T_unexpected variable. Any suggestions on how to approach this?
require_once("dbconn.php");
function getResultSearch($q, $chronology=''){
$retArr = array();
//SQL statement change this for different searches
$sql= "SELECT DISTINCT c.NEWSITE_, c.site_, c.location1,"
//get period
$period = array("MAXOFCL"=>c.interpcl, "MAXOFEH"=>"c.interpeh,", "MAXOFH"=>"c.interph,", "MAXOFER"=>"c.interper,", "MAXOFMR"=>"c.interpmr,", "MAXOFLA1"=>"c.interpla1,", "MAXOFLA2"=>"c.interpla2,", "MAXOFEM"=>"c.interpem,", "MAXOFMM"=>"c.interpmm,", "MAXOFLM"=>"c.interplm,");
// if chronology matches then add appropriate interp-field
foreach($period as $phase){
if($chronology = $period)
{
$interpretation = $$period;
}
}
//add interp-field to sql statement
if($interpretation){
$interpretation = '('.$interpretation.'),';
}
$sql .= $interpretation;
$fields="c.descriptio, c.presence_a, c.material, c.interpcl, c.interpeh, c.interph, c.interper, c.interpmr, c.INTERPLA1, c.INTERPLA2, c.interpem, c.interpmm, c.interplm, c.INTERPMOD1, c.INTERPMOD2, c.M2, chronology.CLASS
FROM compsites02 c"
.' left join `ceramic dbase july2k1` chronology ON c.NEWSITE_=chronology.NEW_SITE__'
;
if($fields){
$sql .= $fields;
}
if($q){
}
$where = "c.interpcl = '".$q."'
OR c.interpeh = '".$q."'
OR c.interph = '".$q."'
OR c.interper = '".$q."'
OR c.interpmr = '".$q."'
OR c.INTERPLA1 = '".$q."'
OR c.INTERPLA2 = '".$q."'
OR c.interpem = '".$q."'
OR c.interpmm = '".$q."'
OR c.interplm = '".$q."'
OR c.INTERPMOD1 = '".$q."'
OR c.INTERPMOD2 ='".$q."'
OR c.site_ = '".$q."'
OR c.material = '".$q."'
OR chronology.CLASS = '".$q."'"
;
}
if($chronology && $chronology != 'ALL'){
if($where){
$where = '('.$where.') and';
}
$where .= ' c.`'.$chronology.'` is not NULL';
}
if($where){
$sql .= ' where '.$where;
}
$sql .= ' group by c.NEWSITE_, c.site_, c.location1, c.descriptio, c.presence_a, c.material, c.M2, chronology.CLASS';
if( $result = clMyConnect::$mysqli->query($sql)){
while ($row = mysqli_fetch_array($result)) {
if(!is_array($retArr[$row['NEWSITE_']])){
$retArr[$row['NEWSITE_']] = $row;
}
else{
if(trim($retArr[$row['NEWSITE_']]['CLASS'])){
$retArr[$row['NEWSITE_']]['CLASS'] .= ', ';
}
$retArr[$row['NEWSITE_']]['CLASS'] .= $row['CLASS'];
}
}
}
return $retArr;
}
function getResultSearchForXml($q, $chronology=''){
$retArr = array();
//SQL statement change this for different searches
$sql= "SELECT c.site_, c.location1, c.descriptio, c.presence_a, c.material, points.Lat, points.Lon,
c.newsite_, c.utm_e, c.utm_n
FROM compsites02 c
INNER join points ON points.NEWSITE_=c.NEWSITE_"
.' left join `ceramic dbase july2k1` chronology ON c.NEWSITE_=chronology.NEW_SITE__'
;
if($q){
$where = " c.interpcl = '".$q."'
OR c.interpeh = '".$q."'
OR c.interph = '".$q."'
OR c.interper = '".$q."'
OR c.interpmr = '".$q."'
OR c.INTERPLA1 = '".$q."'
OR c.INTERPLA2 = '".$q."'
OR c.interpem = '".$q."'
OR c.interpmm = '".$q."'
OR c.interplm = '".$q."'
OR c.INTERPMOD1 = '".$q."'
OR c.INTERPMOD2 ='".$q."'
OR c.site_ = '".$q."'
OR c.material = '".$q."'
OR chronology.CLASS = '".$q."'"
;
}
if($chronology && $chronology!= 'ALL'){
if($where){
$where = '('.$where .') and';
}
$where .= ' c.`'.$chronology.'` is not NULL';
}
if($where){
$sql .= ' where '.$where;
}
$sql .= ' group by c.NEWSITE_';
//echo $sql;
if( $result = clMyConnect::$mysqli->query($sql)){
while ($row = mysqli_fetch_array($result)) {
$retArr[] = $row;
}
}
return $retArr;
}