Mysql PHP中的全文搜索返回多个值

时间:2017-02-11 08:15:58

标签: php mysql search full-text-search

您好stackoverflow社区, 我在PHP中使用Mysql全文搜索时遇到问题。下面的代码正在运行,但它会为每个匹配的关键字返回多篇文章。我想为每个关键字返回一篇不同的文章,并按相关性对其进行排序。

以下代码的屏幕截图

enter image description here

      function relatedArticle($item, $category){
          global $con;

        $keywords=removePrepositions($item);

        $keyword_tokens = explode(' ', $keywords);

           $x=0;
           $query_result='';
        foreach($keyword_tokens as $item) {

            $x++; 
            $construct = " "; 
            $construct=$item;

         $query = "SELECT DISTINCT * FROM apps WHERE Match(title) AGAINST ('$construct' IN NATURAL LANGUAGE MODE) and Type = '$category' LIMIT 4";

       $query_result=$con->query($query) or trigger_error($con->error."[$query]");

        echo"<div id='Big_container_rel'>"; 
          echo"<div id='container_rel'>";   
        while($row=$query_result->fetch_array()){

             echo"<div id='container1_rel'>";

             $AdTitle=pre_replace($row['title']);
             $type= strtolower(pre_replace($row['Type']));

             echo"<a href='/".$type."/".$row['appId']."/".$AdTitle."/'><img id='imgMobi' alt=".$row['title']." src='uploads/".$row['image']."'/></a>";
             echo"<br/><h4><a href='/".$type."/".$row['appId']."/".$AdTitle."/'>".$row['title']."</a></h4>";

            echo"</div>";


            }
            echo"</div>";
         echo"</div>";

        }               

      }

请帮我解决问题。

1 个答案:

答案 0 :(得分:0)

我已经成功解决了这个问题

function relatedArticle($item, $category){
              global $con;

            $keywords=removePrepositions($item);

            $keyword_tokens = explode(' ', $keywords);

               $query_result=implode(',', $keyword_tokens);
               $construct='';

             $construct=$query_result;
             $query = "SELECT DISTINCT * FROM apps WHERE Match(title) AGAINST ('$construct' IN NATURAL LANGUAGE MODE) and Type = '$category' limit 4";

           $query_result=$con->query($query) or trigger_error($con->error."[$query]");

            echo"<div id='Big_container_rel'>"; 
              echo"<div id='container_rel'>";   
            while($row=$query_result->fetch_array()){

                 echo"<div id='container1_rel'>";

                 $AdTitle=pre_replace($row['title']);
                 $type= strtolower(pre_replace($row['Type']));

                 echo"<a href='/".$type."/".$row['appId']."/".$AdTitle."/'><img id='imgMobi' alt=".$row['title']." src='uploads/".$row['image']."'/></a>";
                 echo"<br/><h4><a href='/".$type."/".$row['appId']."/".$AdTitle."/'>".$row['title']."</a></h4>";

                echo"</div>";


                }
                echo"</div>";
             echo"</div>";



          }

什么是骄傲的屏幕截图

enter image description here