Job to grab X number recent posts and put in UL

Snitz™ Forums 2000
https://forum.snitz.com/forumTopic/Posts/63836?pagenum=1
05 November 2025, 02:34

Topic


garyrobar
Job to grab X number recent posts and put in UL
24 January 2007, 11:29


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
%>

 

Replies ...


JJenson
24 January 2007, 11:33


Will try and implement this later looks like a nice add on though. smile<
garyrobar
24 January 2007, 12:09


Thanks. I've added a zip of the file, since the post seems to be alterning some of the code due to formatting rules.<
runsh
24 January 2007, 15:58


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
<
JJenson
24 January 2007, 16:23


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?<
runsh
24 January 2007, 17:05


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<
JJenson
24 January 2007, 18:12


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.<
garyrobar
25 January 2007, 10:45


you have to create an empty (inc_forumrecentposts.asp) file for the first execution.
Sorry, i forgot to mention that :)<
runsh
25 January 2007, 10:50


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<
runsh
25 January 2007, 10:52


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 & "")
<
runsh
25 January 2007, 13:24


Is there a way to have one for each forum?<
runsh
25 January 2007, 16:47


list is not updating new post<
AnonJr
25 January 2007, 17:38


Has the set interval passed? Based on the initial post, it looks like this script doesn't update the list immediately upon a new post, only when the interval you set passes.<
runsh
25 January 2007, 21:07


do you know which part set the interval. is it possible to be set every hour or so?<
AnonJr
25 January 2007, 23:14


I couldn't tell you what part, but according to the first post its possible to set the interval for every hour.
Edit: after going back and re-reading the initial post one more time (seems to be taking my sleep-deprived mind a few times to finally sink in) - it seems that this script would be something you would have to set up as a regular job on the server.
Now, if you were really feeling froggy, you could probably code a check in inc_header to see if its been re-built within the time-frame and run this if it hasn't. I might be able to sketch out something a little more detailed tomorrow, but I make no promises.
For the record, while I am always in favor of cutting unnecessary database calls, I think this is going just a little too far in the other direction.<
garyrobar
26 January 2007, 05:31


Hello everyone,

Here is a version, exactly the same in every way, except that it is dynamic, and updates automatically. You simply use it as an include file whereever you want the list to be displayed.
recent-post-dynamic.rar
Just include it in the page,
make sure you edit lines 5 and 6 depending on where your files are in the directory. Then edit line 76 to your own domain name

With this one, your forum's most recent posts will display always, without any manual updating.
The OTHER option is to use the original code, and include the file at the end of post.asp. That way anyone updating, posting new, or replying to a topic will cause the file to be regenerated.<
AnonJr
26 January 2007, 06:14


Cool.<
runsh
26 January 2007, 11:00


looks good. thanks<
garyrobar
26 January 2007, 18:09


Originally posted by MrsJJenson
Is any coding suppose to go in the inc_forumrecentpost.asp? I don't see any new posts on my homepage where I would like it to show. What did i do wrong or need to do?


Just download the file from my recent post (3 posts above) and use that one instead. Its dynamic, and you just include it where you want to show the list.<
MrsJJenson
26 January 2007, 18:21


k, so i downloaded the file from above and created the blank
inc_forumrecentpost.asp. Then i changed the files paths in line
5&6 and changed the url path on line 76. then I uploaded the two
files to my site and where I want the recent posts to show on my homepage they are not showing.
<!-- INCLUDE FILE=forum/inc_forumrecentpost.asp --> this needs to be
where I want the recent posts to show, correct? What else do I need to do to fix this?<
garyrobar
26 January 2007, 22:02


the file you just downloaded is a separate version. The folks who posted earlier wanted one that you didn't have to update manually. So, just download that most recent one a few posts up, and make the changes. Then use a server side include wehreever you wanted the list to appear. no need to do anything else.<
JJenson
27 January 2007, 11:47


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.
<
garyrobar
27 January 2007, 14:21


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.<
JJenson
27 January 2007, 19:44


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.<
garyrobar
27 January 2007, 21:15


actuallly, you should change the SQL statement to only grab from records of a certain forum id- are you familliar with SQL?<
JJenson
27 January 2007, 21:18


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<
garyrobar
29 January 2007, 11:03


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
<
runsh
29 January 2007, 19:13


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). <
garyrobar
29 January 2007, 21:17


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.<
runsh
30 January 2007, 00:29


great mod. thanks<
© 2000-2021 Snitz™ Communications