Initialize a value ONLY once

Discussion in 'PHP' started by lost, Dec 15, 2005.

  1. #1
    How and where can i declare a variable that is initialized to the value "1" only once ever and gets incremented always.
     
    lost, Dec 15, 2005 IP
  2. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you have a file that is included at the top of each page like a counter or database connection file you could stick it in there.
     
    dave487, Dec 15, 2005 IP
  3. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i tried, its not working.
    i also tried static variable...no luck
     
    lost, Dec 15, 2005 IP
  4. HN Will

    HN Will Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is a very cryptic question - in what context are you using it? What is its purpose?
     
    HN Will, Dec 15, 2005 IP
  5. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i need to have a counter variable that will always increment and never repeat the same number.
     
    lost, Dec 15, 2005 IP
  6. Djohn

    Djohn Peon

    Messages:
    75
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Every time the page is loaded or connected to something else?
     
    Djohn, Dec 15, 2005 IP
  7. HN Will

    HN Will Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yeah - where are you using this variable? When do you want it incremented?
     
    HN Will, Dec 15, 2005 IP
  8. himambo

    himambo Peon

    Messages:
    100
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    himambo, Dec 16, 2005 IP
  9. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Himambo, that is exactly what i need.
    I've followed the tutorial, however, i keep getting the following error:

    Access denied for user 'ODBC'@'localhost' (using password: NO)
     
    lost, Dec 16, 2005 IP
  10. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    by the way, it only gives me that error when trying to do the update, why?
     
    lost, Dec 16, 2005 IP
  11. Djohn

    Djohn Peon

    Messages:
    75
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Are username, password and host correct?

    By the way, you should be aware that this script won't count actual visitors, just hits on page. If a visitor chooses to load a page several times, that would register as individual hits as well.
     
    Djohn, Dec 16, 2005 IP
  12. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Yes username, password and host are correct.
    Actually i didnt realize the refresh would also increment the counter.
    I have a button called "Create" which causes a form to be filled out. Upon completion of the form, the user submits. I need to associate a unique number for every new 'creation'. does that make sense?
     
    lost, Dec 16, 2005 IP
  13. Djohn

    Djohn Peon

    Messages:
    75
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Absolutely

    Do you submit the contents of the form to a mysql database? If so, you could just add an ID column with auto increment and save yourself the work of doing it in php.
     
    Djohn, Dec 16, 2005 IP
  14. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Actually I got it to work. It was my bad, i had named the insert field wrong when trying to write to the database.

    This works fine with what i'm trying to accomplish. The numbers may bot always be sequential, depending on refreshing the page, but as long as they unique, i'm good.

    Thanks for all your help guys.
     
    lost, Dec 16, 2005 IP
  15. himambo

    himambo Peon

    Messages:
    100
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Congrats :)
     
    himambo, Dec 16, 2005 IP
  16. HN Will

    HN Will Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #16
    To throw out another tip that wouldn't be too much extra code - you could check the ip address or referer page to check for dups or bots...
     
    HN Will, Dec 16, 2005 IP