Using arrays to Insert, Update, Retrieve from a Table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theblissfulwizard
    New Member
    • Feb 2008
    • 1

    Using arrays to Insert, Update, Retrieve from a Table

    FYI - New to ASP

    I'm trying to get multiple values from checkboxes inserted into a table using arrays. I'm using VBscript - ASP 1.1 (not 2.0) and here is what I want to do.

    I have a page that displays a list of news articles - includes Article_ID, Title, Date. I have a checkbox next to each Title that automatically gets assigned a name of "CB0" - "CB1" and so on, the value of each checkbox includes the Article_ID number (unique to each article).

    When a user selects multiple checkboxes and clicks Submit on the form I want the Article_ID to be collected and stored into another Table as a string value like "12,14,15,16,23 ". I also need to be able to retrieve these Article_ID numbers on another page to display back to the user which articles they have selected. So I need to be able to reparse the string without commas.

    I have found many posts about arrays but not in ASP code, so any help would be appreciated.
  • markrawlingson
    Recognized Expert Contributor
    • Aug 2007
    • 346

    #2
    Well, something tells me you're using .NET, not classic ASP - as per your "I'm using ASP 1.1, not 2.0 - But here's some classic ASP for you anyway. I'm sure it can be easily converted to .NET if that's the case.

    I would try something like the below...

    [CODE=ASP]
    'First, the push to the database.
    If Request.Form("C B0") <> Empty Then
    sTemp = sTemp & Request.Form("C B0") & " "
    End If
    If Request.Form("C B1") <> Empty Then
    sTemp = sTemp & Request.Form("C B1") & " "
    End If
    If Request.Form("C B2") <> Empty Then
    sTemp = sTemp & Request.Form("C B2") & " "
    End If
    '...etc, etc. You'll end up with a string of only the selected checkboxes values, split by a space. You'll probably end up with a useless space at the end, so we'll trim that off. We'll also need to put the commas in place.
    sTemp = Replace( Trim( sTemp ), " ", "," )
    'Now push the info into your DB.
    oRs("sSelection s") = sTemp

    'Second, to pull the data out of the db and manipulate...
    sSQL = "SELECT sSelections FROM tblTable WHERE User = '" & User & "';"
    Set oRs = Server.CreateOb ject("ADODB.Rec ordSet")
    oRs.Open sSQL, Connection, adOpenReadOnly, adLockOptimisti c, adCmdText
    If oRs.EOF = False Then
    sTemp = oRs("sSelection s")
    End If
    oRs.Close
    Set oRs = Nothing
    aTemp = Split(sTemp, ",") 'Will split the selections by the comma
    If IsArray(aTemp) Then
    For i = 0 To UBound(aTemp)
    Response.Write aTemp(i)
    Next
    Else
    Response.Write aTemp
    End If
    [/CODE]

    Then you can use aTemp(i) and push it into the database to find the other information for the article, such as its title - to display information back to the user that they can actually understand.

    OR.. you can just do it the easy way and create a new record in a sub-table for each article the user chooses. Then you can simply pull the information out using GetRows() in one recordset (using a join, much more efficient).

    Sincerely,
    Mark

    Comment

    • CroCrew
      Recognized Expert Contributor
      • Jan 2008
      • 564

      #3
      Hello theblissfulwiza rd,

      I did not know what type of database you are using so this example is using Microsoft Access.

      PageOne.asp
      [code=asp]
      <%
      Set Conn = Server.CreateOb ject("ADODB.Con nection")
      Conn.Open "DRIVER={Micros oft Access Driver (*.mdb)}; DBQ=" & Server.MapPath( "YourDatabase.m db")
      Set rsList = Server.CreateOb ject("ADODB.Rec ordset")
      SQL = "SELECT Article_ID, Title FROM TheTable"
      rsList.CursorTy pe = 1
      rsList.LockType = 3
      rsList.Open SQL, Conn
      %>
      <html>
      <head>
      <title>Page One</title>
      <script language="JavaS cript">
      function Validate()
      {
      var TempArticlesArr ay = "";

      for(var i = 0; i < <%=rsList.Recor dCount%>; i++)
      {
      if (document.getEl ementsByName('C B' + i).item(0).chec ked)
      {
      TempArticlesArr ay = TempArticlesArr ay + document.getEle mentsByName('CB ' + i).item(0).valu e;
      }
      if (!(i==<%=rsList .RecordCount%>) )
      {
      TempArticlesArr ay = TempArticlesArr ay + ", ";
      }
      }

      document.xForm. ArticlesArray.v alue=TempArticl esArray;
      }
      </script>
      </head>
      <body>
      <form method="post" action="PageTwo .asp" name="xForm" id="xForm">
      <input type="hidden" name="ArticlesA rray" value="">
      <%i = 0%>
      <%Do Until (rsList.EOF)%>
      <input type="checkbox" name="CB<%=i%>" value="<%=rsLis t("Article_ID") .value%>"> <%=rsList("Titl e").value%>< br>
      <%i = (i + 1)%>
      <%rsList.MoveNe xt%>
      <%Loop%>
      <input type="submit" value="Submit" class="button" onClick="Valida te();">
      </form>
      </body>
      </html>
      [/code]

      PageTwo.asp
      [code=asp]
      <%
      Set Conn = Server.CreateOb ject("ADODB.Con nection")
      Conn.Open "DRIVER={Micros oft Access Driver (*.mdb)}; DBQ=" & Server.MapPath( "YourDatabase.m db")

      Function GetArticleTitle (Article_ID)
      Set rsArticleTitle = Server.CreateOb ject("ADODB.Rec ordset")
      SQL = "SELECT Title FROM TheTable WHERE Article_ID = '" & Article_ID & "'"
      rsArticleTitle. CursorType = 1
      rsArticleTitle. LockType = 3
      rsArticleTitle. Open SQL, Conn

      GetArticleTitle = rsArticleTitle( "Title").va lue
      End Function

      ArticlesArray = Split(Request.F orm("ArticlesAr ray"), ",")
      %>
      <html>
      <head>
      <title>Page Two</title>
      </head>
      <body>
      <table>
      <tr>
      <td>Location In Array</td>
      <td>&nbsp;</td>
      <td>Value of Location</td>
      <td>&nbsp;</td>
      <td>Title from Database</td>
      </tr>
      <%For i = 0 To (UBound(Article sArray)-1)%>
      <tr>
      <td><%Response. Write(i)%></td>
      <td>&nbsp;</td>
      <td><%Response. Write(ArticlesA rray(i))%></td>
      <td>&nbsp;</td>
      <td><%Response. Write(GetArticl eTitle(Articles Array(i)))%></td>
      </tr>
      <%Next%>
      </table>
      </body>
      </html>
      [/code]

      Comment

      Working...