arrays and recordsets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart

    #1

    arrays and recordsets

    Hi there,

    I am trying to populate the array "yourImages " in the following code,

    except i am trying to fill it with the values from an access database.
    I already publish info from the database to an asp page, but i would like to
    preload the images.

    I open the database successfully like this
    ---------------
    <%
    If IsObject(Sessio n("Fragrance_co nn")) Then
    Set conn = Session("Fragra nce_conn")
    Else
    Set conn = Server.CreateOb ject("ADODB.Con nection")
    conn.open "Fragrance","", ""
    Set Session("Fragra nce_conn") = conn
    End If
    %>
    <%
    If IsObject(Sessio n("retail_rs" )) Then
    Set rs = Session("retail _rs")
    Else
    sql = "SELECT * FROM [retail]"
    Set rs = Server.CreateOb ject("ADODB.Rec ordset")
    rs.Open sql, conn, 3, 3
    If rs.eof Then
    rs.AddNew
    End If
    Set Session("retail _rs") = rs
    End If"
    ---------------------


    The record i want to use is
    rs.Fields("Imag es").Value

    Could anyone please help me out?



    Thanks



  • Tim Williams

    #2
    Re: arrays and recordsets

    Stuart,

    It's considered bad practise to store connection or recordset objects
    objects in session variables (search relevant FAQ's for the reason).
    It's much better to create connections as needed and to store data in
    arrays rather than a rs.

    Anyway, once you have your recordset then just loop through it and add
    the items:

    strImgBase = "http://www.mysite.com/images/"
    strArr = ""

    if not rs.eof then
    do while not rs.eof

    strArray = strArray & """" & _
    strImgBase & rs("Images").va lue & ""","

    rs.movenext
    loop
    end if

    strArray=left(s trArray,len(str Array)-1)


    //then in the js preload script
    var yourImages = new Array(<%=strArr ay%>) // Fill this array with the
    images you wish to preload

    stuff strArray in a session (or application variable) to cache it for
    the next use.


    Tim.


    "Stuart" <stuart@nexusne t.co.nz> wrote in message
    news:bti22r$apn $3@lust.ihug.co .nz...[color=blue]
    > Hi there,
    >
    > I am trying to populate the array "yourImages " in the following[/color]
    code,[color=blue]
    > http://www.javascriptkit.com/script/...adimage2.shtml
    > except i am trying to fill it with the values from an access[/color]
    database.[color=blue]
    > I already publish info from the database to an asp page, but i would[/color]
    like to[color=blue]
    > preload the images.
    >
    > I open the database successfully like this
    > ---------------
    > <%
    > If IsObject(Sessio n("Fragrance_co nn")) Then
    > Set conn = Session("Fragra nce_conn")
    > Else
    > Set conn = Server.CreateOb ject("ADODB.Con nection")
    > conn.open "Fragrance","", ""
    > Set Session("Fragra nce_conn") = conn
    > End If
    > %>
    > <%
    > If IsObject(Sessio n("retail_rs" )) Then
    > Set rs = Session("retail _rs")
    > Else
    > sql = "SELECT * FROM [retail]"
    > Set rs = Server.CreateOb ject("ADODB.Rec ordset")
    > rs.Open sql, conn, 3, 3
    > If rs.eof Then
    > rs.AddNew
    > End If
    > Set Session("retail _rs") = rs
    > End If"
    > ---------------------
    >
    >
    > The record i want to use is
    > rs.Fields("Imag es").Value
    >
    > Could anyone please help me out?
    >
    >
    >
    > Thanks
    >
    >
    >[/color]


    Comment

    • Brynn

      #3
      Re: arrays and recordsets

      I agree ... check out my DBConn.asp script at the following ...



      this gets you in and out of your database incredibly fast using the
      getrows method ... much faster than looping through a recordset at
      all. This way you wil build the string after your recordset and
      database connection is closed.
      GetRows will give you a 2-dimensional array of your recordset ...
      first dimension columns, second dimension rows. Yours just being 1
      column will be like the below loop. I would be sure to put Select
      Images From [retail]; instead of * ... if you don't need the other
      info.

      I recommend putting your connection string in an application variable
      via the global.asa ... or just set it on the page like so ...

      I like the way Tim got rid of last comma .. good idea Tim ... versus
      if .. then in the loop

      <!-- #include file="/coolpier_script s/_database_tools/DBConn.asp" -->

      <%
      Dim yourSqlStatemen t, rsArray, strArray: strArray=""
      yourSqlStatemen t = "SELECT Images FROM [retail];"

      cp_TheConnectio nString = "yourConnection String"
      cp_DBConn(("ope n")
      rsArray = cp_SqlArray(you rSqlStatement)
      cp_DBConn("clos e")

      strImgBase = "http://www.mysite.com/images/"
      For n = 0 to ubound(rsArray, 2)
      strArray = strArray & """" & strImgBase & rsArray(0,n) & ""","
      Next
      strArray=left(s trArray,len(str Array)-1) '//to get rid of last comma
      %>




      On Thu, 08 Jan 2004 05:35:58 GMT, "Tim Williams"
      <saxifraxREMOVE @THISpacbell.ne t> wrote:
      [color=blue]
      >Stuart,
      >
      >It's considered bad practise to store connection or recordset objects
      >objects in session variables (search relevant FAQ's for the reason).
      >It's much better to create connections as needed and to store data in
      >arrays rather than a rs.
      >
      >Anyway, once you have your recordset then just loop through it and add
      >the items:
      >
      >strImgBase = "http://www.mysite.com/images/"
      >strArr = ""
      >
      >if not rs.eof then
      >do while not rs.eof
      >
      >strArray = strArray & """" & _
      > strImgBase & rs("Images").va lue & ""","
      >
      >rs.movenext
      >loop
      >end if
      >
      >strArray=left( strArray,len(st rArray)-1)
      >
      >
      >//then in the js preload script
      >var yourImages = new Array(<%=strArr ay%>) // Fill this array with the
      >images you wish to preload
      >
      >stuff strArray in a session (or application variable) to cache it for
      >the next use.
      >
      >
      >Tim.
      >
      >
      >"Stuart" <stuart@nexusne t.co.nz> wrote in message
      >news:bti22r$ap n$3@lust.ihug.c o.nz...[color=green]
      >> Hi there,
      >>
      >> I am trying to populate the array "yourImages " in the following[/color]
      >code,[color=green]
      >> http://www.javascriptkit.com/script/...adimage2.shtml
      >> except i am trying to fill it with the values from an access[/color]
      >database.[color=green]
      >> I already publish info from the database to an asp page, but i would[/color]
      >like to[color=green]
      >> preload the images.
      >>
      >> I open the database successfully like this
      >> ---------------
      >> <%
      >> If IsObject(Sessio n("Fragrance_co nn")) Then
      >> Set conn = Session("Fragra nce_conn")
      >> Else
      >> Set conn = Server.CreateOb ject("ADODB.Con nection")
      >> conn.open "Fragrance","", ""
      >> Set Session("Fragra nce_conn") = conn
      >> End If
      >> %>
      >> <%
      >> If IsObject(Sessio n("retail_rs" )) Then
      >> Set rs = Session("retail _rs")
      >> Else
      >> sql = "SELECT * FROM [retail]"
      >> Set rs = Server.CreateOb ject("ADODB.Rec ordset")
      >> rs.Open sql, conn, 3, 3
      >> If rs.eof Then
      >> rs.AddNew
      >> End If
      >> Set Session("retail _rs") = rs
      >> End If"
      >> ---------------------
      >>
      >>
      >> The record i want to use is
      >> rs.Fields("Imag es").Value
      >>
      >> Could anyone please help me out?
      >>
      >>
      >>
      >> Thanks
      >>
      >>
      >>[/color]
      >
      >[/color]

      Comment

      • Stuart

        #4
        Re: arrays and recordsets

        I was playing woith someone elses script and i kind of got it working to an
        extent. The reason i am trying to preload *some* images is that there are
        4000 plus records that i will be displaying in a table, which includes a
        picture of each item. I would like to split it into pages displaying maybe
        50 items max, but i don't know how to do that yet. Here is the kind of
        working script i am playing with

        <html>
        <%
        If IsObject(Sessio n("Fragrance_co nn")) Then
        Set conn = Session("Fragra nce_conn")
        Else
        Set conn = Server.CreateOb ject("ADODB.Con nection")
        conn.open "Fragrance","", ""
        Set Session("Fragra nce_conn") = conn
        End If
        %>
        <%
        If IsObject(Sessio n("retailU_rs") ) Then
        Set rs = Session("retail U_rs")
        Else
        sql = "SELECT * FROM [retailU]"
        Set rs = Server.CreateOb ject("ADODB.Rec ordset")
        rs.Open sql, conn, 3, 3
        If rs.eof Then
        rs.AddNew
        End If
        Set Session("retail U_rs") = rs
        End If
        %>
        <head>
        <script language="JavaS cript1.2">
        <!-- begin hiding
        // (C) 2000 Marcin P Wojtowicz [one_spook@hotma il.com]. All rights reserved.
        // Obtain permission before selling/redistributing in any medium.

        startingColor = new Array() // <-- Do not modify!
        endingColor = new Array() // <-- Do not modify!

        // YOU MAY MODIFY THE FOLLOWING:
        var yourImages = new Array() // Fill this array with the images you wish to
        preload





        <%
        x = 0
        On Error Resume Next
        rs.MoveFirst
        do while Not rs.eof //x <= 151

        x = x + 1

        response.write( "yourImages[" & x & "]=" & "'" & rs("Images") & "';")

        rs.MoveNext
        loop
        %>
        document.write( yourImages.leng th);

        var locationAfterPr eload = "http://192.168.1.3/retail.asp" // The script
        will redirect here when the preloading finishes *successfully*
        var preloadbarWidth = 4000 // 151 The length of the preload bar. Should be
        greater than total amount of images you want to preload!
        var preloadbarHeigh t = 15 // The height of the gradient/preload bar
        var backgroundOfGra dient = "#000000" // Default color while the preload bar
        is "filling up"

        // Color the preloadbar is starting with - enter 1st, 3rd and 5th
        numbers/letters of color code
        startingColor[0] = "c"
        startingColor[1] = "f"
        startingColor[2] = "f"

        // Color the preloadbar is going to end up with - enter the 1st, 3rd and 5th
        numbers/letters of color code
        endingColor[0] = "c"
        endingColor[1] = "0"
        endingColor[2] = "0"

        // FOR TROUBLESHOOTING :
        var gap = 2 // PLAY AROUND WITH THIS SETTING IF YOU GET A JAVASCRIPT
        ERROR!!! 2 is the minumum value!!!


        // DO NOT MODIFY ANYTHING BEYOND THIS POINT!!!

        if (!document.all) location.replac e(locationAfter Preload)
        var a = 10, b = 11, c = 12, d = 13, e = 14, f=15, i, j, ones = new Array(),
        sixteens = new Array(), diff = new Array();
        var convert = new
        Array("0","1"," 2","3","4","5", "6","7","8","9" ,"a","b","c","d ","e","f"),
        imgLen = yourImages.leng th;
        var loaded = new Array(), preImages = new Array(), currCount = 0, pending =
        0, h = 0, hilite = new Array(), cover = new Array();

        var num = Math.floor(prel oadbarWidth/gap);
        for (i = 0; i < 3; i++) {
        startingColor[i] = startingColor[i].toLowerCase();
        endingColor[i] = endingColor[i].toLowerCase();
        startingColor[i] = eval(startingCo lor[i]);
        endingColor[i] = eval(endingColo r[i]);
        diff[i] = (endingColor[i]-startingColor[i])/num;
        ones[i] = Math.floor(diff[i]);
        sixteens[i] = Math.round((dif f[i] - ones[i])*15);
        }
        endingColor[0] = 0;
        endingColor[1] = 0;
        endingColor[2] = 0;
        i = 0, j = 0;
        while (i <= num) {
        hilite[i] = "#";
        while (j < 3) {
        hilite[i] += convert[startingColor[j]];
        hilite[i] += convert[endingColor[j]];
        startingColor[j] += ones[j];
        endingColor[j] += sixteens[j];
        if (endingColor[j] > 15) {
        endingColor[j] -= 15;
        startingColor[j]++;
        }
        j++;
        }
        j = 0;
        i++;
        }
        function loadImages() {
        for (i = 0; i < imgLen; i++) {
        preImages[i] = new Image();
        preImages[i].src = yourImages[i];
        loaded[i] = 0;
        cover[i] = Math.floor(num/imgLen)*(i+1)
        }
        cover[cover.length-1] += num%imgLen
        checkLoad();
        }
        function checkLoad() {
        if (pending) { changeto(); return }
        if (currCount == imgLen-1) { location.replac e(locationAfter Preload);
        return }
        for (i = 0; i < imgLen; i++) {
        if (!loaded[i] && preImages[i].complete) {
        loaded[i] = 1; pending++; currCount++;
        checkLoad();
        return;
        }
        }
        setTimeout("che ckLoad()",10);
        }
        function changeto() {
        if (h+1 > cover[currCount-1]) {
        var percent = Math.round(100/imgLen)*currCou nt;
        if (percent > 100) while (percent != 100) percent--;
        if (currCount == imgLen && percent < 100) percent = 100;
        defaultStatus = "Loaded " + currCount + " out of " + imgLen + " images ["
        + percent + "%].";
        pending--;
        checkLoad();
        return;
        }
        eval("document. all.cell" + (h+1) + ".style.backgro undColor = hilite[h]");;
        h++;
        setTimeout("cha ngeto()",1);
        }
        defaultStatus = "Loaded 0 out of " + imgLen + " images [0%]."
        // end hiding -->
        </script>
        </head>
        <body>
        <center>
        <font face="Verdana, Arial, Helvetica" size="2"><cente r>Preloading Images...
        Please Wait..</center>
        <script language="JavaS cript1.2">
        <!-- beging hiding
        document.write( '<table border="0" cellpadding="0" cellspacing="0" width="' +
        preloadbarWidth + '"><tr height="' + preloadbarHeigh t + '" bgcolor="' +
        backgroundOfGra dient + '">');
        for (i = 0; i < num; i++) {
        document.write( '<td width="' + gap + '" id="cell' + (i+1) + '"></td>');
        }
        document.write( '</tr></table>');
        document.write( '<p><small><a
        href="javascrip t:location.repl ace(locationAft erPreload)">Ski p Preloading</a>
        &nbsp;| &nbsp;<a
        href="http://javascriptkit.c om/script/script2/preloadimage2.s html">Script
        Credits</a></small></p></font>')
        loadImages();
        // end hiding -->
        </script>
        </center>
        </body>
        </html>




        "Brynn" <z@z.com> wrote in message
        news:3ffcf5e1.1 6791184@news.co mcast.giganews. com...[color=blue]
        > I agree ... check out my DBConn.asp script at the following ...
        >
        > http://www.coolpier.com/cp/cp_scripts/index.asp
        >
        > this gets you in and out of your database incredibly fast using the
        > getrows method ... much faster than looping through a recordset at
        > all. This way you wil build the string after your recordset and
        > database connection is closed.
        > GetRows will give you a 2-dimensional array of your recordset ...
        > first dimension columns, second dimension rows. Yours just being 1
        > column will be like the below loop. I would be sure to put Select
        > Images From [retail]; instead of * ... if you don't need the other
        > info.
        >
        > I recommend putting your connection string in an application variable
        > via the global.asa ... or just set it on the page like so ...
        >
        > I like the way Tim got rid of last comma .. good idea Tim ... versus
        > if .. then in the loop
        >
        > <!-- #include file="/coolpier_script s/_database_tools/DBConn.asp" -->
        >
        > <%
        > Dim yourSqlStatemen t, rsArray, strArray: strArray=""
        > yourSqlStatemen t = "SELECT Images FROM [retail];"
        >
        > cp_TheConnectio nString = "yourConnection String"
        > cp_DBConn(("ope n")
        > rsArray = cp_SqlArray(you rSqlStatement)
        > cp_DBConn("clos e")
        >
        > strImgBase = "http://www.mysite.com/images/"
        > For n = 0 to ubound(rsArray, 2)
        > strArray = strArray & """" & strImgBase & rsArray(0,n) & ""","
        > Next
        > strArray=left(s trArray,len(str Array)-1) '//to get rid of last comma
        > %>
        >
        >
        >
        >
        > On Thu, 08 Jan 2004 05:35:58 GMT, "Tim Williams"
        > <saxifraxREMOVE @THISpacbell.ne t> wrote:
        >[color=green]
        > >Stuart,
        > >
        > >It's considered bad practise to store connection or recordset objects
        > >objects in session variables (search relevant FAQ's for the reason).
        > >It's much better to create connections as needed and to store data in
        > >arrays rather than a rs.
        > >
        > >Anyway, once you have your recordset then just loop through it and add
        > >the items:
        > >
        > >strImgBase = "http://www.mysite.com/images/"
        > >strArr = ""
        > >
        > >if not rs.eof then
        > >do while not rs.eof
        > >
        > >strArray = strArray & """" & _
        > > strImgBase & rs("Images").va lue & ""","
        > >
        > >rs.movenext
        > >loop
        > >end if
        > >
        > >strArray=left( strArray,len(st rArray)-1)
        > >
        > >
        > >//then in the js preload script
        > >var yourImages = new Array(<%=strArr ay%>) // Fill this array with the
        > >images you wish to preload
        > >
        > >stuff strArray in a session (or application variable) to cache it for
        > >the next use.
        > >
        > >
        > >Tim.
        > >
        > >
        > >"Stuart" <stuart@nexusne t.co.nz> wrote in message
        > >news:bti22r$ap n$3@lust.ihug.c o.nz...[color=darkred]
        > >> Hi there,
        > >>
        > >> I am trying to populate the array "yourImages " in the following[/color]
        > >code,[color=darkred]
        > >> http://www.javascriptkit.com/script/...adimage2.shtml
        > >> except i am trying to fill it with the values from an access[/color]
        > >database.[color=darkred]
        > >> I already publish info from the database to an asp page, but i would[/color]
        > >like to[color=darkred]
        > >> preload the images.
        > >>
        > >> I open the database successfully like this
        > >> ---------------
        > >> <%
        > >> If IsObject(Sessio n("Fragrance_co nn")) Then
        > >> Set conn = Session("Fragra nce_conn")
        > >> Else
        > >> Set conn = Server.CreateOb ject("ADODB.Con nection")
        > >> conn.open "Fragrance","", ""
        > >> Set Session("Fragra nce_conn") = conn
        > >> End If
        > >> %>
        > >> <%
        > >> If IsObject(Sessio n("retail_rs" )) Then
        > >> Set rs = Session("retail _rs")
        > >> Else
        > >> sql = "SELECT * FROM [retail]"
        > >> Set rs = Server.CreateOb ject("ADODB.Rec ordset")
        > >> rs.Open sql, conn, 3, 3
        > >> If rs.eof Then
        > >> rs.AddNew
        > >> End If
        > >> Set Session("retail _rs") = rs
        > >> End If"
        > >> ---------------------
        > >>
        > >>
        > >> The record i want to use is
        > >> rs.Fields("Imag es").Value
        > >>
        > >> Could anyone please help me out?
        > >>
        > >>
        > >>
        > >> Thanks
        > >>
        > >>
        > >>[/color]
        > >
        > >[/color]
        >[/color]


        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: arrays and recordsets

          Stuart wrote:
          [color=blue]
          > I open the database successfully like this
          > ---------------
          > <%
          > If IsObject(Sessio n("Fragrance_co nn")) Then
          > Set conn = Session("Fragra nce_conn")
          > Else
          > Set conn = Server.CreateOb ject("ADODB.Con nection")
          > conn.open "Fragrance","", ""
          > Set Session("Fragra nce_conn") = conn
          > End If
          > %>[/color]

          What the heck has this to do with JavaScript?
          Go away, to a <jehova>VBScrip t</jehova> NG, please.


          PointedEars

          Comment

          Working...