如何获取查询以忽略我的搜索引擎中的空搜索字段 [英] How to get query to ignore empty search fields in my search engine

查看:112
本文介绍了如何获取查询以忽略我的搜索引擎中的空搜索字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用mysqli进行设置时,我正在使用if语句在搜索引擎中附加$ query。= AND列LIKE $ suchandsuch,但是我正在将其更新为PDO并正在努力找出如何以确保它将忽略留为空白的字段。

I was using and if statement to append $query .= "AND column LIKE $suchandsuch" in my search engine when I had it set up with mysqli, but I'm updating it to PDO and am struggling to figure out how to make sure it will ignore the fields that are left blank.

表单字段为:条件,程度,专业,城市,州。条件是必需的,但没有其他条件,因此在这些字段为空时,需要忽略这些字段。我觉得这应该很明显,但是我对PDO还是陌生的。

The form fields are: condition, degree, specialty, city, state. Condition is required, but nothing else is, so it needs to ignore those fields when they're empty. I feel like this should be obvious but I'm new to PDO and stumped.

if(isset($_POST['submit'])){
  $condition = $_GET["condition"];

  if(isset($_GET["degree"]) && !empty($_GET["degree"])){
    $degree = $_GET["degree"];


 }
  if(isset($_GET["specialty"]) && !empty($_GET["specialty"])){
    $specialty = $_GET["specialty"];
  }
  if(isset($_GET["city"]) && !empty($_GET["city"])){
    $city = $_GET["city"];
  }
  if(isset($_GET["state"]) && !empty($_GET["state"])){
    $state = $_GET["state"];
  }

$query = $connection->prepare("SELECT
  `providers`.`provider_first_name`, `providers`.`provider_last_name`,
  `providers`.`p_date_added`, `providers`.`id`,
  (SELECT GROUP_CONCAT(DISTINCT `degree_type` SEPARATOR ', ')
          FROM `degrees`
          WHERE `providers`.`id` = `prov_id`
    ) AS all_degrees,
  (SELECT GROUP_CONCAT(DISTINCT `specialty` SEPARATOR ', ')
          FROM `specialties`
          WHERE `providers`.`id` = `prov_id`
    ) AS all_specialties,
  (SELECT   GROUP_CONCAT(DISTINCT CONCAT(`locations`.`city`, ', ',
    `locations`.`state`)
    SEPARATOR '<br>')
    FROM `locations`
    WHERE `providers`.`id` = `prov_id`
  ) AS all_locations,
  (SELECT GROUP_CONCAT(DISTINCT `practice_name` SEPARATOR ', ')
          FROM `practices`
          WHERE `providers`.`id` = `prov_id`
    ) AS all_practices,
  (SELECT GROUP_CONCAT(DISTINCT `link` SEPARATOR '<br>')
          FROM `links`
          WHERE `providers`.`id` = `prov_id`
    ) AS all_links,
  (SELECT GROUP_CONCAT(DISTINCT `condition_name` SEPARATOR ', ')
          FROM `conditions`
          WHERE `providers`.`id` = `prov_id`
    ) AS all_conditions,
  (SELECT AVG(`reviews`.`star_rating`)
          FROM `reviews`
          WHERE `providers`.`id` = `prov_id`
    ) AS rating
  FROM `providers`
  WHERE `all_conditions` LIKE :condition");


  if($degree && !empty($degree)){
      $query.= " AND (`all_degrees` LIKE :degree)";
      $degree = "%".$degree."%";
      $query->bindParam(':degree', $degree, PDO::PARAM_STR);
  }
  if($specialty && !empty($specialty)){
      $query.= " AND (`all_specialties` LIKE :specialty)";
      $specialty = "%".$specialty."%";
      $query->bindParam(':specialty', $specialty, PDO::PARAM_STR);
  }
  if($city && !empty($city)){
      $query .= " AND (`all_locations` LIKE :city)";
      $city = "%".$city."%";
      $query->bindParam(':city', $city, PDO::PARAM_STR);
  }
  if($state && !empty($state)){
      $query .= " AND (`all_locations` LIKE :state)";
      $state = "%".$state."%";
      $query->bindParam(':state', $state, PDO::PARAM_STR);
  }

$condition = "%".$condition."%";
$query->bindParam(':condition', $condition, PDO::PARAM_STR);

$query->execute();


推荐答案

请添加非空条件。

if(isset($_GET["degree"]) && !empty($_GET["degree"])){
  $degree = $_GET["degree"];
}

这篇关于如何获取查询以忽略我的搜索引擎中的空搜索字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆