How to add links to related posts on the current post page PHP SQL
Im quite new to php and sql and Im trying to build a news site and im really stuck on trying to display a list of links to posts related to the current post being displayed,here is the code for the topics page
<?php
$sql = "SELECT
topic_id,
topic_subject,
topic_image,
topic_content
FROM
topics
WHERE
topics.topic_id = " . mysql_real_escape_string($_GET['id']);
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
echo '<h3>' . $row['topic_subject'] . '<h3>
<p>' . $row['topic_content'] . '</p>
<p>' . $row['topic_image'] . '</p>';
}
?>
开发者_JAVA技巧
And here is the table from the database im using
CREATE TABLE topics (
topic_id INT(8) NOT NULL AUTO_INCREMENT,
topic_subject VARCHAR(255) NOT NULL,
topic_content TEXT NOT NULL,
topic_date DATETIME NOT NULL,
topic_cat INT(8) NOT NULL,
topic_by INT(8) NOT NULL,
PRIMARY KEY (topic_id)
) TYPE=INNODB;
If at all possible could someone give me the MYSQL and PHP for this Do ye recommend I add a new field to the database called Tags and use that in tandom with the topic_subject Any help would be really appreciated This is my first question on SO Thanks again guys
I would recommend using a tags field in the table so the articles you retrieve are really related to the current article.
Another options would be to include articles in the same category.
If you want to have more control over what is displayed as related articles, setting a list or related articles in at the time you are writing an article would be a better approach, as the related content might be updated later.
Here's the code I used:
$parameters = array();
$content = '<p>Lorem ispum dolor sit amet.</p><p>{relatedcontent ids="1,2,3,4"}</p>';
if (strpos($content, 'relatedcontent') !== false) {
$pluginPattern = '#{relatedcontent\s*(.*?)}#s';
preg_match_all($pluginPattern, $content, $matches);
if ($pluginCount = count($matches[0])) {
for ($i = 0; $i < $pluginCount; $i++) {
$parameterPattern = '#(\w+)=\"(.*)\"#s';
preg_match_all($parameterPattern, $matches[0][$i], $params);
$parametersCount = count($params[1]);
for ($j = 0; $j < $parametersCount; $j++) {
$parameters[strtolower($params[1][$j])] = $params[2][$j];
}
}
}
}
if (!empty($parameters['ids'])) {
$ids = explode(',', $parameters['ids']);
var_dump($ids);
}
With this code you can then add more parameters to your 'plugin code' like {relatedcontent width="500"...display="above"}
.
I hope it helps.
精彩评论