SitePoint Sponsor

User Tag List

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 64
  1. #26
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,029
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Try:
    Set rs=conn.Execute(sql)

    and then:
    arr = rs.GetRows
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  2. #27
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thats what i tried and it started working, thanks
    But now i got other 2 questions to ask
    1: in general, i know what Dim is for, but do i really have to write it everywhere? i mean it doesnt give any errors if i dont write it...what benefit does it gives

    2: here is my form now, after using GetRows, but apperantly im still using RS opening and closing all over the page as well as the conn that needs to stay opened for the whole page, i dunno if its because the page is too long (since there are alot of SQLs and infos to pull out of the DB), but i just want u to take a look at it and tell me if i can write it somehow in a shorter way.

    Code ASP:
    <%
    DB = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/animals.mdb")
    Set Conn = Server.CreateObject("ADODB.Connection")
    Conn.Open DB
    GetMsg = "SELECT FileID, Desc FROM Animals WHERE (animaltype= 'msgs')"
    Set rs = Conn.Execute(GetMsg)
    table1 = rs.GetRows
    Set rs= Nothing
    limit = UBound(table1,2)
    For iCounter = 0 to limit
    sDesc = Replace (table1(1,iCounter), vbNewLine, "<br/>")
    iPetID = table1(0,iCounter)
    GetPics = "SELECT FileName FROM pictures WHERE UniqueNum=" & iPetID
    Set rs = Conn.Execute(GetPics)
    table2 = rs.GetRows
    Set rs = Nothing
    limit2 = UBound(table2,2)
    For iCounter2 = 0 to limit2
    %>
    ' Pulling out required data (Msgs and their pics)
    <%
    next
    Next
    GetAnimals = "SELECT FileID, PetName, Type, Desc FROM Animals WHERE (adoptionstatus= 'available')  AND (animaltype= 'Month dog')  ORDER BY fileID DESC"
    Set rs = Conn.Execute(GetAnimals)
    table1 = rs.GetRows
    Set rs = Nothing
    limit = UBound(table1,2)
    For iCounter = 0 to limit
    sDesc = Replace (table1(3,iCounter), vbNewLine, "<br/>")
    iPetID = table1(0,iCounter)
    GetPics = "SELECT FileName FROM pictures WHERE UniqueNum=" & iPetID
    Set rs = conn.Execute(GetPics)
    table2 = rs.GetRows
    Set rs = Nothing
    limit2 = UBound(table2,2)
    %>
    ' Pulling out information (Month dogs)
    <%
    For iCounter2 = 0 to limit2
    %>
    ' Pulling out month dogs images
    <%
    Next
    Next

    I think thats enough, its not the whole page its just 1/3 of it, but its kinda the same so ill avoid writing too much... but as u can see / guess i repeat RS.execute, RS = nothing at least 8 times in that page.

    Thanks for the help

  3. #28
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,029
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Well that's where you get into your coding style and "best" practices.

    Dimensioning your variables isn't mandatory in ASP unless you have <% OPTION EXPLICIT %> at the top of your page. Option explicit is handy when you first start out because it double checks everything you do and gives you a nasty error page if you forget anything. Option explicit also slows down your pages so if you use it during development, remove it before production.

    Now, dimming your variables does help out in that if you dim a variable on your page and then a few years later make an addition and try to dim another variable, ASP will throw an error telling you that you're trying to dimension a variable tat has already been dimmed. That'll save a bunch of time when things don't work out the way you planned

    Now you can figure out your own best practices when it comes to writing ASP but here are a few I try to adhere to:

    1) Write a header at the top of the page describing what the file is, what it does, and what it's dependencies are. Update it whenever you make changes. It could look like this:
    Code:
        '================================================================================
        '    NAME OF THE FILE          
        '    Build:    1.0
        '    Author:   Your Name
        '    Web:      www.your-website.com
        '    Date:     02-01-2009
        '    Revised:  03-22-2009
        '    Revised:  01-25-2010
        '    Revised:  06-30-2010
        '    Revised:  09-01-2010 to include project fields for up to 8 project entries.
        '    Copyright 2009 Your Company All Rights Reserved.
        '    
        '    This code queries the database for the member in question,
        '    retrieve the date that they became active to produce a schedule
        '    of three month reporting terms. In turn, the member or administrator will 
        '    be able to view or update monthly reports or quarterly summaries.
        '         
        '================================================================================
    2) If I'm going to dimension variables, I do it at the top of the page under the header so I can see them all in the same spot and I won't write the same name twice.

    3) I Normalize my databases and then I can simplify database calls. You'll need to talk to the database gurus about this.

    4) Use functions to do repetitive work. If you set a connection string at the top of the page and then make repetitive database calls with getrows to return an array of results, wrap up the code into a function.

    I use something like this:
    (You'll probably have to adjust for your use)
    Code:
        ' Function to run a query against a database and return a 2-dimensional result set
        ' Arguments:
        '    string query - A database query 
        '    Requires Conn - A database connection object
        '    Adds error data to a variable called session("message")
        function runquery(query) 
            on error resume next
            Set objRS     = Conn.Execute(query)
            if err.number<>0 then 
                session("message") = session("message") & "<p><strong>Warning:</strong> An error occured during this database transaction. Please try again. <br />If this error persists please contact ME and reference the following error.</p>"
                session("message") = session("message") & "<p>" & err.number & "<br />" & err.description & "<br />"
                session("message") = session("message") & "Database Query: " & query & "</p>"
                err.Clear
            else
                if NOT objRS.EOF AND NOT objRS.BOF then
                    runquery = objRS.GetRows
                end if                
            end if
            set objRS = nothing
        End Function
    So then I create my database array with something like the following and everything gets taken care of by the function and if any errors are present, they get stuffed into a session variable:
    Code:
                strQuery = "SELECT * from my_DB_Table WHERE something = " & some_variable
                myArray = runquery(strQuery)
    5) I Put all of my functions in the same place... Top of the page or bottom of the page, it doesn't matter but in one spot that you can refer to quickly if you need to adjust or extend your code.

    6) Destroy all object as soon as possible so that I don't create memory leaks. That means for Database connections at the end of the page and database recordsets as soon as I'm done with them.

    7) I Comment anything tricky or changes I've made over time. Sometimes just notes like the following:

    Code:
    'NOTE: Jan 25 2010 - We may need to enter in weeks where there is no data available to ensure that dates are handled correctly in the reporting page.
    Well that's a start but the more you work with code the more you'll come up with your own coding practices.
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  4. #29
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    but if i use the same function (runquery(query)) for 2 GetRows calls, which in my case are inside each other (inner getrows call, outer getrows call), wouldn't it overwrite the previous Getrows? so that ill end up getting error after the first loop
    If not then i could probably try it out...

  5. #30
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The whole purpose of a function is to return a result based on what was sent TO the function. The key is that the result is assigned to the variable that called the function.
    Code:
    table1 = runquery ("SELECT * FROM Table1")
    table2 = runquery ("SELECT * FROM Table2")
    Here we can see that we are using the same function (runquery) but in each case it is returning an array (GetRows) to two different variables. And in each case, what is returned is different.

    Just think of runquery like any other function in ASP. E.g. does the UCase() function always return the same result? No - it returns an uppercase version of the string sent to it.
    Ian Anderson
    www.siteguru.co.uk

  6. #31
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,029
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    The whole purpose of a function is to return a result based on what was sent TO the function. The key is that the result is assigned to the variable that called the function.
    Exactly
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  7. #32
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    heh thanks for the advices Now i got it even faster by adding the JOIN method i got a while ago from the database forum, reducing my SQL calls from 2 to 1 (in total on the adoption page from 8 to 4 )

    Code:
    sql = "SELECT a.FileID " &_
    ", a.Desc" &_
    ", p.FileName " &_
    "FROM Animals AS a " &_
    "LEFT OUTER " &_
    "JOIN pictures AS p " &_
    "ON p.UniqueNum = a.FileID " &_
    "WHERE a.AnimalType= 'dogs'"

  8. #33
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Tweaking can become addictive!
    Ian Anderson
    www.siteguru.co.uk

  9. #34
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    oh well now i ran into a problem ;P i cant find out the solution for displaying more than 1 photo for a certain dog, can any1 give me any advices on how to do it? ill probably need another counter to do that ?

    Code ASP:
    sql = "SELECT a.FileID, a.Desc, p.FileName FROM Animals AS a LEFT OUTER JOIN pictures AS p ON p.UniqueNum = a.FileID WHERE a.AnimalType= 'dogs'" 
    table1 = runquery(sql)
    if isarray(table1) then
    limit = UBound(table1,2)
    For iCounter = 0 to limit
    sDesc = Replace (table1(1,iCounter), vbNewLine, "<br/>")
    <tr><td>
    ' Pet Infos....
    </td><td>
    ' here the photos should be displayed...
    </td></tr>

  10. #35
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I suspect this is one instance where a 2nd SQL command will be needed. 1st SQL command gets the main details, then as you cycle through that getrows array you call another SQL command to get all/some photos for that animal.
    Ian Anderson
    www.siteguru.co.uk

  11. #36
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    oh, so that didnt help me reduce SQL calls after all

  12. #37
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It did - if you wanted to only show ONE image.
    Ian Anderson
    www.siteguru.co.uk

  13. #38
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It did - if you wanted to only show ONE image.
    even thats false !
    if i had only 1 image per pet than i wouldn't need this pictures table at all
    Anyways i thought of something else that might help me avoid a 2nd SQL call and thats:
    Deleting my Pictures table and adding 4 more coluums to my Animals table:
    Pic1, Pic2, Pic3, Pic4

    Ofc the downside of that is that i might have alot of empty fields, and that ill have to check for each image if the field is not empty...(empty means no image..)

    Tell me what do u think, the current way or this one would be better

    thanks..

  14. #39
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    IMHO that's a backwards step. What if in the future you want to allow 5 images? or 6? or 10? If I was doing this then my DB structure would be something like ...
    Code:
    Pets
    PetID | AnimalID | AnimalName |  Description   | AnyOtherStuff
      1   |    1     |    Fido    | A fluffy puppy |     Blah
    
    Animals
    AnimalID | AnimalType
       1     |    Dog
    
    Pictures
    PetID |   PicName    | MainPic
      1   | Photo001.jpg |    1
      1   | Photo002.jpg |    0
      1   | Photo003.jpg |    0
      1   | Photo004.jpg |    0
    When listing the animals (e.g. by type) I would use something like:
    Code:
    SELECT a.AnimalName,a.Description,p.PicName 
    FROM Animals a INNER JOIN Pictures p ON a.PetID=p.PetID 
    WHERE a.AnimalID=1 AND p.MainPic=1 
    ORDER BY a.AnimalName
    When an animal is chosen I would have another page where I would get the details in one SQL, and then the photos in another SQL.
    Code:
    SQL1
    SELECT * FROM Animals WHERE PetID=1
    
    SQL2
    SELECT PicName FROM Pictures WHERE PetID=1
    In summary - I wouldn't show multiple photos when showing multiple animals, only when showing more details about individual animals.
    Ian Anderson
    www.siteguru.co.uk

  15. #40
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    oh nice suggestion ! i liked it, but the only downside again is the redirection to another page, having ppl redirected for every pet they click will annoy alittle bit in my opinion
    how about some function that pops up a small window in the center of the page with all the pics and details of the selected animal using the SQLs?

    Is there any function i could use ? (something like Thumbviewer for images for example)

  16. #41
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Of course it is possible.

    One way would be to have a hidden DIV, and when you click on a pet it uses some Javascript/AJAX to call an ASP page, get the picture details, returns a string that represents the HTML for the pictures, then populates the innerHTML of the DIV and then shows it.

    But in my opinion, clicking on a pet to see its pictures, then having to click BACK is not a great hardship - most people are used to doing this.
    Ian Anderson
    www.siteguru.co.uk

  17. #42
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I thought more of just showing the main pic for each pet, then when they click on it a window pops up at the centers with all the details / rest of the images...
    Well ill check the Javascript forum for some way of doing that, altho i'd preffer u would show me some example if thats possible, since u know what im talking about already...

    thanks in advance

  18. #43
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Define a DIV and give it an ID.
    Code:
    <div id="photos"></div>
    Make sure on page load that it is hidden.
    Code:
    <script type="text/javascript">
    window.onload = function {
      document.getElementById('photos').style.visibility='hidden';
    }
    </script>
    </head>
    When displaying each pet have a click event (e.g. on the main photo).
    Code:
    <img src="photo001.jpg" onclick="morephotos('<%=PetID%>')" />
    And then your onclick function could be something like below. (Put this before the </script> tag above).
    Code:
    function morephotos (petid) {
    	var xmlHttp;
    	var resp = '';
    	try {
    		// Firefox, Opera 8.0+, Safari
    		xmlHttp = new XMLHttpRequest();
    	}
    	catch (e) {
    		// Internet Explorer
    		try {
    			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch (e) {
    			try {
    				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch (e) {
    				alert ('Your browser does not handle the photo selection process');
    				return false;
    			}
    		}
    	}
    	xmlHttp.onreadystatechange = function() {
    		if (xmlHttp.readyState == 4) {
    			resp = xmlHttp.responseText;
    			if (resp == 'ERROR') {
    				alert ('There was a problem getting the photos.');
    			} else {
    				el = document.getElementById('photos');
    				el..innerHTML = resp;
    				el.style.visibility = 'visible';
    		}
    	}
    
    	// Send the info to the PHP handler page
    	var photos = 'getphotos.asp?petid=' + petid;
    
    	xmlHttp.open ("GET",data,true);
    	xmlHttp.send (null);
    }
    Finally your ASP code would get the petid, query the database, get the photos, create a string with the HTML code (including some Javascript to allow the DIV to be clicked to close (hide) it). This string is in the above Javascript as the resp variable. (Just think about what the HTML would normally look like - it is this that needs to form the innerHTML of the DIV). If there is an error getting the photos then the string returned could be ERROR so that you can handle it and not show the DIV.

    Make a start with that lot first.
    Ian Anderson
    www.siteguru.co.uk

  19. #44
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ehm sounds complicated ;P , Im going to start from the fact that my Div doesnt get hidden for some reason, i think u got some mistake at the hidding function ;p
    Also where is the line that contains the SQLs to get the details / images is?

  20. #45
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hidden DIV? That's just how I would do it, so as to maximise screen usage and only show additional details/photos when selected. That way you can use as much space as you need for these photos without wasting loads of space in the main display.

    The SQLs? That's in the last paragraph. You already know how to get the data from the database, and you know how to manipulate the recordset (getrows) to produce the HTML to display them. So there's no point in me explaining it again. The only difference now is that instead of Response.Write-ing the HTML you concatenate it into a string, and then Response.Write that final string.

    PS - I've just noticed there's a missing } ... it should be on the line after el.style.visibility = 'visible';
    Ian Anderson
    www.siteguru.co.uk

  21. #46
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,029
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Hey Ian,
    Those are some great solutions you posted for the database structure and managing the pet gallery. Very nice. It's also nice to see I'm not the only one cobbling together ASP/AJAX code on the fly.

    These are pure gold!

    Cheers,
    Andrew
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  22. #47
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you want to really jazz up your web page with things like rollover images and AJAX then you should look into jQuery which has many plugins to assist in this area.

    jQuery with Classic ASP virtually eliminates the need for ASP.net

    PS - this thread is getting off-topic and should be closed

  23. #48
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,029
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webber123456 View Post
    if you want to really jazz up your web page with things like rollover images and AJAX then you should look into jQuery which has many plugins to assist in this area.

    jQuery with Classic ASP virtually eliminates the need for ASP.net

    PS - this thread is getting off-topic and should be closed
    Yup jQuery is my favorite JS library and there are lots of bits of example code out there.

    I don't see why the thread should be closed though there's lots of good information here about speeding it up, particularly the additions about running simple queries to produce the main gallery pages and then running XMLHTTP in the background to do a light-box type treatment for the additional images. That's super valuable information.
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  24. #49
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i agree there's alot of infos here, i didn't start building it up with siteguru's idea yet tho, im a few pages away from having a fully working backup files of my website, when ill finish with that i can start head-clean with this idea of siteguru

    Thanks all for the help tho, you really helped me out alot couldn't see my self progressing with this website without u

  25. #50
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webber123456 View Post
    if you want to really jazz up your web page with things like rollover images and AJAX then you should look into jQuery which has many plugins to assist in this area.
    I agree. But I've not spent any time yet trying to get my head around JQuery, hence why I posted a pure JS solution.
    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

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