Hits on a link (with db) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Hits on a link (with db)

Hi!
I was wondering if someone can help me with something for my site.
I am trying to write a script that counts how many visitors have clicked on a specific link (hit) using a access database (one column for the link name and another for the counter), so far without success... :-(
can someone please tell me how it can be done?
thanks!

Comments

  • bagles1bagles1 Posts: 54Member
    Well lets give it a shot. First I would use only page names. This is because if you pass anything through the querystring that is going to give you a different address, plus the relative path is really irrelevant to your goal. So column 1 will be "Page_Name" also remember that good coding practices should keep a page name all one word using an underscore to fill in where a space would be. This eliminates the need for encoding/decoding urls or parsing out the encoding. Column 2 will be "Page_Count" and call the table "Page". Ok so now you have the Database layout, now we need the code to count it, so here it is.
    <%
    ' First we need to determine which page we are on.
    strRelPath = Request.ServerVariables("PATH_INFO")
    intPosition = inStrRev(strRelPath, "/")
    if intPosition > 0 then
    strPageName = mid(strRelPath, intPosition + 1, len(strRelPath)-(4 + intPosition))
    end if
    ' Now lets update the database.
    ' here is the sql... the rest is up to you.
    strDSN = "YOUR CONNECTION STRING HERE"
    set objConnection = Server.CreateObject("ADODB.Connection")
    objConnection.Open strDSN
    strSQL = "UPDATE page SET page_count = (SELECT MAX(page_count) + 1 FROM page) WHERE page_name = '" & strPageName & "'"
    objConnection.Execute(strSQL)
    objConnection.Close
    set objConnection = nothing
    'Your done. Just put this on all your asp pages and you have a tracking system. :o)
    %>

    -Bagles1

    PS. I don't have Access only SQL Server so you might need a tweak here and there. (mostly changing the single quotes to double quotes) but try it as is first and make sure to put the proper connection string in there.


    : Hi!
    : I was wondering if someone can help me with something for my site.
    : I am trying to write a script that counts how many visitors have clicked on a specific link (hit) using a access database (one column for the link name and another for the counter), so far without success... :-(
    : can someone please tell me how it can be done?
    : thanks!
    :

  • KingOfTheWorldKingOfTheWorld Posts: 44Member
    what should I put here:
    strDSN = "YOUR CONNECTION STRING HERE" ?




    : Well lets give it a shot. First I would use only page names. This is because if you pass anything through the querystring that is going to give you a different address, plus the relative path is really irrelevant to your goal. So column 1 will be "Page_Name" also remember that good coding practices should keep a page name all one word using an underscore to fill in where a space would be. This eliminates the need for encoding/decoding urls or parsing out the encoding. Column 2 will be "Page_Count" and call the table "Page". Ok so now you have the Database layout, now we need the code to count it, so here it is.
    : <%
    : ' First we need to determine which page we are on.
    : strRelPath = Request.ServerVariables("PATH_INFO")
    : intPosition = inStrRev(strRelPath, "/")
    : if intPosition > 0 then
    : strPageName = mid(strRelPath, intPosition + 1, len(strRelPath)-(4 + intPosition))
    : end if
    : ' Now lets update the database.
    : ' here is the sql... the rest is up to you.
    : strDSN = "YOUR CONNECTION STRING HERE"
    : set objConnection = Server.CreateObject("ADODB.Connection")
    : objConnection.Open strDSN
    : strSQL = "UPDATE page SET page_count = (SELECT MAX(page_count) + 1 FROM page) WHERE page_name = '" & strPageName & "'"
    : objConnection.Execute(strSQL)
    : objConnection.Close
    : set objConnection = nothing
    : 'Your done. Just put this on all your asp pages and you have a tracking system. :o)
    : %>
    :
    : -Bagles1
    :
    : PS. I don't have Access only SQL Server so you might need a tweak here and there. (mostly changing the single quotes to double quotes) but try it as is first and make sure to put the proper connection string in there.
    :
    :
    : : Hi!
    : : I was wondering if someone can help me with something for my site.
    : : I am trying to write a script that counts how many visitors have clicked on a specific link (hit) using a access database (one column for the link name and another for the counter), so far without success... :-(
    : : can someone please tell me how it can be done?
    : : thanks!
    : :
    :
    :

  • StPatrickStPatrick Posts: 76Member
    : what should I put here:
    : strDSN = "YOUR CONNECTION STRING HERE" ?


    Your connection string, how you connect to database, like "DSN=MyDSN" or "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:dbmydatabase.mdb"

    [b][italic]Philip Patrick[/italic][/b]
    [b]Home page[/b]: http://www.saintopatrick.com
    [size=1][italic]"Two beer or not two beer?"[/italic] [b]Shakesbeer[/b][/size]

  • KingOfTheWorldKingOfTheWorld Posts: 44Member
    It counts when the user is already in the page.
    But what if I want to count how many user have downloaded
    a file (*.zip)? It is different. How can I do This?
  • _Quentin_Quentin Posts: 10Member
    : It counts when the user is already in the page.
    : But what if I want to count how many user have downloaded
    : a file (*.zip)? It is different. How can I do This?
    :

    i always do this with a file called "redirect.asp" (opened in a new window most times)

    so you list your links and instead of using href="http://something" you use href="redirect.asp?id=3"

    redirect.asp gets the id and looks whetere this id is in the database. if a row has been found you update the column VISITS and then you do a response.redirect(filename)



    performance hint: save the filename to redirect in a variable instead of writing response.redirect recordset.fields("filename")

    after updating the recordset you have to close the connection and the recordset and destroy the objects. for your server's health ;) then you can do the redirect...

    hope that helps ;)
Sign In or Register to comment.