简体   繁体   English

在布尔模式下全文搜索的foreach循环

[英]foreach loop for fulltext search in boolean mode

I have php code which fetches names of toys from words table and matches these words in toys_text field of toys table using Boolean mode fulltext search and displays names of toys for users and counts of toys and unique users on webpage. 我有php代码,该代码可从words表中获取names of toys并使用布尔模式全文本搜索在toys表的toys_text字段中匹配这些单词, 在网页上为users显示names of toys以及玩具和唯一用户的数量

The names of toys can be either one word or combination of words. 玩具的名称可以是一个单词或单词的组合。 We want to fetch those results from toys which has toys_text field having all the words of the word combinations from words table 我们想从toys获取那些结果,这些toys toys_text字段具有words表中所有单词组合的words

(example - If words table has entry called blue car - BOTH blue and car should be present in the toys table toys_text field for a match). (示例-如果words表具有名为blue car条目- bluecar都应出现在toystoys_text字段中以进行匹配)。 The database changes over time with new toys with their users being added and outdated toys with their users removed. 随着时间的推移,数据库会随着添加了用户的新玩具和移除用户的过时玩具而变化。

I am facing issue in properly converting the array to string to input into the Boolean fulltext search. 我在将数组正确转换为字符串以输入到布尔全文搜索中时遇到问题。 I using foreach loop and want to output +(blue) +(car) but it is only inseting +(car) into the Boolean full text query. 我使用foreach循环,想输出+(blue)+(car),但它只是将+(car)插入布尔全文查询中。

echo $query1 outputs 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)`

while I would like the query to output as 而我想查询输出为

`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)`

This is my code - 这是我的代码-

<?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++;} ?> 

Your problem is that you don't concatenate the string : 您的问题是您没有连接字符串:

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

Should be : 应该 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM