Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    51 03' -78" N -114 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Useing a if statement to do response.write

    I am creating an asp page that grabs info from a database file in access (not ready for sql lol) and I was wondering if there is a way that when someone clicks on a link it gives them all the listings of the names in the names field that starts with the letter A and if they clicked on link B they would get all the names that started with the letter B but not the A I have the javascript made for the dom for the hide and show and all that done! I just need to know what if statement to use in my file to check the db file for the letter A and then do a response.write accordingly I figured ParseInt would work but that is for letters! lol
    Not sure if this is a Javascript question or a ASP question but I am not able to Cross post them so I thought I would try here first

    Any thoughts


  • #2
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    Rather than doing this with Javascript and hiding/showing DOM objects, it is better to do all the clever stuff behind the scenes with ASP. You can pass the letter you want to the ASP page on the address bar of the browser which you then feed into your SQL statement for querying the database.

    This example should show you how to do it. Save the page in the same directory as an Access database called "database.mdb". Create a field called "names" in a table called "people" and you are away.

    Good luck! Post back here if you need anything explaining further.


    <html>
    <head>
    <title>Querying a database based on a letter</title>
    </head>
    <body>

    <%
    thisPage = Request.ServerVariables("SCRIPT_NAME")
    response.write "<a href="""&thisPage&"?letter=a"">a</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=b"">b</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=c"">c</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=d"">d</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=e"">e</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=f"">f</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=g"">g</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=h"">h</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=i"">i</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=j"">j</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=k"">k</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=l"">l</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=o"">o</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=m"">m</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=n"">n</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=o"">o</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=p"">p</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=q"">q</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=r"">r</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=s"">s</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=t"">t</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=u"">u</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=v"">v</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=w"">w</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=x"">x</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=y"">y</a>&nbsp;|&nbsp;"
    response.write "<a href="""&thisPage&"?letter=z"">z</a>"

    letter = Request.Querystring("letter")

    If Len(letter) <> 1 Then
    Response.Write "<p>Pick a letter</p>"
    Else

    dbPath = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("database.mdb")
    set objConn = Server.CreateObject("ADODB.Connection")
    set objRS = Server.CreateObject("ADODB.Recordset")
    objConn.Open dbPath
    objRS.Open "SELECT * FROM people WHERE names LIKE '"&letter&"%'", objConn

    If objRS.EOF Then
    Response.Write "<p>No records found for <strong>"&letter&"</strong></p>"
    Else
    Do While Not objRS.EOF
    Response.Write "<p>"&objRS("names")&"</p>"
    objRS.MoveNext
    Loop
    End If

    objRS.Close
    objConn.Close
    set objRS = nothing
    set objConn = nothing

    End If
    %>

    </body>
    </html>
    As easy as 3.1415926535897932384626433832795028841

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd definitely agree with the above post and stick to doing it the simple ASP way for now - if nothing else, it's MUCH easier.

    Once you've got that figured out, then perhaps experiment with preloading every possible javascript array, etc. with information from a database and deciding what information to display using DOM; or perhaps using a hidden frame to send a request to the server, or use another tricky technique to get the information without the page refreshing.

    But all of that is quite a bit more complicated, and could conceivably take longer to initially load than just doing it the easy way and refreshing the page or whatnot.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    51 03' -78" N -114 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    WoW! i just tried the script you gave me and it looks like its going to work even better then what I was working with as it will even work on non javascript enabled browsers! Damn even works with every platform I have tried!

    Its kinda too bad I worked hard on the script below trying to make sure it was cross browser compatable and that no matter what screen size or browser the person was useing and I was very proud of myself when I finally figured it out! but what you have written works just as well but even better! It almost seems like asp is the answer to the whole cross browser compatability issue (At least to me it does) I guess thats because its server side right meaning that the browser really doesnt do anything!

    Thanks again! but check my script out and tell me what you think!



    <SCRIPT LANGUAGE="JavaScript1.2">
    function HideShow(){
    //if(!ns && !ie && !w3) return; needed for netscape? dont know why
    if(!ns && !ie && !w3) return;

    IObj=eval(doc + 'I' + sty);IObj.visibility ="hidden";if(ie){IObj.left = document.body.clientWidth/2 - I.offsetWidth/2;}if(ns||w3){IObj.left = innerWidth/2 - 135;}
    }

    onload=HideShow ;
    //End-->
    </script>

    <a href="#" onClick="Javascript:HideShow();IObj.visibility='visible';">I</a>

    <%
    //create connection to database with dns-less connection
    Set Conn = Server.CreateObject("ADODB.Connection")
    Set RS = Server.CreateObject("ADODB.Recordset")
    StrConn = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.Mappath("file.mdb")
    Conn.Open(StrConn)

    //Visitor information and Message was now saved to database; To create a simple table for displaying all data from database
    //The ORDER BY DateField DESC is what makes everything show up newest to oldest remove to have it go oldest to newest

    Set RSI= Conn.Execute("select * from files ORDER BY DateField DESC")
    %>




    <div id="I" style="position:absolute;visibility:hidden">
    <%
    do until RSH.EOF

    If RSI.fields("FNameField") <>"" Then
    response.write("<table border='0'>")
    response.write("<tr>")
    response.write("<td>")
    response.write("<p align=center>")
    response.write("<b><a href='#' target='_blank' >" & RSI.fields("FNameField") & " " & RSI.fields("MNameField") & " " & RSI.fields("LNameField") & "</a></b>")
    response.write("</p>")
    response.write("</td>")
    response.write("</tr>")
    response.write("</table>")
    Else
    response.write(" <td width='30%'>&nbsp;</td>")
    End If

    RSI.movenext
    loop
    %>


    </div>
    But as I always have trouble with the mathimatical parts of these scripts I.e. I understand that this "==" is equals to and "!=" is not equal to and "<" is less then and ">" is greater then and I can understand what they are about but what does "<>" stand for in the scripts above! and how can I tell it that if the field starts with the letter I then show it else dont show anything

    Thanks again

    P.S. Dont take this the wrong way but I think you might want to freshen up on you alphabet last time I checked O is after n not before lol I realize that writing long scripts its gaurenteed to have typos lol just had a chuckle when I saw that one and wanted to share it!
    Last edited by Crash1hd; 01-25-2003 at 06:47 AM.

  • #5
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Crash..., glad you found the script useful and spotted my deliberate mistake with the alphabet!

    In answer to your question, <> means "does not equal", it's as simple as that.

    The code I wrote only picks those entries from the database who start with the letter specified. No need to hide ones that start with a different letter! To acheive this I was using a LIKE clause in the SQL statement used to query the database. For example, using:

    SELECT * FROM people WHERE names LIKE 'a%'

    will pick all the entries from the people table where the names field entry begins with the letter a.

    As you correctly spotted, it's far better and cross-broswer compliant to do as much as possible on the server side in ASP rather than relying on client-side javascript.

    Happy coding...
    As easy as 3.1415926535897932384626433832795028841

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    51 03' -78" N -114 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Excelent! I totally agree and i would just like to say thankyou again

    Adam

    P.s. I may call upon your help again in time lol


  •  

    Posting Permissions

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