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

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";

    }
  • 写回答

3条回答 默认 最新

  • dongyuqi3808 2015-12-15 11:27
    关注

    At last i have found the solution and thanks to cFreed and other who help me. My main concern is that if user want to search with one field only or more than 1 field in that case below answer is helpful for me and may be also for someone:

    if (empty($_GET['name']) && empty($_GET['gender']) && empty($_GET['colf']) && empty($_GET['deptf']) && empty($_GET['natf']) && empty($_GET['fstatusf']) && empty($_GET['fyearf']))
    {
        $sql="select * from fmaf ";
    }
    else
    {
     $wheres = array();
    
    $sql = "select * from fmaf where ";
    
    if (isset($_GET['name']) and !empty($_GET['name']))
    {
        $wheres[] = "name like '%{$_GET['name']}%' ";
    } 
    
    if (isset($_GET['gender']) and !empty($_GET['gender']))
    {
        $wheres[] = "gender = '{$_GET['gender']}'";
    } 
    
    if (isset($_GET['colf']) and !empty($_GET['colf']))
    {
        $wheres[] = "college = '{$_GET['colf']}' ";
    } 
    
    if (isset($_GET['deptf']) and !empty($_GET['deptf']))
    {
        $wheres[] = "department = '{$_GET['deptf']}' ";
    } 
    
    if (isset($_GET['natf']) and !empty($_GET['natf']))
    {
        $wheres[] = "nationality = '{$_GET['natf']}' ";
    } 
    
    if (isset($_GET['fstatusf']) and !empty($_GET['fstatusf']))
    {
        $wheres[] = "finalstatus = '{$_GET['fstatusf']}' ";
    }
    
    if (isset($_GET['fyearf']) and !empty($_GET['fyearf']))
    {
        $wheres[] = "fyear = '{$_GET['fyearf']}' ";
    } 
    
    foreach ( $wheres as $where ) 
    {
    $sql .= $where . ' AND ';   //  you may want to make this an OR
      }
     $sql=rtrim($sql, "AND "); 
    
         }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看