Job to grab X number recent posts and put in UL - Posted (3552 Views)
New Member
garyrobar
Posts: 90
90
Description: Use this to show recent forum topics in a list on other non-forum areas of your website. This helps "grab" users from other areas of your site and bring them to the forum.
I made this code to grab a specified number of the most recent active posts. It puts the info in to a file as an unordered list. It creates and writes an html file with the info, so that every pagevew isn't grabbing information from the db. You could set this as a job to run every X hours/minutes/days whatever.
It also scrubs all formatting out of the post, so that it displays normally. It converts all tags back to html, then uses a regular expression to get rid of all tags.
You can specify the length of the subject and description.
I'm sorry I don't have time to make it more easy to implement, but thought i'd share what i have so far.
You may just need to adjust the pathway to some of the included files, and you'll also have to adjust the path and filename of where you'll want to write the list to. Remember, you'll need write permissions on the directory that you choose to write the file to.
Any improvements or changes are welcomed, and I hope this helps someone out.
<b>FYI - Since some of the code isn't showing properly here because of formatting rules, i've zipped up the file for download:</b>

generate_recent_posts.rar recent-post-dynamic.rar (Dynamic Version)
Code:

<!--#INCLUDE FILE="../../forum/config.asp"-->
<!--#INCLUDE FILE="../../forum/inc_func_common.asp"-->
<%
set my_Conn = Server.CreateObject("ADODB.Connection")
my_Conn.Open strConnString

intResults = 10 '#of results to show
intAge = 30 'age in days of topics to look through
lastDate = DateToStr(DateAdd("d",-(intAge),Now())) 'leave this alone
strMethod = "last_post"
subjectlength = 122 'length in characters of the subject line
subjectlengthe = 60 'length in characters of the description line
intPage = 1 'leave alone


strSql = "SELECT F.F_SUBJECT, T.TOPIC_ID, T.T_AUTHOR, T.T_SUBJECT, T.T_MESSAGE"
strSql = strSql & ", T.T_LAST_POST, T.T_LAST_POST_REPLY_ID, T.T_REPLIES, T.T_DATE, M.M_NAME"
strSql = strSql & ", MEMBERS_1.M_NAME AS LAST_POST_AUTHOR_NAME"
strSql = strSql & " FROM ((" & strTablePrefix & "FORUM F LEFT JOIN " & strTablePrefix & "TOPICS T"
strSql = strSql & " ON F.FORUM_ID = T.FORUM_ID) LEFT JOIN " & strMemberTablePrefix & "MEMBERS M"
strSql = strSql & " ON T.T_AUTHOR = M.MEMBER_ID) LEFT JOIN " & strMemberTablePrefix & "MEMBERS MEMBERS_1"
strSql = strSql & " ON T.T_LAST_POST_AUTHOR = MEMBERS_1.MEMBER_ID"
strSql = strSql & " WHERE T.T_STATUS < 2 ORDER BY T.T_LAST_POST DESC"



'-----------------------------------------end the getting of the data------------------


Set getForumRecentPostsFSO = Server.CreateObject("Scripting.FileSystemObject")
getForumRecentPostsFSO.DeleteFile "E:\path\path\path\path\path\dev\bfadmin\write\inc_forumrecentposts.asp" 'delete old one
Set getForumRecentPostsFile = getForumRecentPostsFSO.CreateTextFile("E:\path\path\path\path\path\dev\bfadmin\write\inc_forumrecentposts.asp", True) 'create new one
getForumRecentPostsFile.WriteLine("<ul class=""ulnav"">")


set objRs = my_Conn.execute(TopSQL(strSql,intResults))

do until objRs.eof

intID = objRs("TOPIC_ID")
strSubject = ChkString(objRs("T_SUBJECT"),"display")

if Len(strSubject) > (subjectlength - 1) then
strSubject = mid(strSubject,1,subjectlength) & "..."
end if

strTopicPart = ChkString(objRs("T_MESSAGE"),"display")
strFSubject = ChkString(objRs("F_SUBJECT"),"display")


strTopicPart = replace(strTopicPart, "<b>", "<b>") 'replace <b> open
strTopicPart = replace(strTopicPart, "</b>", "</b>") 'replace </b> close
strTopicPart = replace(strTopicPart, "<u>", "<u>") 'replace <u> open
strTopicPart = replace(strTopicPart, "</u>", "</u>") 'replace </u> close
strTopicPart = replace(strTopicPart, "<i>", "<i>") 'replace <i> open
strTopicPart = replace(strTopicPart, "</i>", "</i>") 'replace </i> close
strTopicPart = replace(strTopicPart, "[url="", "<a href=""") 'replace url open
strTopicPart = replace(strTopicPart, "[/url]", "</a>") 'replace close of /a tag
strTopicPart = replace(strTopicPart, "[img]", "<img src=""") 'replace img tag and set invisible
strTopicPart = replace(strTopicPart, "[/img]", """ />") 'replace close of img tag
strTopicPart = replace(strTopicPart, ""]", """>") 'replace url end enclosure
strTopicPart = replace(strTopicPart, "<font", "<font ") 'replace font tag and comment out
strTopicPart = replace(strTopicPart, "</font", "</font>< ") 'replace font tag and comment out
strTopicPart = replace(strTopicPart, "">", " "">") 'replace font tag and comment out
strTopicPart = replace(strTopicPart, "<ul>", "<ul>") 'replace font tag and comment out
strTopicPart = replace(strTopicPart, "</ul>", "</ul>") 'replace font tag and comment out
strTopicPart = replace(strTopicPart, "<li>", "<li>") 'replace font tag and comment out
strTopicPart = replace(strTopicPart, "</li>", "</li>") 'replace font tag and comment out

'bust out the rgex here, and get rid of all tags.
strTopicPart = removeHTML(strTopicPart)

'end regex bust out.
strTopicPart = replace(strTopicPart, """, """") 'replace ascii quote with straight quote.
'pear down the length of the description now that code has been cleaned. if Len(strTopicPart) > (subjectlengthe - 1) then
strTopicPart = mid(strTopicPart,1,subjectlengthe) & "..."
end if

getForumRecentPostsFile.WriteLine("<li>")
getForumRecentPostsFile.WriteLine("<a href=""http://www.site.com/forum/topic.asp?TOPIC_ID=" & intID & "&whichpage=" & intPage & """ title=""" & strSubject & """>" & strSubject & "</a> - " & strTopicPart & "")
getForumRecentPostsFile.WriteLine("</li>")

objRs.MoveNext
loop

getForumRecentPostsFile.WriteLine("</ul>")

'this is the function that removes the html from the post
Function RemoveHTML( strText )
Dim RegEx

Set RegEx = New RegExp

RegEx.Pattern = "<[^>]*>"
RegEx.Global = True

RemoveHTML = RegEx.Replace(strText, "")
End Function

objRs.close
set objRs = nothing
my_conn.close
set my_Conn = nothing
getForumRecentPostsFile.close
set getForumRecentPostsFSO = nothing
set getForumRecentPostsFile = nothing
%>
 Sort direction, for dates DESC means newest first  
 Page size 
Posted
Advanced Member
JJenson
Posts: 2121
2121
Will try and implement this later looks like a nice add on though. smile<
Posted
New Member
garyrobar
Posts: 90
90
Thanks. I've added a zip of the file, since the post seems to be alterning some of the code due to formatting rules.<
Posted
Starting Member
runsh
Posts: 34
34
Thanks for the mod
I am getting this error

Microsoft VBScript runtime error '800a0035'
File not found

/generate_recent_posts.asp, line 31


line 31 getForumRecentPostsFSO.DeleteFile "E:\path\path\path\path\path\dev\bfadmin\write\inc_forumrecentposts.asp" 'delete old one
<
Posted
Advanced Member
JJenson
Posts: 2121
2121
Just to make sure did you edit that line to point to where you are trying to generate recent post. Or where you put this file?<
Posted
Starting Member
runsh
Posts: 34
34
I did change it but was not sure if I was doing it right. should the path be same as path to database? do we have to make inc_forumrecentposts.asp? where should be placed? etc... little confusing!
some instruction would be nice on what needs to be done to make it work. thanks<
Posted
Advanced Member
JJenson
Posts: 2121
2121
Not sure I cannot get to download the file. He said there is some code missing from the text. So I will see when I can get the code what needs to be done.<
Posted
New Member
garyrobar
Posts: 90
90
you have to create an empty (inc_forumrecentposts.asp) file for the first execution.
Sorry, i forgot to mention that :)<
Posted
Starting Member
runsh
Posts: 34
34
Well I got it to work. is very nice (so far). to install:
1. download the page form link above. 2. make changes t line 31 and 32 according to your path. 3. make a page called inc_forumrecentposts.asp
4. upload both pages and you can included in almost anypage. again thanks for the mod is pretty good<
Posted
Starting Member
runsh
Posts: 34
34
also change this line to your website and at end of page redirect line. getForumRecentPostsFile.WriteLine("<a href=""http://www.site.com/forum/topic.asp?TOPIC_ID=" & intID & "&whichpage=" & intPage & """ title=""" & strSubject & """>" & strSubject & "</a> - " & strTopicPart & "")
<
Posted
Starting Member
runsh
Posts: 34
34
Is there a way to have one for each forum?<
You Must enter a message