If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > Loop, Array or something else?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-10-07, 11:20
full monty full monty is offline
Registered User
 
Join Date: Jan 2004
Posts: 22
Loop, Array or something else?

I have a small database which holds my FAQ's answers and questions. I want to be able to display the answers and questions on my FAQ's web-page but not in the same order they appear in the database. I will also probably add to the database as time goes on.


The database is created thus:



CREATE TABLE [FAQS] (
[Field] [smallint] IDENTITY (1, 1) NOT NULL ,
[Question] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Answer] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
PRIMARY KEY CLUSTERED
(
[Field]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO


The Answer field will often contain HTML.


I am using the following SQL statement:


SELECT Field, Question, Answer FROM FAQS order by Field ASC


and then using Response.Write to get the value from the database - but of course it only writes values from the top record.

For example, I want to be able to place the Answer from record 3 in one place on the HTML and another Answer from another record somewhere else.

How do I write values of rows that I specify?


Should I be using a loop, an array or something else?


Cheers,
Alski
Reply With Quote
  #2 (permalink)  
Old 10-10-07, 11:28
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 10-10-07, 11:36
full monty full monty is offline
Registered User
 
Join Date: Jan 2004
Posts: 22
Would that be the GetRows method?
Reply With Quote
  #4 (permalink)  
Old 10-10-07, 11:44
full monty full monty is offline
Registered User
 
Join Date: Jan 2004
Posts: 22
Got it

<%
'The first number indicates how many records to copy
'The second number indicates what recordnumber to start on
p=Recordset1.GetRows(3,0)



response.write("<p>This example returns the value of the first three columns in the second record:</p>")
response.write(p(0,1))
response.write("<br>")
response.write(p(1,1))
response.write("<br>")
response.write(p(2,1))
%>


I'll use this format to now write the FAQ's page, placing questions, answers and anchors.

Thank you!
Reply With Quote
  #5 (permalink)  
Old 10-10-07, 14:43
full monty full monty is offline
Registered User
 
Join Date: Jan 2004
Posts: 22
OK - I have tried to get get around writing response.write(p(0,1)) and variants of as it's very easy to make a mistake.

So I tried a loop so that I could write the values to a string e.g. strField1, strQuestion1, strAnswer1, strField2, strQuestion2, strAnswer2 etc.


noofrecords = 47
p=Recordset1.GetRows(noofrecords,0)


Dim i
For i=1 to noofrecords

strField = "strField" & i
strField = (p(0,(i - 1)))


strQuestion = "strQuestion" & i
strQuestion = (p(1,(i - 1)))


strAnswer = "strAnswer" & i
strAnswer = (p(2,(i - 1)))
Next

but when I use response.write(strQuestion5), I get zip, and when I write response.write(strQuestion) I get all of them.

What I want to do is use response.write(strQuestion5) and only have the Question from the 5th record.

I think I'm almost there but can't quite get it to work...
Reply With Quote
  #6 (permalink)  
Old 10-11-07, 03:51
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Code:
strQuestion = "strQuestion" & i
strQuestion = (p(1,(i - 1)))
What this code does is assign the value of "strQuestion5" to the variable strQuestion. It then overwrites it with the answer. Therefore the variable strQuestion doesnt exist - hence the NULL result.

How do you want your results displayed?
If I can understand a bit better what you want to do with your answers then I'll try and help
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 10-11-07, 08:13
full monty full monty is offline
Registered User
 
Join Date: Jan 2004
Posts: 22
I re-wrote that bit out in long-hand:

strField1 = (p(0,0))
strField2 = (p(0,1)) etc.

strQuestion1 = (p(1,0))
strQuestion2 = (p(1,1)) etc.

strAnswer1 = (p(2,0))
strAnswer2 = (p(2,1)) etc.

Essentially so I could just have a Response.Write(strAnswer2) or whatever where I needed it within the HTML. strAnswer2 is so much easier to use within the HTML than (p(2,1))

It would have been a neater solution to have the For...Next Loop working but if not my long-hand version works OK.
Reply With Quote
  #8 (permalink)  
Old 10-11-07, 09:15
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
You do have the option of a loop within a loop...
Code:
noofrecords = 47
p=Recordset1.GetRows(noofrecords,0)

Dim i
Dim j

For i = 0 to NumRecs - 1
  For j = 1 to NumQs
    Response.Write p(j,i)
  Next j
Next i
__________________
George
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On