Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2004
    Posts
    22

    Unanswered: 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

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10

  3. #3
    Join Date
    Jan 2004
    Posts
    22
    Would that be the GetRows method?

  4. #4
    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!

  5. #5
    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...

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    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
    Home | Blog

  7. #7
    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.

  8. #8
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    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
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •