将字符串拆分为数组并在查询字符串中使用数组的每个项目

时间:2016-04-25 20:56:36

标签: php arrays

我搜索了将字符串拆分成数组的所有主题。但我实际上没有遇到过我的需求。

在搜索框上,用户键入任意长度的任何字符串(我无法分辨单词的长度)。在提交时,php脚本将值收集为$ q。

示例:

  1. 我们说$q="users typed in search query of any length."
  2. php将$q中的每个单词拆分为$stringArray ("users","type","in","search","etc")
  3. php迭代$stringArray的每个项目,如:for(each $stringArray as $i);
  4. 使用数组的每个$i运行查询以查找匹配项并显示与$q中的任何一项匹配的所有行。
  5. PHP代码

    <?php
    require_once('../data/conString_mysqli.php');
    
    if(isset($_POST['services'])){$q = trim($_POST['services']);}else if(isset($_REQUEST['q'])){$q = trim($_REQUEST['q']);}
    else if(isset($_REQUEST['s'])){$s = trim($_REQUEST['q']);}else{header('Location:../home/');}
    
    ?>
    
    <?php
    
    $stringArray = explode(" ", $q);//split using spaces
    
    foreach (array($stringArray) as &$q) {  
    ?>
    
    <?php
    //run a search query here
    $search_query = mysqli_query($connect,"SELECT * FROM service_providers WHERE INSTR(pname, '".$q."') OR INSTR(bizaddr, '".$q."') OR INSTR(bizname, '".$q."') OR INSTR(bizdesc, '".$q."') OR INSTR(pmobile, '".$q."') OR INSTR(pemail, '".$q."') OR INSTR(bizcat, '".$q."') OR INSTR(pmobile, '".$q."')ORDER BY bizname");
    
        $records = mysqli_num_rows($search_query);
        if(!$records >= 1){
            die(mysql_error());//('<script type="text/javascript">alert("PROVIDERS SEARCH RESULT \n\nCould not find a match. \nTry using one word, exact words. \nYou can search by category. Just type the category name (tv repairs, air conditioning, painting, car repairs, carpentry, plumbing, tiles, rug, washing, catering, cakes, creams, computer, software, hardware etc. \n\nDo not use periods, commas, and other symbols!");window.location="test_array.php?q='.$q.'";<script>');
        }else {
            while($row = mysqli_fetch_array($search_query)){
                $pcode = $row['pcode'];
                $bizname = $row['bizname'];
                $bizaddr = $row['bizaddr'];
                $bizdesc = $row['bizdesc'];
                $pname = $row['pname'];
                $pmobile = $row['pmobile'];
                $pemail = $row['pemail'];
                if($row['bizcat'] ==''){
                    $category = "UNKNOWN CATEGORY";}
                else{$category = $row['bizcat'];}
    ?>
    <?php
                echo $pcode .'<br />';
                echo $bizname .'<br />';
                echo $bizaddr .'<br />';
                echo $bizdesc .'<br />';
                echo $category .'<br />';
                echo $pname .'<br />';
                echo $pmobile .'<br />';
                echo $pemail .'<br />';
                echo '--------------------' .'<br />';
                }
            }
    ?>
    
    <?php
    }
    ?>
    

    当我运行此代码时,它会返回以下错误:

    Notice: Array to string conversion in C:\Wamp\www\servicehub.ng\find\test_array.php on line 17
    

1 个答案:

答案 0 :(得分:0)

第17行

 foreach (array($stringArray) as &$q)

删除数组,您将获得没有该错误的代码

foreach ($stringArray as $q)

此外,您可以将mysqli_fetch_array更改为mysqli featch assoc,因为它只会获取关联数组。

你可以摆脱$ search_query中的所有连接。 Double quoute做到这一切。