Similar String Listing

This tutorial can hlep you to display the list of similar articles of the viewing one on your website. This is the important thing that all developers should know in order to standardize their websites which conain bunch of contents or articles. Actually, there are many ways to achieve this job; however, this tutoria is one among them.

INSTRUCTION

Now suppose that we're making a news website and want to display the similar news' titles when a visitor is viewing a news.

In the news viewing page, we should have the viewing news' title, let's say it stores in variable $news_title. We also need to have a new variable to store the list of similar titles, let's name it as $similar_title.

$similar_title = '';

To begin the MySql query, please make sure that the unwanted words are removed from the news' title. To do this, we create a new array to store all unwanted words. We can add more word as we want if we think they are not the wanted keywords.

$excluded = array(' in ',' on ',' at ',' for ',' of ',' about ',' is ',' are ',' the ',' a ',' an ',' by ',' with ');

Now, we have an array named $excluded that stores the unwanted word and to make sure that they are the words not charactors within other words, we add a space at the both side of each word.

We will remove the unwanted words from the current viewing news' title by replacing them with a space using the above excluded word as the reference.

$cleaned_title = str_replace($excluded," ",$news_title);

We now have cleaned title string store in a variable named $cleaned_title. The keywords in this string are sererated by a space that will be replaced by sql string as bellow. We might have a database table named tblnews with some fields like id, title, and description.

$sql_title = "title LIKE '%" . str_replace(" ","%' OR title LIKE '%",$cleaned_title);

Finally we have the sql string contains only the wanted keywords obtained from the current viewing news. The following lines are the querying stage in which the uft8 database query funtion named exec_query_utf8 is used.

$qry= exec_query_utf8("SELECT * FROM tblnews WHERE ($sql_title) AND title <> '$news_title' AND active=1 LIMIT 10");
while($row=mysqli_fetch_assoc($qry)){
$similar_title .= '<a href="#">'.$row['title'].'</a><br />';
}
if(mysqli_num_rows($qry)==0){$similar_title = '<div>No similar news</div>';}

The above query will give us the similar news' rows from the database.

Now let's put all the codes together.

$similar_title = '';					
$excluded = array(' in ',' on ',' at ',' for ',' of ',' about ',' is ',' are ',' the ',' a ',' an ',' by ',' with ');
$cleaned_title = str_replace($excluded," ",$news_title);
$sql_title = "title LIKE '%" . str_replace(" ","%' OR title LIKE '%",$cleaned_title);	
					
$qry= exec_query_utf8("SELECT * FROM tblnews WHERE ($sql_title) AND title <> '$news_title' AND active=1 LIMIT 10");
while($row=mysqli_fetch_assoc($qry)){
$similar_title .= '<a href="#">'.$row['title'].'</a><br />';
}
if(mysqli_num_rows($qry)==0){$similar_title = '<div>No similar news</div>';}

We now have the similar news' titles displayed.

Similar Tutorials

No similar tutorial

Comments