Find related topics while posting
From PunBB Resource Wiki
This is not finished yet but its functionality is ok
- Function:
- Shows the 5 most relevant topics as soon as you finish typing your topic title
- Problems:
- Requires full text search (could be changed if someone has the time)
- Degradablity:
- Has no affect on any normal functions and will not show anything if javascript is disabled.
[edit]
Installation
Add this to header.php:
<script type="text/javascript"> var ajax=false; /*@cc_on @*/ /*@if (@_jscript_version >= 5) try { ajax = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { ajax = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { ajax = false; } } @end @*/ if (!ajax && typeof XMLHttpRequest!='undefined') { ajax = new XMLHttpRequest(); } function getMyHTML(serverPage, objID) { var obj = document.getElementById(objID); ajax.open("GET", serverPage); ajax.onreadystatechange = function() { if (ajax.readyState == 4 && ajax.status == 200) { obj.innerHTML = ajax.responseText; } } ajax.send(null); } </script>
Replace this in post.php:
if ($fid): ?> <label><strong><?php echo $lang_common['Subject'] ?></strong><br /><input class="longinput" type="text" OnBlur="getMyHTML('searchrelated.php?value=' + document.getElementById('post').req_subject.value,'ajaxwrapper')" name="req_subject" value="<?php if (isset($_POST['req_subject'])) echo pun_htmlspecialchars($subject); ?>" size="80" maxlength="70" tabindex="<?php echo $cur_index++ ?>" /><br /></label> <div id="ajaxwrapper"> </div> <?php endif; ?>
<div id="ajaxwrapper"> will get filled with the text from test.php?value=(the value of the subject will be put here) every time the subject field loses focus.
Create searchrelated.php and put this in it:
<?php define('PUN_ROOT', './'); require PUN_ROOT.'include/common.php'; require PUN_ROOT.'include/parser.php'; $result = $db->query('SELECT t.subject, p.id, p.topic_id, p.message FROM '.$db->prefix.'posts AS p LEFT JOIN '.$db->prefix.'topics AS t ON p.topic_id=t.id INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND MATCH (p.message) AGAINST (\''.$_GET['value'].'\') limit 0,5') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error()); if ($db->num_rows($result) != 0) { while ($row = $db->fetch_assoc($result)) { echo $row['subject'] . ' - ' . parse_message($row['message'],0); } } else { echo "No similar topics"; }
Using fulltext search (sorry too lazy to work out search.php ;)) You will need to add a fulltext index to the posts.message field

