根据数据库中的两个字段进行搜索,如果两个字段都为真,则获得结果,否则如果任何一个字段为假,则不显示结果,使用 symfony php

Search on the basis of two fields from database if both are true then get result otherwise not display result if any one is false, using symfony php

提问人:Timothy 提问时间:12/21/2022 更新时间:12/21/2022 访问量:23

问:

我正在使用 doctrine db 来影响数据库中的数据,我有数据库,其中包含一些信息,例如公司拥有的城市、州和车辆类型,我有两个过滤器用于基于州和车辆类型的搜索,它们都工作正常,我想合并这两个过滤器,只有那些公司应该显示谁处于搜索状态并拥有搜索到的车辆类型。

基于城市和州的搜索

         $fromCity_arr = explode(', ', $fromCity);
         $whereStr = '';
         foreach($fromCity_arr as $fromCity_el){
         if(!empty($whereStr)){$whereStr .= " OR ";}
         $whereStr .= "state  LIKE '%".$fromCity_el."%'";
       }

根据车辆类型搜索

       $vehicleTypeName = $this->vehicleCategory->getName();
       $vehicleCat = explode(',', $vehicleTypeName);
       $vehicleType = '';
       foreach($vehicleCat as $vehicle_cat){
         if(!empty($vehicleType)){$vehicleType .= " OR ";}
         $vehicleType .= "vehicle_types Like '%".$vehicle_cat."%'";
       }
 

在单行数据中搜索这两者的查询

    $sql = "SELECT * FROM vendor_info WHERE  $vehicleType AND $whereStr";
    $em = $this->getDoctrine()->getManager();
    $stmt = $em->getConnection()->prepare($sql);
    $stmt->execute();
    $company = $stmt->fetchAll();

使用 dump 进行此筛选器的结果

enter image description here

当前结果现在通过使用此过滤器,所有处于搜索状态的公司都来了,所有搜索过车辆类型的公司都来了,

预期结果 只有那些具有特定搜索车辆类型并处于搜索状态的公司才应被宣传,

php mysql symfony 搜索 学说

评论

1赞 Umar Sharjeel 12/21/2022
您能通过为这两个示例提供示例数据集来解释吗?你能在两者周围添加括号并检查结果集是否发生变化吗?$vehicleType$whereStr

答: 暂无答案