Job to grab X number recent posts and put in UL - Posted (3548 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
Looks good gary, One thing to make it work on my site I had to remove the option explicit on the top. Now everything works just as it should. I don't think its needed but i could be wrong.
<
Posted
New Member
garyrobar
Posts: 90
90
Glad you like!
Option Explicit isn't really needed.

It just forces you to code properly. if you have any undeclared variables anywehre on the page you are executing, you'll get errors with option explicit.<
Posted
Advanced Member
JJenson
Posts: 2121
2121
Yeah figured it was something like that. Now I know you didn't make it this way but h ow hard would it be to convert to only topics from a certain forum get posted? I was trying to do this and changed the url and that didn't work and figured it owuld be a little more difficult than with my knowledge. smile But I will take a stab at it.<
Posted
New Member
garyrobar
Posts: 90
90
actuallly, you should change the SQL statement to only grab from records of a certain forum id- are you familliar with SQL?<
Posted
Advanced Member
JJenson
Posts: 2121
2121
Not really no I am just now starting to expand my knowledge in these areas. Maybe give me an example and I should be able to take it from there? Thanks<
Posted
New Member
garyrobar
Posts: 90
90
okay so make these adjustments to display only posts from ONE forum...
Change this...
Code:

intResults = 10
intAge = 30
lastDate = DateToStr(DateAdd("d",-(intAge),Now()))
strMethod = "last_post"
subjectlength = 122
subjectlengthe = 60
intPage = 1

to this...
Code:

intResults = 10
intAge = 30
lastDate = DateToStr(DateAdd("d",-(intAge),Now()))
strMethod = "last_post"
subjectlength = 122
subjectlengthe = 60
intPage = 1
displayForum = 26 'enter the FORUM_ID, put 111111 for display all.

and then change the SQL statements from this...
Code:

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"

to this...
Code:

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"

if displayforum <> 111111 then
strSql = strSql & " WHERE T.T_STATUS < 2"
strSql = strSql & " AND F.FORUM_ID = " & displayForum & " ORDER BY T.T_LAST_POST DESC"
else
strSql = strSql & " WHERE T.T_STATUS < 2 ORDER BY T.T_LAST_POST DESC"
end if
<
Posted
Starting Member
runsh
Posts: 34
34
thanks for changing it. works great. I was wondering if is possible to change it bit more so to allow category (all forums in cat. to display). <
Posted
New Member
garyrobar
Posts: 90
90
Yes, it is. We just need to do two things:

1 - Add a variable called "displayCat" and set it equal to the category ID of the category you want to display. 2 - Find the "WHERE" clause:

Code:

if displayforum <> 111111 then
strSql = strSql & " WHERE T.T_STATUS < 2"
strSql = strSql & " AND F.FORUM_ID = " & displayForum & " ORDER BY T.T_LAST_POST DESC"
else
strSql = strSql & " WHERE T.T_STATUS < 2 ORDER BY T.T_LAST_POST DESC"
end if

...and change it to

Code:

if displayforum <> 111111 then
strSql = strSql & " WHERE T.T_STATUS < 2"
strSql = strSql & " AND F.CAT_ID = " & displayCat & " ORDER BY T.T_LAST_POST DESC"
else
strSql = strSql & " WHERE T.T_STATUS < 2 ORDER BY T.T_LAST_POST DESC"
end if

SQL is actually quite logical. The "WHERE" clause just helps you narrow down the chink of records you are grabbing from the database. SO, if you only want records from say, category ID 7, the SQL essentially says:

"get me (select) all records from the certain tables that have the data I need, and only get me records WHERE some condition is true. (cat_id = 7)"

Hope this helps.<
Posted
Starting Member
runsh
Posts: 34
34
great mod. thanks<
You Must enter a message