我有php代码,可从布尔模式全文本搜索中从names of toys表中提取words并匹配toys_text表的toys字段中的这些单词,并在names of toys上显示users以及玩具和唯一用户的数量网页。

玩具的名称可以是一个单词或单词的组合。我们想从toys中获取那些结果,该结果的toys_text字段具有words表中单词组合的所有单词

(示例-如果words表具有名为blue car的条目-bluecar字段中应同时存在toystoys_text进行匹配)。随着时间的推移,数据库会随着添加用户的新玩具和移除用户的过时玩具而变化。

我在将数组正确转换为字符串以输入到布尔全文搜索中时遇到问题。我使用foreach循环并想输出+(blue)+(car),但它只是将+(car)插入到布尔全文查询中。

echo $ query1输出

`select COUNT(*) as 'count', COUNT(DISTINCT tw.screen_name) AS 'cnt' from
tweets tw join users us on tw.user_id=us.user_id WHERE MATCH (tweet_text)
AGAINST (' +(car)' IN BOOLEAN MODE)`


而我希望查询输出为

`select COUNT(*) as 'count', COUNT(DISTINCT tw.screen_name) AS 'cnt' from
tweets tw join users us on tw.user_id=us.user_id WHERE MATCH (tweet_text)
AGAINST ('+(blue) +(car)' IN BOOLEAN MODE)`


这是我的代码-

<?php
$query = "SELECT * FROM words ORDER BY `words`.`words`
ASC";
$result = mysqli_query($con, $query);

$i = 1;
while($row = mysqli_fetch_array($result)){
    $Word = $row['words'];
    $WordsArr = explode(" ", $row['words']);
    $query = "";

       if(count($WordsArr) > 1){
          foreach ($WordsArr as $value) {
                    $value1 = " +"."(".$value."*".")";
                    }
           $query1 = "select COUNT(*) as 'count', COUNT(DISTINCT
           t.name) AS 'cnt' from toys t WHERE MATCH (toys_text) AGAINST
          ('$value1' IN BOOLEAN MODE)";
        }else{
        $query1 = "select COUNT(*) as 'count', COUNT(DISTINCT t.name) AS
       'cnt' from toys t WHERE
        MATCH (toys_text) AGAINST ('$WordsArr[0]* IN BOOLEAN MODE')";
        }
        $Data1 = mysqli_query($con, $query1);
        $total1 = mysqli_fetch_assoc($Data1);
            ?>
        <tr>
          <td><?php echo $i;?></td>
          <td><?php echo $row['words'];?></td>
          <td><a href="showtoys.php?word=<?php echo
          urlencode($Word); ?>" target="_blank"><?php echo
          $total1['count']; ?></a></td>
          <td><a href="showtoys.php?keyword=<?php echo urlencode($Word);
          ?>" target="_blank"><?php echo $total1['cnt']; ?></a></td>
        </tr>
            <?php $i++;} ?>

最佳答案

您的问题是您没有连接字符串:

foreach ($WordsArr as $value) {
    $value1 = " +"."(".$value."*".")";
}


应该 :

$value1 = '';
foreach ($WordsArr as $value) {
    $value1 .= " +"."(".$value."*".")";
}

10-06 06:22