我有php代码,可从布尔模式全文本搜索中从names of toys
表中提取words
并匹配toys_text
表的toys
字段中的这些单词,并在names of toys
上显示users
以及玩具和唯一用户的数量网页。
玩具的名称可以是一个单词或单词的组合。我们想从toys
中获取那些结果,该结果的toys_text
字段具有words
表中单词组合的所有单词
(示例-如果words
表具有名为blue car
的条目-blue
表car
字段中应同时存在toys
和toys_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."*".")";
}