webForumDet fria alternativet

Hämta senaste inlägget?

ASP

26 svar · 952 visningar · startad av Carro_

Medlem sedan feb. 200745 inlägg
Frågan#1

Hej
Håller på och trixar med en "senaste inlägget" funktion.

Koden jag har idag ser ut så här, och den hämtar senaste tråden som är postad, men jag vill att den ska lista senaste tråden som någon har skrivit i.

<%

Set objConn = Server.CreateObject("ADODB.Connection") 
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("fo.mdb")
'//Converts UNIX timestamps into the format the current webserver use.
Function uTimeStamp(byval dDate) '// Bring the date back from database and show in current session.LCID
on error resume next
uTimeStamp = DateAdd("s", dDate, "01/01/1970 00:00:00")
if err then
cTimeStamp = int(0)
end if
End Function

dim strSQL
strSQL = "SELECT TOP 10 * FROM aspbb_topics WHERE tforumid=6 ORDER BY tdatecreate DESC" 'Change tforumid=30 to the id of your newsthread
set objNews = objConn.Execute(strSQL)

set objRegExp = new regexp
objRegExp.global = true
objRegExp.pattern = "\[img=([^\]]+)\]"
Do Until objNews.EOF
 Response.Write "<p><div class=""aspbbcolormain"">" & objNews("ttitle") & "&nbsp;-&nbsp;" & uTimeStamp(objNews("tdatecreate")) & "</div><br/>" & vbNewLine
 strText = Replace(objNews("tbody"), vbcrlf, "<br/>") & "<br/>"
 strText = objRegExp.replace(strText,"<img border=""0"" src=""$1""><br/>")
 response.write strText
 Response.write "<div style=""text-align:right""><a href=""topic.asp?TID=" & objNews("tid") & """>Läs</a></div>" & vbNewLine
 Response.Write "<br/></p>" & vbNewLine 
 objNews.MoveNext
Loop
set objRegExp = nothing
   
objNews.Close : Set objNews = Nothing
objConn.Close : Set objConn = Nothing
%>

I självaste forumet finns denna funktionen men det är en fristående sida jag håller på med så det är inte mycket till hjälp.

Men koden som finns i forumet ser ut så här.

<%
	LanguageParser.IncludeSection "latestposts"
%>
&nbsp;<span class="Stort"><%= langLatestPost %>: (<%=intDisplayThreads%>)</span>
<table width="<%=strTable%>" align="<%=strAlign%>" border="0" cellspacing="1" cellpadding="4" class="aspbbmain">
  <tr class="aspbbcolormain"> 
    <td class="aspbbcolormain"><% =langThread %></td>
    <td class="aspbbcolormain" width="120"><center><% =langAuthor %></center></td>
    <td class="aspbbcolormain" width="120"><center><% =langLastPost %></center></td>
    <td width="70"  class="aspbbcolormain"><center><% =langReplies %></center></td>
    <td width="70"  class="aspbbcolormain"><center><% =langViews %></center></td>
  </tr>
  <% 
	
	dim arrForum, x, strLastPoster, arrLastPoster, strLastPosterId, postid, arrTempCategories, accessedSubs
		'/// Our SQL query
		if IsAdmin(userId) Then
			strSQL = "SELECT aspbb_topics.ttitle, aspbb_topics.ttype, aspbb_topics.tanswers, aspbb_topics.tseen, aspbb_topics.tid, aspbb_topics.tpoll, aspbb_topics.tbody, aspbb_members.mid, aspbb_members.muser " & _
			"FROM aspbb_topics INNER JOIN aspbb_members ON aspbb_topics.tmemberid = aspbb_members.mid " & _
			"WHERE ((aspbb_topics.ttype = 0 AND aspbb_topics.tshow = 1 AND aspbb_topics.toldid = 0) OR (aspbb_topics.ttype = 2 AND aspbb_topics.tshow = 1 AND aspbb_topics.toldid = 0)) ORDER BY aspbb_topics.tdatelast DESC" 
		Else
			strSQL = "Select sid from aspbb_subcategory"
			objDAL.Open arrTempCategories, strSQL, Empty
			if not EOF(arrTempCategories) Then
				for i = 0 to UBound(arrTempCategories, 2)
					if AccessSub(userid, arrTempCategories(0, i)) Then
						if accessedSubs <> "" then accessedSubs = accessedSubs & ","
						accessedSubs = accessedSubs & "" & arrTempCategories(0, i) & ""
					End if
				Next
			else
				accessedSubs = 0
			End if
			
			strSQL = "SELECT aspbb_topics.ttitle, aspbb_topics.ttype, aspbb_topics.tanswers, aspbb_topics.tseen, aspbb_topics.tid, aspbb_topics.tpoll, aspbb_topics.tbody, aspbb_members.mid, aspbb_members.muser " & _
			"FROM aspbb_topics INNER JOIN aspbb_members ON aspbb_topics.tmemberid = aspbb_members.mid " & _
			"WHERE (((aspbb_topics.ttype = 0 AND aspbb_topics.tshow = 1 AND aspbb_topics.toldid = 0) OR (aspbb_topics.ttype = 2 AND aspbb_topics.tshow = 1 AND aspbb_topics.toldid = 0))"

			
			if len(accessedSubs) > 0 Then
				strSQL = strSQL & " AND aspbb_topics.tforumid in(" & accessedSubs & "))"
			Else
				strSQL = strSQL & " AND aspbb_topics.tforumid in(" & 0 & "))"
			End if
			 strSQL = strSQL & " ORDER BY aspbb_topics.tdatelast DESC" 
		End if
		objDAL.GetRows arrForum, intDisplayThreads, strSQL, empty
	
		if not EOF(arrForum) then
			for i = 0 to UBound(arrForum, 2)
		
				'// Last post was made by?
				strLastPoster = "-"
				strSQL = "Select aspbb_members.muser,aspbb_members.mid, aspbb_posts.pid FROM aspbb_posts INNER JOIN aspbb_members ON aspbb_members.mid=aspbb_posts.pmemberid WHERE aspbb_posts.ptopicid= {0} ORDER BY aspbb_posts.pdatecreate DESC" 
				objDAL.GetRows arrLastPoster, 1, strSQL, Array(arrForum(4, i))
				
				If Not EOF(arrLastPoster) Then
					strLastPoster = arrLastPoster(0, 0)
					strLastPosterId = arrLastPoster(1, 0)
					postid = arrLastPoster(2,0)
				End if
				 
				'//Check if this is an administrative part of the forum

					If arrForum(1, i) = 0 or arrForum(1, i) = 2 then 
						if x mod 2 then
							dim mouseover
							if blnThirdColor then
								mouseover = " onmouseover=""this.className='aspbbcolor3'"" onmouseout=""this.className='aspbbcolor1'"""
							end if
	%>
	
							<tr class="aspbbcolor1"<%=mouseover%>> 
								<td>
									<%if arrForum(1, i) = 2 then 
										response.write("<span class=""stort"">"& langSticky &": </span>")
									End if
									If arrForum(5, i) <> 0 then 
										Response.Write("<span class=""stort"">"& langPoll &": </span>")
									End if%>
										<a href="topic.asp?COMMAND=GOTOLAST&TID=<%=arrForum(4, i)%>#<%=postid%>"><%=Server.HTMLEncode(arrForum(0, i))%></a>
								</td>
								<td class="center"><a href="profile.asp?PROFILE_ID=<%=arrForum(7, i)%>"><%=arrForum(8, i) %></a></td>
								<td class="center">
									<% If strLastPoster <> "-" then %>
										<a href="profile.asp?PROFILE_ID=<%=strLastPosterId %>"><% =strLastPoster%></a>
									<% Else %>
										<% =strLastPoster%>
									<% End if%>
								</td>
								<td class="center"><%=arrForum(2, i)%>&nbsp;</td>
								<td class="center"><%=arrForum(3, i)%>&nbsp;</td>
							</tr>
						<% Else
							if blnThirdColor then
								mouseover = " onmouseover=""this.className='aspbbcolor3'"" onmouseout=""this.className='aspbbcolor2'"""
							end if	
						 %>
							<tr class="aspbbcolor2"<%=mouseover%>> 
								<td>
									<%if arrForum(1, i) = 2 then 
										response.write("<span class=""stort"">"& langSticky &": </span>")
									End if
									If arrForum(5, i) <> 0 then 
										Response.Write("<span class=""stort"">"& langPoll &": </span>")
									End if%>
										<a href="topic.asp?COMMAND=GOTOLAST&TID=<%=arrForum(4, i)%>#<%=postid%>"><%=Server.HTMLEncode(arrForum(0, i))%></a>
								</td>
								<td class="center"><a href="profile.asp?PROFILE_ID=<%=arrForum(7, i)%>"><%=arrForum(8, i) %></a></td>
								<td class="center">
									<% If strLastPoster <> "-" then %>
										<a href="profile.asp?PROFILE_ID=<%=strLastPosterId %>"><% =strLastPoster%></a>
									<% Else %>
										<% =strLastPoster%>
									<% End if%>
								</td>
								<td class="center"><%=arrForum(2, i)%>&nbsp;</td>
								<td class="center"><%=arrForum(3, i)%>&nbsp;</td>
					
							</tr>
	
	  <%					End If
									
						x = x + 1
					
					Elseif arrForum(1, i) = 1 then 'Sticky announcment
	  %>
						<tr class="aspbbcolor1"> 
							<td class="aspbbcolor2" colspan="5"><strong><%=Server.HTMLEncode(arrForum(0, i))%></strong><br /><%=arrForum(6, i)%></td>
					
						</tr>
	  
	  <%								
					End if				
							
			Next
			
			
		else
			Response.write "<tr class=""aspbbmain1""><td width=""20""></td><td colspan=""5"" class=""aspbbmain1"">" & LanguageParser.GetKey("errors", "ErrorNoThreads") &"</td></tr>"	
		end if
			
%>			
  
</table>
<br />

Och includerar jag denna koden så blir det helknas, men en massa felmeddelanden och så.

Medlem sedan feb. 200745 inlägg
#2

Någon som kan hjälpa mig?

Medlem sedan dec. 19996 721 inlägg
#3

För att veta säkert skulle vi behöva mer insikt i databasens utformning, så följande är bara ett höftskott.

Byt ut sökningen till:

SELECT DISTINCT TOP 10 t.* FROM aspbb_topics t,aspbb_posts p 
WHERE t.tforumid=6 AND t.tid=p.ptopicid
ORDER BY p.pdatecreate DESC
Medlem sedan feb. 200745 inlägg
#4

emission skrev:

För att veta säkert skulle vi behöva mer insikt i databasens utformning, så följande är bara ett höftskott.

Byt ut sökningen till:

SELECT DISTINCT TOP 10 t.* FROM aspbb_topics t,aspbb_posts p 
WHERE t.tforumid=6 AND t.tid=p.ptopicid
ORDER BY p.pdatecreate DESC

Får följande felmeddelande(n).

Microsoft VBScript compilation  error '800a0409'

Unterminated string constant

/news.asp, line 18

strSQL2 = "SELECT DISTINCT TOP 10 t.* FROM aspbb_topics t,aspbb_posts p
-----------------------------------------------------------------------

Då provade jag med all kod på en rad och fick detta meddelande.

Microsoft JET Database Engine error '80004005'

ORDER BY clause (p.pdatecreate) conflicts with DISTINCT.

/news.asp, line 19
Medlem sedan feb. 200745 inlägg
#5

Bifogar databasen

Medlem sedan dec. 20025 483 inlägg
#6

Testa

SELECT DISTINCT TOP 10 t.*, p.pdatecreate...
Medlem sedan feb. 200745 inlägg
#7

Om jag kör med denna så visas de senaste inläggen flera gånger efter varandra, visst det är ju bättre men det är inte perfekt.

SELECT DISTINCT TOP 10 t.*, p.pdatecreate FROM aspbb_topics t,aspbb_posts p WHERE t.tforumid=6 AND t.tid=p.ptopicid ORDER BY p.pdatecreate DESC
Medlem sedan feb. 200745 inlägg
#8

Exakt så här ser koden ut nu, och den listar tydligen varje t.tforumid=1 10gånger ?

dim strSQL2
strSQL2 = "SELECT DISTINCT TOP 10 t.*, p.pdatecreate FROM aspbb_topics t,aspbb_posts p WHERE t.tforumid=1 OR t.tforumid=2 OR t.tforumid=3 OR t.tforumid=4 OR t.tforumid=5 OR t.tforumid=6 OR t.tforumid=7 OR t.tforumid=8 OR t.tforumid=9 OR t.tforumid=10 OR t.tforumid=11 OR t.tforumid=12 AND t.tid=p.ptopicid ORDER BY p.pdatecreate DESC" 'Change tforumid=30 to the id of your newsthread
set objNews = objConn.Execute(strSQL2)
Medlem sedan dec. 20025 483 inlägg
#9
select distinct top 10
       t.*
  from aspbb_topics t
inner
  join aspbb_posts p
    on t.tid = p.ptopicid
 where t.tforumid = 6
   and p.pdatecreate
    in ( select distinct top 10
                pdatecreate
           from aspbb_posts
         order
             by pdatecreate desc )
Medlem sedan feb. 200745 inlägg
#10

Det blir inte riktigt rätt ändå, är det mina "OR" som ställer till det kanske?

dim strSQL2
strSQL2 = "select distinct top 10 t.* from aspbb_topics t inner join aspbb_posts p on t.tid = p.ptopicid where t.tforumid=1 OR t.tforumid=2 OR t.tforumid=3 OR t.tforumid=4 OR t.tforumid=5 OR t.tforumid=6 OR t.tforumid=7 OR t.tforumid=8 OR t.tforumid=9 OR t.tforumid=10 OR t.tforumid=11 OR t.tforumid=12 and p.pdatecreate in ( select distinct top 10 pdatecreate from aspbb_posts order by pdatecreate desc )" 'Change tforumid=30 to the id of your newsthread
set objNews = objConn.Execute(strSQL2)
Medlem sedan juni 200032 967 inlägg
#11

Carro_ skrev:

Det blir inte riktigt rätt ändå, är det mina "OR" som ställer till det kanske?

Mycket troligt. Antingen får du gruppera alla dina OR-villkor med parenteser, eller ersätt:

t.tforumid=1 OR t.tforumid=2 OR t.tforumid=3 OR t.tforumid=4 OR t.tforumid=5 OR t.tforumid=6 OR t.tforumid=7 OR t.tforumid=8 OR t.tforumid=9 OR t.tforumid=10 OR t.tforumid=11 OR t.tforumid=12

med

t.forumid IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

mvh

Medlem sedan feb. 200745 inlägg
#12

Om du menar så här

dim strSQL2
strSQL2 = "select distinct top 10 t.* from aspbb_topics t inner join aspbb_posts p on t.tid = p.ptopicid where t.forumid IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) and p.pdatecreate in ( select distinct top 10 pdatecreate from aspbb_posts order by pdatecreate desc )" 'Change tforumid=30 to the id of your newsthread
set objNews = objConn.Execute(strSQL2)

Så får jag detta felmeddelande

Microsoft JET Database Engine error '80040e10'

No value given for one or more required parameters.

/news.asp, line 19
Medlem sedan dec. 20025 483 inlägg
#13

t.tforumid

Medlem sedan juni 200032 967 inlägg
#14

Du måste alltid se till att korrekturläsa våra inlägg. :)

mvh

Medlem sedan feb. 200745 inlägg
#15

Peter S skrev:

t.tforumid

Som sagt, jag är nybörjare :r :e

Nu verkar det fungera bättre, dock är det två problem nu.

1. Den listar den senaste tråden längst ner och inte längst upp som det ska vara. Kan man vända på det på något vis?

2. De trådar som det inte finns något svar på visas inte överhuvudtaget (dock inte ett jätte problem, men om det finns någon enkel lösning så skulle det uppskattas)

Medlem sedan dec. 20025 483 inlägg
#16

Ännu ett förslag:

select t.tid
     , t.ttitle
     , t.tdatecreate
     , t.tbody
  from aspbb_topics t
 left
  join aspbb_posts p
    on t.tid = p.ptopicid
 where t.tforumid in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
group
    by t.tid
     , t.ttitle
     , t.tdatecreate
     , t.tbody
order
    by iif(p.ptopicid is null, t.tdatecreate, p.pdatecreate) desc
Medlem sedan feb. 200745 inlägg
#17

Jaha då får jag följande meddelande :OO

Microsoft JET Database Engine error '80040e21'

You tried to execute a query that does not include the specified expression 'IIf(p.ptopicid Is Null,t.tdatecreate,p.pdatecreate)' as part of an aggregate function.

/news.asp, line 19
dim strSQL2
strSQL2 = "select t.tid, t.ttitle, t.tdatecreate, t.tbody from aspbb_topics t left join aspbb_posts p on t.tid = p.ptopicid where t.tforumid in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) group by t.tid, t.ttitle, t.tdatecreate, t.tbody order by iif(p.ptopicid is null, t.tdatecreate, p.pdatecreate) desc"
set objNews = objConn.Execute(strSQL2)
Medlem sedan dec. 20025 483 inlägg
#18

Om vi lägger till lite saker i SELECT då:

select t.tid
     , t.ttitle
     , t.tdatecreate
     , t.tbody
     , iif(p.ptopicid is null, t.tdatecreate, p.pdatecreate) as d
  from aspbb_topics t
 left
  join aspbb_posts p
    on t.tid = p.ptopicid
 where t.tforumid in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
group
    by t.tid
     , t.ttitle
     , t.tdatecreate
     , t.tbody
order
    by iif(p.ptopicid is null, t.tdatecreate, p.pdatecreate) desc

Annars blir det till att tänka om :)

Medlem sedan feb. 200745 inlägg
#19

Peter S skrev:

Annars blir det till att tänka om :)

Ja det blir nog så ;)

Kör jag:

dim strSQL2
strSQL2 = "select t.tid, t.ttitle, t.tdatecreate, t.tbody, iif(p.ptopicid is null, t.tdatecreate, p.pdatecreate) as d from aspbb_topics t left join aspbb_posts p on t.tid = p.ptopicid where t.tforumid in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) group by t.tid , t.ttitle , t.tdatecreate , t.tbody order by iif(p.ptopicid is null, t.tdatecreate, p.pdatecreate) desc"
set objNews = objConn.Execute(strSQL2)

Så får jag detta till svar

Microsoft JET Database Engine error '80040e21'

You tried to execute a query that does not include the specified expression 'IIf(p.ptopicid Is Null,t.tdatecreate,p.pdatecreate)' as part of an aggregate function.

/news.asp, line 19

Måste bara få säga att det uppskattas väldigt mycket att ni försöker hjälpa mig. (y)

Medlem sedan dec. 20025 483 inlägg
#20
select distinct top 10
       t.tid
     , t.ttitle
     , t.tbody
     , iif(p.ptopicid is null, t.tdatecreate, p.pdatecreate) as d
  from aspbb_topics t
 left
  join aspbb_posts p
    on t.tid = p.ptopicid
 where t.tforumid in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
order
    by 4 desc

:e

270 ms totalt · 4 externa anrop · v20260731065814-full.a51de22e
125 ms — deklarationer (db)
0 ms — hämta statistik (cache)
141 ms — hämta tråd, inlägg och bilagor (db)
126 ms — ändringar (db)