doujiyan0971
2015-12-13 21:19
浏览 336
已采纳

PHP搜索多个字段

I want to search data using ajax method with multiple fields search option (e.g. name, college, department, year, nationality e.t.c ). I have insert name for searching and rest of fields are empty than it went to foreach loop but this if (isset($_GET[$field]) && !empty($_GET['$field'])) condition not successful and went to else loop

  $fields = array(
  'name' => TRUE,
  'gender' => TRUE,
  'colf' => TRUE,
  'deptf' => TRUE,
  'natf' => TRUE,
  'fstatusf' => TRUE,
  'fyearf' => TRUE
  );
 foreach ($fields as $field => $like) {
  if (isset($_GET[$field]) && !empty($_GET['$field'])) {
    $value = $_GET[$field];
    $search[] = $field . ( $like ? ('LIKE "%' . $value . '%"') : ('="' . $value . '"') );
       }
 } 
  if ($search) {
  $sql = 'SELECT * FROM fmaf WHERE ' . implode(' or ' . $search);
  }

else{
$sql="SELECT * FROM fmaf";

    }

图片转代码服务由CSDN问答提供 功能建议

我想使用带有多个字段搜索选项的ajax方法搜索数据(例如姓名,学院,部门,年份,国籍 等)。 我有用于搜索的插入名称,其余的字段是空的,而不是它转到foreach循环但是这个if(isset($ _ GET [$ field])&&!empty($ _ GET ['$ field']))条件没有 成功并转到其他循环

  $ fields = array(
'name'=> TRUE,
'sexity'=> TRUE,
'colf'  => TRUE,
'deptf'=> TRUE,
'natf'=> TRUE,
'fstatusf'=> TRUE,
'fyearf'=> TRUE 
); 
  foreach($ fields as $ field => $ like){
 if(isset($ _ GET [$ field])&&!empty($ _ GET ['$ field'])){
 $ value =  $ _GET [$ field]; 
 $ search [] = $ field。  ($ like?('LIKE'%'。$ value。'%''):('=''。$ value。'“')); 
} 
} 
 if($ search){\  n $ sql ='SELECT * FROM fmaf WHERE'。  implode('或'。$ search); 
} 
 
else {
 $ sql =“SELECT * FROM fmaf”; 
 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题