SUGGESTING “ARTICLES YOU MIGHT ALSO LIKE” BASED ON KEYWORDS - May 8th, 2011


User Illume Magazine wanted to be able to suggest articles on their article detail page that, based on matching
keywords, might be of interest to readers of a specific article.

This concept could also be used for showing products that share certain qualities to potential buyers.

Here’s what a collaboration between Chris Waddell from Interactive Tools and CMSB user Perchpole came up with:

For this recipe you’ll need to add a “your_keyword_field” text field to the multi-record editor that contains your
articles.

Here's the code that goes at the top of your detail viewer:

Insert the code between the lines INSERT THIS CODE and END OF INSERTED CODE into your viewer and change the path to your
viewer_functions.php and the names of your table and fields.



<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/path_to_your_server/','','../','../../','../../../');
foreach (
$dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!
function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($your_products_tableRecords, $your_products_tableMetaData) = getRecords(array(
'tableName' => 'your_products_table',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$your_products_tableRecord = @$your_products_tableRecords[0]; // get first record

// show error message if no matching record is found
if (!$your_products_tableRecord) {
header("HTTP/1.0 404 Not Found");
print
"Record not found!";
exit;
}
//INSERT THIS CODE
// construct a where clause out of this record's keywords (e.g. "apples, oranges" -> "keywords LIKE '%apples%' OR
keywords LIKE '%oranges'")

$keywords = preg_split('/,\s*/', $articlesRecord['your_keyword_field']);

$where = '0';
foreach ($keywords as $keyword) {
$where .= ' OR ';
$where .= "your_keyword_field LIKE '%" . mysql_escape($keyword) . "%'";
}

// find the most recent records with at least one matching keyword (but not including the current record)
list($similarRecords)= getRecords(array(
'tableName' => 'your_products_table',
'where' => "(" . $where . ") AND num != " . $your_products_tableRecord['num'],
'limit' => 5,
));
// END OF INSERTED CODE
?>



Then, where you want to display the titles of, and links to similar articles on the detail page, use the following code:



<?php foreach ($similarRecords as $record): ?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br /><?php endforeach ?>



The regular expression in the first line of code above will create keywords split on a comma followed by any number of
spaces. Here it is again for reference:



$keywords = preg_split('/,\s*/', $articlesRecord['keywords']);



For example, "a quick, brown fox" would turn into two keywords: "a quick" and "brown fox".

If you want to split on commas and/or spaces, you can use this regular expression instead:



$keywords = preg_split('/[,\s]+/', $articlesRecord['keywords']);



For example, "a quick, brown fox" would turn into four keywords: "a", "quick", "brown", and "fox".

NOTE: You can also use this one to list keywords in a multi-value list field.

** If you're going to use this in a multi value situation, you'll need to insure that there are no spaces between multi
word "keywords. You can use "_" or "-" to separate multiple words and then if you need to print out a keyword list for
some reason you can use another regular expression like the one below to replace each "_" or "-" with the required
spaces.



<?php foreach ($your_tableRecords as $record): ?>

<!-- Insert This Code -->

<?PHP $record['your_field'] = preg_replace("/[-_]/", "&nbsp;", $record['your_field'] ); ?>

<!-- End of Insert-->

<?php echo $record['your_field] ?>

<?php endforeach; ?>




You can learn more about the power of regular expressions in the recipe called: “REPLACING ONE CHARACTER WITH ANOTHER
USING “REGULAR EXPRESSIONS”“



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service