This is the third article about .
Once you have started tagging your entries this information can be used in several contexts and one of my favourites is the related blog entries functionality.
For each blog entry that is loaded to the user - tags for that entry needs to be matched to other tags in the database. All of these other tags are being used in the same way as a marker or keyword for one or several other entries. So other entries that have one or several of those matching tags will be displayed as related blog entries. This is a very dynamic and nice solution that updates both old and new entries with the most recent information automatically.
So here are again a couple of php-functions to solve this. As in the former example it is assumed that the tags are stored as a #-separated string with a leading and trailing # in the database for each entry.
Make these two calls in the place where you want to display related entries:
$resultSet = returnMatchingDesignsByTagsAsResult($row['tags'], "#"); printRelatedEntries($resultSet, $row['id']); |
Where $row['tags'] is the tags-string.
The function 'returnMatchingDesignsByTagsAsResult' performs a query to the database for entries with matching tags and returns a resultset with those entries. Here is where it comes to use with the leading and trailing # in the tags-string:
/* Return query result that contains design rows with matching tags */ function returnMatchingDesignsByTagsAsResult($tagsString, $delimiter) { $mysql_table = 'the_table'; $counter = 0; $tags = array(); /* Remove # characters at the beginning and at the end */ $subTags = substr($tagsString, 1, -1); /* Split string into array */ $subTags = split($delimiter, $subTags); /* Loop through array */ foreach ($subTags as $value) { $tags[$counter] = strtolower(trim($value)); $counter = $counter + 1; } /* Create beginning of SQL-string */ $queryString = sprintf("SELECT * FROM %s WHERE ((published = 'yes') AND (", $mysql_table); $counter = 0; /* Loop through array of tags */ foreach ($tags as $value) { $tmpStr = "%" . $delimiter . $value . $delimiter . "%";
/* Find all entries that have a column value 'tags' which includes a substring with the pattern #tag# */ /* If first element in array */ if ($counter == 0) { $queryString .= sprintf("(tags LIKE '%s')", $tmpStr); } else { $queryString .= sprintf(" OR (tags LIKE '%s')", $tmpStr); } $counter = $counter + 1; }
/* Complete the SQL-string */ $queryString .= ")) ORDER BY publishDate DESC";
$result_set = mysql_query($queryString); if (!$result_set) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $queryString; die($message); } return $result_set; } |
Function 'printrelatedEntries' uses the result set from the other function and the id of the current design to print related entries to the web page:
function printRelatedEntries($resultSet, $id) { /* If there is at least on hit */ if (mysql_num_rows($resultSet) > 1) { Print "<tr valign=\"middle\"><td></td><td>"; Print "<b>Related blog entries:</b> <br />"; while ($row2 = mysql_fetch_assoc($resultSet)) { /* Don't print the entry if it is the same as the current since this is not excluded from the result set */ if ($row2['id'] != $id) { Print sprintf("- <a href=\"http://www.wohill.com/design/%s/%s.html\" title=\"%s\">%s</a><br />", $row2['id'], createUrlTitle($row2['name']), $row2['name'], $row2['name']); } } Print "</td><td></td><td></td></tr>"; } } |
You still have to create your own sql-table and fill it with data to query of course, but hopefully this will help someone on the way for their specific solution.