Hi stackoverflow community, I'm having a problem with Mysql full text search in PHP. The code below is working, however, it is returning multiple articles for each individual matching keyword. I want to return a distinct article for each keyword and order them by relevance.
Screen shot for the code below
code
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>";
}
}
Please help me solve the problem.
I have managed solving the problem
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>";
}
What is prouding screen shot
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.