I have a website that will showcase various publications. One publication will be highlighted on the homepage, and then 6 related publications will show alongside the highlighted one. The publications are related by topic and each publication can be categorized under a max of 3 topics. For example, if the highlighted pub is categorized under "health", "technology", and "policy", I then go into the database to find two publications that are categorized under "health", two more categorized under "tech", and two more under "policy".
The code below works fine EXCEPT when related publications are categorized under more than one of the highlighted publication's categories. Let's say Publication A is categorized under "health" and "technology". The results I get look like this - which I don't want:
Health
Publication A <
Publication B
Technology
Publication A <
Publication C
Policy
Publication D
Publication E
I'm not sure how to code my database query so that it excludes a publication ID if it's already been selected in a previous category. Any help -- REALLY appreciated! Thanks! Here's my code:
/*
We already have what we need for the highlighted publication, including what topics it's under. Now we figure out how many topics we are dealing with and then limit the query for related publications to 6, 3, or 2 depending on the number of topics
*/
$row_count_topic = mysql_num_rows($result_topic);
if ($row_count_topic == '1') {
$limit_to = '6';
} elseif ($row_count_topic == '2') {
$limit_to = '3';
} else {
$limit_to = '2';
}
while ($row_topic = mysql_fetch_array($result_topic)) {
$topic_id = $row_topic['topic_id'];
$topic_name = $row_topic['topic_name'];
// Now get related publications for each topic
$sql = "SELECT DISTINCT
content.publications.pid AS pid,
content.publications.title AS title,
FROM
content.publications,
content.topics,
WHERE
content.topics.topic_id = '$topic_id'
AND
content.topics.pub_id = content.publications.pid
AND
content.publications.highlighted != '1'
LIMIT 0,$limit_to";
$result_related = mysql_query($sql);
$row_count_related = mysql_num_rows($result_related);
if ($row_count_related > 0) {
echo ("<p>$topic_name</p>");
while ($row_related = mysql_fetch_array($result_related)) {
$title_related = $row_related['title'];
echo ("<P><b>$title_related</b></p>");
}
}
}