dousi6405 2017-02-16 06:47
浏览 17

如果搜索的关键字超过2个,则PHP搜索无效

I am trying to build a search box which will search in 2 columns.

html

<div class="autocomplete-list1">
    <input type="text" class="searchpropertyinputs areaname-list-completed" name="buildingorlocation" id="buildingorlocation" placeholder="Building or Location" onkeyup="autofillbuildingorlocation()" maxlength="50" />
    <ul class="areaname-list searchpropertyinputs" id="property_buildingorlocation_list"></ul>
</div>

php

$property_buildingorlocation = ($_GET['buildingorlocation']);
$property_buildingorlocation = str_replace(',', ' ', $property_buildingorlocation);


$select = $con->prepare(

"SELECT 
    * 
from 
    tbl_property 
WHERE 
    property_buildingname LIKE '%$property_buildingorlocation%' 
    OR property_areaname LIKE '%$property_buildingorlocation%'");

$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();

If i enter up to two key words like madison garden, then the result comes but if i put three like madison garden road then there is no result... can anyone enlighten me please..

  • 写回答

1条回答 默认 最新

  • dsy48837 2017-02-16 06:59
    关注

    Try the below code:

    <?php
    $property_buildingorlocations = array_filter(explode(',', $_GET['buildingorlocation']));
    $query = "SELECT * from tbl_property ";
    $count = count($property_buildingorlocations);
    if($count){
       $query .=" WHERE ";
    }
    $i = 0;
    foreach ($property_buildingorlocations as $property_buildingorlocation){
       $query .= "property_buildingname LIKE '%".trim($property_buildingorlocation)."%'";
       if(++$i !== $count) {
          $query .=" OR ";
       }
    
    }
    
    
    
    $select = $con->prepare($query);
    
    $select->setFetchMode(PDO::FETCH_ASSOC);
    $select->execute();
    
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本