arrays and recordsets

Discussion in 'Javascript' started by Stuart, Jan 7, 2004.

  1. Stuart

    Stuart Guest

    Hi there,

    I am trying to populate the array "yourImages" in the following code,
    http://www.javascriptkit.com/script/script2/preloadimage2.shtml
    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(Session("Fragrance_conn")) Then
    Set conn = Session("Fragrance_conn")
    Else
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.open "Fragrance","",""
    Set Session("Fragrance_conn") = conn
    End If
    %>
    <%
    If IsObject(Session("retail_rs")) Then
    Set rs = Session("retail_rs")
    Else
    sql = "SELECT * FROM [retail]"
    Set rs = Server.CreateObject("ADODB.Recordset")
    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("Images").Value

    Could anyone please help me out?



    Thanks
     
    Stuart, Jan 7, 2004
    #1
    1. Advertising

  2. Stuart

    Tim Williams Guest

    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").value & ""","

    rs.movenext
    loop
    end if

    strArray=left(strArray,len(strArray)-1)


    //then in the js preload script
    var yourImages = new Array(<%=strArray%>) // 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" <> wrote in message
    news:bti22r$apn$...
    > Hi there,
    >
    > I am trying to populate the array "yourImages" in the following

    code,
    > http://www.javascriptkit.com/script/script2/preloadimage2.shtml
    > 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(Session("Fragrance_conn")) Then
    > Set conn = Session("Fragrance_conn")
    > Else
    > Set conn = Server.CreateObject("ADODB.Connection")
    > conn.open "Fragrance","",""
    > Set Session("Fragrance_conn") = conn
    > End If
    > %>
    > <%
    > If IsObject(Session("retail_rs")) Then
    > Set rs = Session("retail_rs")
    > Else
    > sql = "SELECT * FROM [retail]"
    > Set rs = Server.CreateObject("ADODB.Recordset")
    > 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("Images").Value
    >
    > Could anyone please help me out?
    >
    >
    >
    > Thanks
    >
    >
    >
     
    Tim Williams, Jan 8, 2004
    #2
    1. Advertising

  3. Stuart

    Brynn Guest

    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_scripts/_database_tools/DBConn.asp" -->

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

    cp_TheConnectionString = "yourConnectionString"
    cp_DBConn(("open")
    rsArray = cp_SqlArray(yourSqlStatement)
    cp_DBConn("close")

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




    On Thu, 08 Jan 2004 05:35:58 GMT, "Tim Williams"
    <> wrote:

    >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").value & ""","
    >
    >rs.movenext
    >loop
    >end if
    >
    >strArray=left(strArray,len(strArray)-1)
    >
    >
    >//then in the js preload script
    >var yourImages = new Array(<%=strArray%>) // 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" <> wrote in message
    >news:bti22r$apn$...
    >> Hi there,
    >>
    >> I am trying to populate the array "yourImages" in the following

    >code,
    >> http://www.javascriptkit.com/script/script2/preloadimage2.shtml
    >> 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(Session("Fragrance_conn")) Then
    >> Set conn = Session("Fragrance_conn")
    >> Else
    >> Set conn = Server.CreateObject("ADODB.Connection")
    >> conn.open "Fragrance","",""
    >> Set Session("Fragrance_conn") = conn
    >> End If
    >> %>
    >> <%
    >> If IsObject(Session("retail_rs")) Then
    >> Set rs = Session("retail_rs")
    >> Else
    >> sql = "SELECT * FROM [retail]"
    >> Set rs = Server.CreateObject("ADODB.Recordset")
    >> 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("Images").Value
    >>
    >> Could anyone please help me out?
    >>
    >>
    >>
    >> Thanks
    >>
    >>
    >>

    >
    >
     
    Brynn, Jan 8, 2004
    #3
  4. Stuart

    Stuart Guest

    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(Session("Fragrance_conn")) Then
    Set conn = Session("Fragrance_conn")
    Else
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.open "Fragrance","",""
    Set Session("Fragrance_conn") = conn
    End If
    %>
    <%
    If IsObject(Session("retailU_rs")) Then
    Set rs = Session("retailU_rs")
    Else
    sql = "SELECT * FROM [retailU]"
    Set rs = Server.CreateObject("ADODB.Recordset")
    rs.Open sql, conn, 3, 3
    If rs.eof Then
    rs.AddNew
    End If
    Set Session("retailU_rs") = rs
    End If
    %>
    <head>
    <script language="JavaScript1.2">
    <!-- begin hiding
    // (C) 2000 Marcin P Wojtowicz []. 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.length);

    var locationAfterPreload = "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 preloadbarHeight = 15 // The height of the gradient/preload bar
    var backgroundOfGradient = "#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.replace(locationAfterPreload)
    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.length;
    var loaded = new Array(), preImages = new Array(), currCount = 0, pending =
    0, h = 0, hilite = new Array(), cover = new Array();

    var num = Math.floor(preloadbarWidth/gap);
    for (i = 0; i < 3; i++) {
    startingColor = startingColor.toLowerCase();
    endingColor = endingColor.toLowerCase();
    startingColor = eval(startingColor);
    endingColor = eval(endingColor);
    diff = (endingColor-startingColor)/num;
    ones = Math.floor(diff);
    sixteens = Math.round((diff - ones)*15);
    }
    endingColor[0] = 0;
    endingColor[1] = 0;
    endingColor[2] = 0;
    i = 0, j = 0;
    while (i <= num) {
    hilite = "#";
    while (j < 3) {
    hilite += convert[startingColor[j]];
    hilite += 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 = 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.replace(locationAfterPreload);
    return }
    for (i = 0; i < imgLen; i++) {
    if (!loaded[i] && preImages[i].complete) {
    loaded[i] = 1; pending++; currCount++;
    checkLoad();
    return;
    }
    }
    setTimeout("checkLoad()",10);
    }
    function changeto() {
    if (h+1 > cover[currCount-1]) {
    var percent = Math.round(100/imgLen)*currCount;
    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.backgroundColor = hilite[h]");;
    h++;
    setTimeout("changeto()",1);
    }
    defaultStatus = "Loaded 0 out of " + imgLen + " images [0%]."
    // end hiding -->
    </script>
    </head>
    <body>
    <center>
    <font face="Verdana, Arial, Helvetica" size="2"><center>Preloading Images...
    Please Wait..</center>
    <script language="JavaScript1.2">
    <!-- beging hiding
    document.write('<table border="0" cellpadding="0" cellspacing="0" width="' +
    preloadbarWidth + '"><tr height="' + preloadbarHeight + '" bgcolor="' +
    backgroundOfGradient + '">');
    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="javascript:location.replace(locationAfterPreload)">Skip Preloading</a>
    &nbsp;| &nbsp;<a
    href="http://javascriptkit.com/script/script2/preloadimage2.shtml">Script
    Credits</a></small></p></font>')
    loadImages();
    // end hiding -->
    </script>
    </center>
    </body>
    </html>




    "Brynn" <> wrote in message
    news:...[color=blue]
    > I agree ... check out my DBConn.asp script at the following ...
    >
    > [url]http://www.coolpier.com/cp/cp_scripts/index.asp[/url]
    >
    > 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_scripts/_database_tools/DBConn.asp" -->
    >
    > <%
    > Dim yourSqlStatement, rsArray, strArray: strArray=""
    > yourSqlStatement = "SELECT Images FROM [retail];"
    >
    > cp_TheConnectionString = "yourConnectionString"
    > cp_DBConn(("open")
    > rsArray = cp_SqlArray(yourSqlStatement)
    > cp_DBConn("close")
    >
    > strImgBase = "http://www.mysite.com/images/"
    > For n = 0 to ubound(rsArray,2)
    > strArray = strArray & """" & strImgBase & rsArray(0,n) & ""","
    > Next
    > strArray=left(strArray,len(strArray)-1) '//to get rid of last comma
    > %>
    >
    >
    >
    >
    > On Thu, 08 Jan 2004 05:35:58 GMT, "Tim Williams"
    > <> 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").value & ""","
    > >
    > >rs.movenext
    > >loop
    > >end if
    > >
    > >strArray=left(strArray,len(strArray)-1)
    > >
    > >
    > >//then in the js preload script
    > >var yourImages = new Array(<%=strArray%>) // 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" <> wrote in message
    > >news:bti22r$apn$...[color=darkred]
    > >> Hi there,
    > >>
    > >> I am trying to populate the array "yourImages" in the following[/color]
    > >code,[color=darkred]
    > >> [url]http://www.javascriptkit.com/script/script2/preloadimage2.shtml[/url]
    > >> 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(Session("Fragrance_conn")) Then
    > >> Set conn = Session("Fragrance_conn")
    > >> Else
    > >> Set conn = Server.CreateObject("ADODB.Connection")
    > >> conn.open "Fragrance","",""
    > >> Set Session("Fragrance_conn") = conn
    > >> End If
    > >> %>
    > >> <%
    > >> If IsObject(Session("retail_rs")) Then
    > >> Set rs = Session("retail_rs")
    > >> Else
    > >> sql = "SELECT * FROM [retail]"
    > >> Set rs = Server.CreateObject("ADODB.Recordset")
    > >> 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("Images").Value
    > >>
    > >> Could anyone please help me out?
    > >>
    > >>
    > >>
    > >> Thanks
    > >>
    > >>
    > >>[/color]
    > >
    > >[/color]
    >[/color][/i][/i][/i][/i][/i][/i][/i]
     
    Stuart, Jan 8, 2004
    #4
  5. Stuart wrote:

    > I open the database successfully like this
    > ---------------
    > <%
    > If IsObject(Session("Fragrance_conn")) Then
    > Set conn = Session("Fragrance_conn")
    > Else
    > Set conn = Server.CreateObject("ADODB.Connection")
    > conn.open "Fragrance","",""
    > Set Session("Fragrance_conn") = conn
    > End If
    > %>


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


    PointedEars
     
    Thomas 'PointedEars' Lahn, Jan 25, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWFkaXNvbg==?=

    SqlDataSource and multiple recordsets

    =?Utf-8?B?TWFkaXNvbg==?=, Jul 24, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    850
    Cowboy \(Gregory A. Beamer\)
    Jul 24, 2007
  2. Philipp
    Replies:
    21
    Views:
    1,190
    Philipp
    Jan 20, 2009
  3. JT

    recordsets and error handling

    JT, Sep 15, 2003, in forum: ASP General
    Replies:
    1
    Views:
    117
    Ken Schaefer
    Sep 16, 2003
  4. Boardrider

    Classes and recordsets

    Boardrider, Nov 3, 2003, in forum: ASP General
    Replies:
    1
    Views:
    154
    Ray at
    Nov 3, 2003
  5. SABmore

    Recordsets vs. Arrays?

    SABmore, Apr 27, 2005, in forum: ASP General
    Replies:
    48
    Views:
    582
    Mark J. McGinty
    May 6, 2005
Loading...

Share This Page