14. september 2005 - 13:29Der er
26 kommentarer og 1 løsning
vis 16 poster af gangen via array
Hejsa eksperter,
Jeg har nogle værdier som bliver smidt ud i en arrow og bliver gentaget 16 gange. Dette virker helt perfekt ved første visning, men når jeg klikker videre og skal have vist de næste 16 værdier kommer den med fejlen! ############################################# Microsoft VBScript runtime error '800a0009' Subscript out of range: '[number: 2]' #############################################
Jeg tester med 17 poster ud af som sagt 16, hvis jeg opretter en post mere hedder fejlen:Subscript out of range: '[number: 3]'
Jeg er desværrre helt ny i arrays, så jeg ved ikke helt hvor jeg skal starte: så jeg smider lige hele koder.. Lidt lang med meget gentages!!
##############################################
Dim projekter, Com, Rec Set projekter = Server.CreateObject("ADODB.Connection") Set Com = Server.CreateObject("ADODB.Command") Set Rec = Server.CreateObject("ADODB.Recordset")
Dim intPageCount, intPageNumber, i intPageNumber = cint(request.QueryString("page")) ' eller hvilken side du vil have vist if request.QueryString("page") = "" then intPageNumber = 1 end if intPageCount = Rec.PageCount Totalrecords = Rec.RecordCount
for i = 1 to intPageCount if i = intPageNumber then Response.Write("" & i & "") else Response.Write(" <a href='projekter.asp?PID="&request.QueryString("PID")&"&page="&i&"'>" & i & "</a> ") end if next
' what pictures to include ' ############## LOOP START #############################################
ReDim pic(0), picon(0), picid(0)
if not Rec.BOF then Rec.AbsolutePage = intPageNumber do while Rec.AbsolutePage = intPageNumber ReDim preserve pic(ubound(pic)+1) ReDim preserve picon(ubound(picon)+1) ReDim preserve picid(ubound(picid)+1)
Set projekter = Nothing Set Rec = Nothing Set Com = Nothing
if thismanytotal < 1 then pic1 = "bg1.gif" picon1 = "bg1.gif" link1 = "#" else if pic(1) <> "" then pic1 = pic(1) else pic1 = "billedemangler.gif" end if if picon(1) <> "" then picon1 = picon(1) else picon1 = "billedemangleron.gif" end if link1 = "uniktprojekt.asp?pid=" & picid(1) end if
if thismanytotal < 2 then pic2 = "bg2.gif" picon2 = "bg2.gif" link2 = "#" else if pic(2) <> "" then pic2 = pic(2) else pic2 = "billedemangler.gif" end if if picon(2) <> "" then picon2 = picon(2) else picon2 = "billedemangleron.gif" end if link2 = "uniktprojekt.asp?pid=" & picid(2) end if
if thismanytotal < 3 then pic3 = "bg3.gif" picon3 = "bg3.gif" link3 = "#" else if pic(3) <> "" then pic3 = pic(3) else pic3 = "billedemangler.gif" end if if picon(3) <> "" then picon3 = picon(3) else picon3 = "billedemangleron.gif" end if link3 = "uniktprojekt.asp?pid=" & picid(3) end if
if thismanytotal < 4 then pic4 = "bg4.gif" picon4 = "bg4.gif" link4 = "#" else if pic(4) <> "" then pic4 = pic(4) else pic4 = "billedemangler.gif" end if if picon(4) <> "" then picon4 = picon(4) else picon4 = "billedemangleron.gif" end if link4 = "uniktprojekt.asp?pid=" & picid(4) end if
if thismanytotal < 5 then pic5 = "bg1.gif" picon5 = "bg1.gif" link5 = "#" else if pic(5) <> "" then pic5 = pic(5) else pic5 = "billedemangler.gif" end if if picon(5) <> "" then picon5 = picon(5) else picon5 = "billedemangleron.gif" end if link5 = "uniktprojekt.asp?pid=" & picid(5) end if
if thismanytotal < 6 then pic6 = "bg2.gif" picon6 = "bg2.gif" link6 = "#" else if pic(6) <> "" then pic6 = pic(6) else pic6 = "billedemangler.gif" end if if picon(6) <> "" then picon6 = picon(6) else picon6 = "billedemangleron.gif" end if link6 = "uniktprojekt.asp?pid=" & picid(6) end if
if thismanytotal < 7 then pic7 = "bg3.gif" picon7 = "bg3.gif" link7 = "#" else if pic(7) <> "" then pic7 = pic(7) else pic7 = "billedemangler.gif" end if if picon(7) <> "" then picon7 = picon(7) else picon7 = "billedemangleron.gif" end if link7 = "uniktprojekt.asp?pid=" & picid(7) end if
if thismanytotal < 8 then pic8 = "bg4.gif" picon8 = "bg4.gif" link8 = "#" else if pic(8) <> "" then pic8 = pic(8) else pic8 = "billedemangler.gif" end if if picon(8) <> "" then picon8 = picon(8) else picon8 = "billedemangleron.gif" end if link8 = "uniktprojekt.asp?pid=" & picid(8) end if
if thismanytotal < 9 then pic9 = "bg1.gif" picon9 = "bg1.gif" link9 = "#" else if pic(9) <> "" then pic9 = pic(9) else pic9 = "billedemangler.gif" end if if picon(9) <> "" then picon9 = picon(9) else picon9 = "billedemangleron.gif" end if link9 = "uniktprojekt.asp?pid=" & picid(9) end if
if thismanytotal < 10 then pic10 = "bg2.gif" picon10 = "bg2.gif" link10 = "#" else if pic(10) <> "" then pic10 = pic(10) else pic10 = "billedemangler.gif" end if if picon(10) <> "" then picon10 = picon(10) else picon10 = "billedemangleron.gif" end if link10 = "uniktprojekt.asp?pid=" & picid(10) end if
if thismanytotal < 11 then pic11 = "bg3.gif" picon11 = "bg3.gif" link11 = "#" else if pic(11) <> "" then pic11 = pic(11) else pic11 = "billedemangler.gif" end if if picon(11) <> "" then picon11 = picon(11) else picon11 = "billedemangleron.gif" end if link11 = "uniktprojekt.asp?pid=" & picid(11) end if
if thismanytotal < 12 then pic12 = "bg4.gif" picon12 = "bg4.gif" link12 = "#" else if pic(12) <> "" then pic12 = pic(12) else pic12 = "billedemangler.gif" end if if picon(12) <> "" then picon12 = picon(12) else picon12 = "billedemangleron.gif" end if link12 = "uniktprojekt.asp?pid=" & picid(12) end if
if thismanytotal < 13 then pic13 = "bg1.gif" picon13 = "bg1.gif" link13 = "#" else if pic(13) <> "" then pic13 = pic(13) else pic13 = "billedemangler.gif" end if if picon(13) <> "" then picon13 = picon(13) else picon13 = "billedemangleron.gif" end if link13 = "uniktprojekt.asp?pid=" & picid(13) end if
if thismanytotal < 14 then pic14 = "bg2.gif" picon14 = "bg2.gif" link14 = "#" else if pic(14) <> "" then pic14 = pic(14) else pic14 = "billedemangler.gif" end if if picon(14) <> "" then picon14 = picon(14) else picon14 = "billedemangleron.gif" end if link14 = "uniktprojekt.asp?pid=" & picid(14) end if
if thismanytotal < 15 then pic15 = "bg3.gif" picon15 = "bg3.gif" link15 = "#" else if pic(15) <> "" then pic15 = pic(15) else pic15 = "billedemangler.gif" end if if picon(15) <> "" then picon15 = picon(15) else picon15 = "billedemangleron.gif" end if link15 = "uniktprojekt.asp?pid=" & picid(15) end if
if thismanytotal < 16 then pic16 = "bg4.gif" picon16 = "bg4.gif" link16 = "#" else if pic(16) <> "" then pic16 = pic(16) else pic16 = "billedemangler.gif" end if if picon(16) <> "" then picon16 = picon(16) else picon16 = "billedemangleron.gif" end if link16 = "uniktprojekt.asp?pid=" & picid(16) end if
Jeg tror det er fordi du checker grænser ifht. antallet af rækker i tabellen og ikke antallet af rækker på den aktuelle side. Derfor vil jeg mene at du skal udskifte thismanytotal med intPageCount...
tak for svaret og jeg tror det kan løse mit problem.
Dog har jeg desværre ikke en variable der fortæller mig hvor mange poster jeg har på den enkelte side. F.eks. hvis der var 18 totalt og der blev vist 16 på første side viser den ikke hvor mange der er på side 2??
Jeg har prøvet at finde en ligning som kan regne det ud for mig, men er desværre lidt tom for ideer:
16 = antal som vises per side intPageNumber = side man befinder sig på Totalrecords = poster i alt
Jeg var (og er egentlig stadig) af den opfattelse, at Rec.PageSize bliver opdateret når du først har åbnet dit recordset. Du burde derfor kunne aflæse Rec.PageSize for at finde ud af hvormange rækker der er på den aktuelle side. Dette kan du så bruge til at lave din paginering med...
Jeg kigger på denne side, når jeg skal finde ud af hvad jeg skal fortælle dig... måske du selv kan kigge på den :)
Umiddelbart kan jeg ikke se hvad du videre skal bruge alle disse variable til, det virker lidt som dobbeltarbejde du er ude i (både for dig og VBScript), så måske du kan fortælle hvad du vil opnå og der kan findes en bedre (mere effektiv) metode...
Ok tak for alt hjælpen, jeg må lige prøve at se om jeg kan lave det lidt enklere, hvis du vil have nogle points for indtil videre må du lige smide et svar. :D
Ok, jeg har 16 billeder som bliver vist på en side som hver har en værdi taget udfra en database som her:
ReDim pic(0), picon(0), picid(0)
if not Rec.BOF then Rec.AbsolutePage = intPageNumber do while Rec.AbsolutePage = intPageNumber ReDim preserve pic(ubound(pic)+1) ReDim preserve picon(ubound(picon)+1) ReDim preserve picid(ubound(picid)+1)
men problemer er så at hvis man f.eks. har 18 billeder/poster og skal have vist de næste 2 via det script du også sendte, kommer den med fejl scriptet ikke ved hvor mange poster der er på den pågældende side.
Således smider jeg de enkelte billeder ud (1-16):
if thismanytotal < 2 then pic2 = "bg2.gif" picon2 = "bg2.gif" link2 = "#" else if pic(2) <> "" then pic2 = pic(2) else pic2 = "billedemangler.gif" end if if picon(2) <> "" then picon2 = picon(2) else picon2 = "billedemangleron.gif" end if link2 = "uniktprojekt.asp?pid=" & picid(2) end if
Kan du prøve at vise et eksempel på noget HTML som dette skal resultere i (bare formatet af en eller to billeder/links)...? Evt. et link til en side hvor det kan ses i aktion.
Det er når man klikker sig til de næste poster den melder fejl, jeg tror (men er selvfølgelig ikke sikker) at den ikke kan finde ud af at nulstille array'ene til de nye værdier??
Jeg er nød til at bede dig poste noget HTML, så jeg kan se hvordan det ser ud når det rammer browseren (vis kilde i IE og copy/paste herind). Det behøves bare at være HTML-kode for et par af billederne. Også meget gerne et link til en side hvor det kan ses i aktion...
Desuden skal jeg lige høre om det stadig er subscript out of range du får, når den fejler?
if Totalrecords < 3 then pic3 = "bg3.gif" picon3 = "bg3.gif" link3 = "#" else if pic(3) <> "" then <- her sker fejlen **** pic3 = pic(3) else pic3 = "billedemangler.gif" end if if picon(3) <> "" then picon3 = picon(3) else picon3 = "billedemangleron.gif" end if link3 = "uniktprojekt.asp?pid=" & picid(3) end if
**** Hvis der er et billede (post) mere sker fejlen i pic(4)
Håber problemet er enkelt, jeg sætter stor pris på at du gider hjælpe:)
<% Dim projekter, Com, Rec Set projekter = Server.CreateObject("ADODB.Connection") Set Com = Server.CreateObject("ADODB.Command") Set Rec = Server.CreateObject("ADODB.Recordset")
Dim intPageCount, intPageNumber, i intPageNumber = cint(request.QueryString("page")) ' eller hvilken side du vil have vist if request.QueryString("page") = "" then intPageNumber = 1 end if intPageCount = Rec.PageCount Totalrecords = Rec.RecordCount
for i = 1 to intPageCount if i = intPageNumber then ' Response.Write("" & i & "") else ' Response.Write(" <a href='projekter.asp?PID="&request.QueryString("PID")&"&page="&i&"'>" & i & "</a> ") ' response.write "<br>(" & Totalrecords & ")" end if next 'response.write "intpagecount=" & intPageCount & "<br>" 'response.write "i=" & i response.write Rec.PageSize thismanytotal = rec.RecordCount startcounthere = 0
if thismanytotal <= 16 then thismanyonpage = thismanytotal else 'thismanyonpage = thismanytotal-(16* "divisionsresten") end if
thismanyonpage = ((intPageNumber-1)*howmanyshowing) 'response.write "<br>Total on page=" & thismanyonpage
' what pictures to include ' ############## LOOP START #############################################
ReDim pic(0), picon(0), picid(0)
if not Rec.BOF then Rec.AbsolutePage = intPageNumber do while Rec.AbsolutePage = intPageNumber ReDim preserve pic(ubound(pic)+1) ReDim preserve picon(ubound(picon)+1) ReDim preserve picid(ubound(picid)+1)
Set projekter = Nothing Set Rec = Nothing Set Com = Nothing
if Totalrecords < 1 then pic1 = "bg1.gif" picon1 = "bg1.gif" link1 = "#" else if pic(1) <> "" then pic1 = pic(1) else pic1 = "billedemangler.gif" end if if picon(1) <> "" then picon1 = picon(1) else picon1 = "billedemangleron.gif" end if link1 = "uniktprojekt.asp?pid=" & picid(1) end if
if Totalrecords < 2 then pic2 = "bg2.gif" picon2 = "bg2.gif" link2 = "#" else if pic(2) <> "" then pic2 = pic(2) else pic2 = "billedemangler.gif" end if if picon(2) <> "" then picon2 = picon(2) else picon2 = "billedemangleron.gif" end if link2 = "uniktprojekt.asp?pid=" & picid(2) end if
if Totalrecords < 3 then pic3 = "bg3.gif" picon3 = "bg3.gif" link3 = "#" else if pic(3) <> "" then pic3 = pic(3) else pic3 = "billedemangler.gif" end if if picon(3) <> "" then picon3 = picon(3) else picon3 = "billedemangleron.gif" end if link3 = "uniktprojekt.asp?pid=" & picid(3) end if
if Totalrecords < 4 then pic4 = "bg4.gif" picon4 = "bg4.gif" link4 = "#" else if pic(4) <> "" then pic4 = pic(4) else pic4 = "billedemangler.gif" end if if picon(4) <> "" then picon4 = picon(4) else picon4 = "billedemangleron.gif" end if link4 = "uniktprojekt.asp?pid=" & picid(4) end if
if Totalrecords < 5 then pic5 = "bg1.gif" picon5 = "bg1.gif" link5 = "#" else if pic(5) <> "" then pic5 = pic(5) else pic5 = "billedemangler.gif" end if if picon(5) <> "" then picon5 = picon(5) else picon5 = "billedemangleron.gif" end if link5 = "uniktprojekt.asp?pid=" & picid(5) end if
if Totalrecords < 6 then pic6 = "bg2.gif" picon6 = "bg2.gif" link6 = "#" else if pic(6) <> "" then pic6 = pic(6) else pic6 = "billedemangler.gif" end if if picon(6) <> "" then picon6 = picon(6) else picon6 = "billedemangleron.gif" end if link6 = "uniktprojekt.asp?pid=" & picid(6) end if
if Totalrecords < 7 then pic7 = "bg3.gif" picon7 = "bg3.gif" link7 = "#" else if pic(7) <> "" then pic7 = pic(7) else pic7 = "billedemangler.gif" end if if picon(7) <> "" then picon7 = picon(7) else picon7 = "billedemangleron.gif" end if link7 = "uniktprojekt.asp?pid=" & picid(7) end if
if Totalrecords < 8 then pic8 = "bg4.gif" picon8 = "bg4.gif" link8 = "#" else if pic(8) <> "" then pic8 = pic(8) else pic8 = "billedemangler.gif" end if if picon(8) <> "" then picon8 = picon(8) else picon8 = "billedemangleron.gif" end if link8 = "uniktprojekt.asp?pid=" & picid(8) end if
if Totalrecords < 9 then pic9 = "bg1.gif" picon9 = "bg1.gif" link9 = "#" else if pic(9) <> "" then pic9 = pic(9) else pic9 = "billedemangler.gif" end if if picon(9) <> "" then picon9 = picon(9) else picon9 = "billedemangleron.gif" end if link9 = "uniktprojekt.asp?pid=" & picid(9) end if
if Totalrecords < 10 then pic10 = "bg2.gif" picon10 = "bg2.gif" link10 = "#" else if pic(10) <> "" then pic10 = pic(10) else pic10 = "billedemangler.gif" end if if picon(10) <> "" then picon10 = picon(10) else picon10 = "billedemangleron.gif" end if link10 = "uniktprojekt.asp?pid=" & picid(10) end if
if Totalrecords < 11 then pic11 = "bg3.gif" picon11 = "bg3.gif" link11 = "#" else if pic(11) <> "" then pic11 = pic(11) else pic11 = "billedemangler.gif" end if if picon(11) <> "" then picon11 = picon(11) else picon11 = "billedemangleron.gif" end if link11 = "uniktprojekt.asp?pid=" & picid(11) end if
if Totalrecords < 12 then pic12 = "bg4.gif" picon12 = "bg4.gif" link12 = "#" else if pic(12) <> "" then pic12 = pic(12) else pic12 = "billedemangler.gif" end if if picon(12) <> "" then picon12 = picon(12) else picon12 = "billedemangleron.gif" end if link12 = "uniktprojekt.asp?pid=" & picid(12) end if
if Totalrecords < 13 then pic13 = "bg1.gif" picon13 = "bg1.gif" link13 = "#" else if pic(13) <> "" then pic13 = pic(13) else pic13 = "billedemangler.gif" end if if picon(13) <> "" then picon13 = picon(13) else picon13 = "billedemangleron.gif" end if link13 = "uniktprojekt.asp?pid=" & picid(13) end if
if Totalrecords < 14 then pic14 = "bg2.gif" picon14 = "bg2.gif" link14 = "#" else if pic(14) <> "" then pic14 = pic(14) else pic14 = "billedemangler.gif" end if if picon(14) <> "" then picon14 = picon(14) else picon14 = "billedemangleron.gif" end if link14 = "uniktprojekt.asp?pid=" & picid(14) end if
if Totalrecords < 15 then pic15 = "bg3.gif" picon15 = "bg3.gif" link15 = "#" else if pic(15) <> "" then pic15 = pic(15) else pic15 = "billedemangler.gif" end if if picon(15) <> "" then picon15 = picon(15) else picon15 = "billedemangleron.gif" end if link15 = "uniktprojekt.asp?pid=" & picid(15) end if
if Totalrecords < 16 then pic16 = "bg4.gif" picon16 = "bg4.gif" link16 = "#" else if pic(16) <> "" then pic16 = pic(16) else pic16 = "billedemangler.gif" end if if picon(16) <> "" then picon16 = picon(16) else picon16 = "billedemangleron.gif" end if link16 = "uniktprojekt.asp?pid=" & picid(16) end if %>
Jeg har lige fedetet lidt med din kode og ændret den iflg. det link jeg gav dig i en tidligere kommentar i denne tråd. Jeg er ikke sikker på hvorfor du gør som du gør, når du opbygger din side, men det virker umiddelbart lidt omstændigt, men hey! - det skal jeg jo ikke blande mig i.
Koden jeg er kommet frem til ser ud som følger:
<% Dim Rec Set Rec = Server.CreateObject("ADODB.Recordset")
SQL = "SELECT * FROM projekter where Pnogle = " & request.QueryString("PID") & " ORDER BY PID DESC"
Dim intPageCount, intRecCount, intPageNumber, i, howmanyshowing
if request.QueryString("page") = "" then intPageNumber = 1 else intPageNumber = cint(request.QueryString("page")) end if
If Not (Rec.BOF Or Rec.EOF) Then Rec.PageSize = howmanyshowing Rec.AbsolutePage = intPageNumber intRecCount = Rec.PageSize intPageCount = Rec.PageCount Do While Not rs.EOF And intRecCount > 0 if intRecCount <> Rec.PageSize then ' Der skal kun udvides hvis det første element er blevet fyldt ReDim preserve pic(ubound(pic)+1) ReDim preserve picon(ubound(picon)+1) ReDim preserve picid(ubound(picid)+1) end if
if Totalrecords < 1 then pic1 = "bg1.gif" picon1 = "bg1.gif" link1 = "#" else pic1 = pic(1) picon1 = picon(1) link1 = "uniktprojekt.asp?pid=" & picid(1) end if
if Totalrecords < 2 then pic2 = "bg2.gif" picon2 = "bg2.gif" link2 = "#" else pic2 = pic(2) picon2 = picon(2) link2 = "uniktprojekt.asp?pid=" & picid(2) end if
' TODO: Du skal blot gentage efter ovenstående recept 14 gange mere - ' hvis altså du insisterer på at gøre det på denne måde... %>
BEMÆRK at jeg har lagt logikken omkring det at sætte default-billeder op i løkken der trækker data ud af databasen, da det er super egenet til at ligge i en løkke. Det betyder også at dine 16 if-sætninger slankes lidt (som du kan se).
Hej tak for forslaget, jeg har nu omskrevet mit script til dit!
Jeg får dog følgende fejl på nedenstående kode??
If Not (Rec.BOF Or Rec.EOF) Then Rec.PageSize = howmanyshowing Rec.AbsolutePage = intPageNumber intRecCount = Rec.PageSize intPageCount = Rec.PageCount Do While Not rec.EOF And intRecCount > 0 if intRecCount <> Rec.PageSize then ' Der skal kun udvides hvis det første element er blevet fyldt ReDim preserve pic(ubound(pic)+1) ReDim preserve picon(ubound(picon)+1) ReDim preserve picid(ubound(picid)+1) end if
if pic(ubound(pic)) <> "" then pic(ubound(pic)) = "billedemangler.gif" end if if picon(ubound(picon)) <> "" then picon(ubound(picon)) = "billedemangleron.gif" end if
Der er åbenbart ingen grænser for hvor tåbelig man kan blive...!?
Defaultbillederne skal naturligvis kun sættes hvis der IKKE er noget billede i databasen:
if pic(ubound(pic)) = "" then pic(ubound(pic)) = "billedemangler.gif" end if if picon(ubound(picon)) = "" then picon(ubound(picon)) = "billedemangleron.gif" end if
Ok nu viser den siden, men 1 er at den kun viser de første 2 billeder?? og når man skal have side 2 vist kommer den stadig med fejl???? håber ikke det er mig der er total tåbelig:O
If Not (Rec.BOF Or Rec.EOF) Then Rec.PageSize = howmanyshowing Rec.AbsolutePage = intPageNumber intRecCount = Rec.PageSize intPageCount = Rec.PageCount Do While Not rec.EOF And intRecCount > 0 if intRecCount <> Rec.PageSize then ' Der skal kun udvides hvis det første element er blevet fyldt ReDim preserve pic(ubound(pic)+1) ReDim preserve picon(ubound(picon)+1) ReDim preserve picid(ubound(picid)+1) end if
if pic(ubound(pic)) = "" then pic(ubound(pic)) = "billedemangler.gif" end if if picon(ubound(picon)) = "" then picon(ubound(picon)) = "billedemangleron.gif" end if
intRecCount = intRecCount - 1 Rec.MoveNext Loop End If
Totalrecords = Rec.RecordCount
Rec.Close set Rec = nothing
thismanytotal = Totalrecords startcounthere = 0
if thismanytotal <= 16 then thismanyonpage = thismanytotal end if
Du skal altså huske at ændre navnene på dine variabler fra if-sætning til if-sætning... som det er nu tildeler du alle de sidste 14 sæt billeder, ikoner og links til pic2, picon2 og link2 - det går jo ikke.
Det var det jeg forsøgte at forklare i min forrige post (og som åbenbart ikke lykkedes ;D)...
Desuden skal du nok ændre den linier hvor der står:
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.