ScottA
10-15-2002, 10:07 PM
This isn't really a hack, more of an add-on for vBulletin.
What I'm trying to do is create a method of displaying related topics at the end of articles for NissanPerformanceMag.com (http://www.nissanperformancemag.com). This isn't the same as the "Last XX Posts on non-vB page (https://vborg.vbsupport.ru/showthread.php?s=&threadid=12324)" hack. Instead, it uses some specified keywords to display 10 discussion topics related to the subject of the article. I haven't been able to locate any similar projects, so I attempted it myself. :)
It's easy to get lost in the search.php file (at least for me :)), but I was able to figure out enough to get this far. It displays the queries used, as well as the final results. The problem is it doesn't work very well with multiple words, such as this:
I can't figure out how to make it display threads containing all of the search words. Right now it uses "OR" instead of "AND", if you can see what I mean. It also doesn't give any weight to results. If anyone can offer some insight into methods of improving this I would appreciate it very much! :) What I would like to do (using the second link as an example), is display threads containing all the search words instead of simply displaying threads that contain just one.
Here's the code:
<?php
mysql_connect("localhost", "username", "password");
$words = strtolower($_GET['query']);
$words = explode(' ', $words);
for ($i = 0; $i < count($words); $i++) {
$searchwords .= "title = '{$words[$i]}'";
if ($i+ 1 < count($words)) {
$searchwords .= ' OR ';
}
}
$query = "SELECT wordid FROM word WHERE $searchwords";
echo "<p>$query";
unset($searchwords);
$result = mysql_db_query("forums", $query);
if (mysql_num_rows($result) >= 1) {
$i = 1;
while ($r = mysql_fetch_array($result)) {
if ($i > 1) {
$searchwords .= ', ';
}
$searchwords .= "'{$r['wordid']}'";
$i++;
}
$query = "SELECT DISTINCT postid FROM searchindex WHERE wordid IN($searchwords)";
echo "<p>$query";
unset($searchwords);
$result = mysql_db_query("forums", $query);
$i = 1;
while ($r = mysql_fetch_array($result)) {
if ($i > 1) {
$searchwords .= ', ';
}
$searchwords .= "'{$r['postid']}'";
$i++;
}
$query = "SELECT threadid FROM post WHERE postid IN($searchwords) ORDER BY dateline DESC";
echo "<p>$query";
unset($searchwords);
$result = mysql_db_query("forums", $query);
$i = 1;
while ($r = mysql_fetch_array($result)) {
if ($i > 1) {
$searchwords .= ', ';
}
$searchwords .= "'{$r['threadid']}'";
$i++;
}
$query = "SELECT threadid, title, replycount, views FROM thread
WHERE threadid IN($searchwords) ORDER BY lastpost DESC LIMIT 10";
echo "<p>$query";
$result = mysql_db_query("forums", $query);
while ($r = mysql_fetch_array($result)) {
echo "<p><a href=\"http://www.nissanforums.com/showthread.php?threadid={$r['threadid']}\">{$r['title']}</a>";
}
}
mysql_close();
?>
What I'm trying to do is create a method of displaying related topics at the end of articles for NissanPerformanceMag.com (http://www.nissanperformancemag.com). This isn't the same as the "Last XX Posts on non-vB page (https://vborg.vbsupport.ru/showthread.php?s=&threadid=12324)" hack. Instead, it uses some specified keywords to display 10 discussion topics related to the subject of the article. I haven't been able to locate any similar projects, so I attempted it myself. :)
It's easy to get lost in the search.php file (at least for me :)), but I was able to figure out enough to get this far. It displays the queries used, as well as the final results. The problem is it doesn't work very well with multiple words, such as this:
I can't figure out how to make it display threads containing all of the search words. Right now it uses "OR" instead of "AND", if you can see what I mean. It also doesn't give any weight to results. If anyone can offer some insight into methods of improving this I would appreciate it very much! :) What I would like to do (using the second link as an example), is display threads containing all the search words instead of simply displaying threads that contain just one.
Here's the code:
<?php
mysql_connect("localhost", "username", "password");
$words = strtolower($_GET['query']);
$words = explode(' ', $words);
for ($i = 0; $i < count($words); $i++) {
$searchwords .= "title = '{$words[$i]}'";
if ($i+ 1 < count($words)) {
$searchwords .= ' OR ';
}
}
$query = "SELECT wordid FROM word WHERE $searchwords";
echo "<p>$query";
unset($searchwords);
$result = mysql_db_query("forums", $query);
if (mysql_num_rows($result) >= 1) {
$i = 1;
while ($r = mysql_fetch_array($result)) {
if ($i > 1) {
$searchwords .= ', ';
}
$searchwords .= "'{$r['wordid']}'";
$i++;
}
$query = "SELECT DISTINCT postid FROM searchindex WHERE wordid IN($searchwords)";
echo "<p>$query";
unset($searchwords);
$result = mysql_db_query("forums", $query);
$i = 1;
while ($r = mysql_fetch_array($result)) {
if ($i > 1) {
$searchwords .= ', ';
}
$searchwords .= "'{$r['postid']}'";
$i++;
}
$query = "SELECT threadid FROM post WHERE postid IN($searchwords) ORDER BY dateline DESC";
echo "<p>$query";
unset($searchwords);
$result = mysql_db_query("forums", $query);
$i = 1;
while ($r = mysql_fetch_array($result)) {
if ($i > 1) {
$searchwords .= ', ';
}
$searchwords .= "'{$r['threadid']}'";
$i++;
}
$query = "SELECT threadid, title, replycount, views FROM thread
WHERE threadid IN($searchwords) ORDER BY lastpost DESC LIMIT 10";
echo "<p>$query";
$result = mysql_db_query("forums", $query);
while ($r = mysql_fetch_array($result)) {
echo "<p><a href=\"http://www.nissanforums.com/showthread.php?threadid={$r['threadid']}\">{$r['title']}</a>";
}
}
mysql_close();
?>