prepare($sql_departments); $st_departments->execute(); if($_POST['submit']) { $name = $_POST['name']; $dept = $_POST['dept']; $nameArr = explode(" ",$name); $nameKeyArr = array("first_name","last_name"); foreach($nameArr as $k=>$v){ if($v==''){ unset($nameArr[$k]); } } $i=0; foreach($nameArr as $k=>$v){ $nameSearch[$nameKeyArr[$i]] = $v; $i++; } $first_name = $nameSearch['first_name']; $last_name = $nameSearch['last_name']; $employee = "Y"; //get first_name LIKE '%$first_name%' AND, if there is a value, last_name LIKE '%$last_name%' $sql_count = "SELECT Count(*) FROM directory WHERE employee=:employee "; if($first_name!=''){ $sql_count .= "AND first_name LIKE :first_name "; } if($last_name!=''){ $sql_count .= "AND last_name LIKE :last_name "; } if($dept!=''){ $sql_count .= "AND dept=:dept"; }//echo '$sql_count '.$sql_count; //count the first results $st_count = $dbmd->prepare($sql_count); if($first_name!=''){ $first_name = "%".$first_name."%"; $st_count->bindParam(':first_name',$first_name,PDO::PARAM_STR); } if($last_name!=''){ $last_name = "%".$last_name."%"; $st_count->bindParam(':last_name',$last_name,PDO::PARAM_STR); } $st_count->bindParam(':employee',$employee,PDO::PARAM_STR); if($dept!=''){ //$dept = "%".$dept."%"; $st_count->bindParam(':dept',$dept,PDO::PARAM_STR); } $st_count->execute(); $count_results = $st_count->fetchColumn(); //if no results for first_name LIKE '%$first_name%' if($count_results==0){ $sql_count2 = "SELECT Count(*) FROM directory WHERE employee=:employee "; $sql_count2 .= "AND last_name LIKE :first_name "; if($dept!=''){ $sql_count2 .= "AND dept=:dept "; } $st_count2 = $dbmd->prepare($sql_count2); if($first_name!=''){ $first_name = "%".$first_name."%"; $st_count2->bindParam(':first_name',$first_name,PDO::PARAM_STR); } if($last_name!=''){ $last_name = "%".$last_name."%"; $st_count2->bindParam(':last_name',$last_name,PDO::PARAM_STR); } $st_count2->bindParam(':employee',$employee,PDO::PARAM_STR); if($dept!=''){ //$dept = "%".$dept."%"; $st_count2->bindParam(':dept',$dept,PDO::PARAM_STR); } $st_count2->execute(); $count_results2 = $st_count2->fetchColumn(); //try the last_name LIKE '%$first_name%' $sql_dir = "SELECT * FROM directory WHERE employee=:employee "; $sql_dir .= "AND last_name LIKE :first_name "; if($dept!=''){ $sql_dir .= "AND dept=:dept "; } }else{ //Or get first_name LIKE '%$first_name%' AND, if there is a value, last_name LIKE '%$last_name%' $sql_dir = "SELECT * FROM directory WHERE employee=:employee "; if($first_name!=''){ $sql_dir .= "AND first_name LIKE :first_name "; } if($last_name!=''){ $sql_dir .= "AND last_name LIKE :last_name "; } if($dept!=''){ $sql_dir .= "AND dept=:dept "; } } //echo $sql_dir; $st_dir = $dbmd->prepare($sql_dir); if($first_name!=''){ $first_name = "%".$first_name."%"; $st_dir->bindParam(':first_name',$first_name,PDO::PARAM_STR); } if($last_name!=''){ $last_name = "%".$last_name."%"; $st_dir->bindParam(':last_name',$last_name,PDO::PARAM_STR); } $st_dir->bindParam(':employee',$employee,PDO::PARAM_STR); if($dept!=''){ $st_dir->bindParam(':dept',$dept,PDO::PARAM_STR); } $st_dir->execute(); } ?> Directory: Norwich University