Results 1 to 13 of 13

Thread: URGENT: What is wrong with my counter?>??!??

  1. #1

    Thread Starter
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625

    Angry URGENT: What is wrong with my counter?>??!??

    I have a counter which keeps track of how many times a page has been visited today and how much total. For some reason, my counters keep reseting... im pretty sure my code is good, but maybe its not? Can anyone please see if my code has a problem??

    PHP Code:
    function CountPage ($pg) {
        global 
    $db;
        
    $db->query ("SELECT * FROM counters WHERE page='$pg'");
        if (
    $db->fetchRow ()) {
            
    // Update the count
            
    $curtodaycount intval($db->record["todaycount"]);
            
    $curtotalcount intval($db->record["totalcount"]);
            
            
    // change the date here
            
    $todaydate $db->record["todaydate"];
            
    $curtotalcount ++;
            if (
    $todaydate == date("Y-m-d")) {
                
    $curtodaycount ++;
            }
            else {
                
    $todaydate date("Y-m-d");
                
    $curtodaycount 0;
                
                
    // Reset all counters to 0
                
    $db->query("UPDATE counters SET todaydate='$todaydate', todaycount='0'");
            }
            
            
    // Now update the count
            
    $db->query("UPDATE counters SET todaydate='$todaydate', todaycount='$curtodaycount', totalcount='$curtotalcount' WHERE page='$pg'");
        }
        else {
            
    // Create a new record
            
    $curtodaycount 1;
            
    $curtotalcount 1;
            
    $todaydate date("Y-m-d");
            
            
    // Insert
            
    $db->query("INSERT INTO counters (page,todaydate,todaycount,totalcount) VALUES('$pg','$todaydate','$curtodaycount','$curtotalcount')");
        }
        
    $db->showErrors ();

    I have no idea whats going on...
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    did you debug all the IFs to make sure it was returning true where it should have and all that jazz?

    It's probably some really small error, but those are always the hardest to find.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    It worked fine before, it suddenly stopped working (i dont kno what i did wrong)
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Shouldn't this
    // Reset all counters to 0
    $db->query("UPDATE counters SET todaydate='$todaydate', todaycount='0'");
    Include a WHERE clause for the Page being referenced?

  5. #5

    Thread Starter
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    I only have two if/else's one of them (I Know it works), is the $db->fetchRow() which just updates the db->records[] array on the record and the next one:

    PHP Code:
    if ($todaydate == date("Y-m-d")) { 
    This always worked and infact to be sure i made a simple script to test it and it worked!

    Aaron Young: I didnt use WHERE because I want it to reset ALL the counters to 0 at the end of the day.

    I have a table like this:

    Code:
    
    	Page:		Today:		Total:
    ----------------------------------------------
     Home			91		2523
     Contact Us		23		126
     Search			32		632
     ...
    And for some reason at around 6pm, the counters mysteriously reset... i dont know whats going on, but my code looks fine right? I mean it worked the other days...
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  6. #6
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    if it keeps reseting

    if ($todaydate == date("Y-m-d")) { then apparently this is never true
    $curtodaycount ++;
    }
    else {
    $todaydate = date("Y-m-d");
    $curtodaycount = 0;

    // Reset all counters to 0
    $db->query("UPDATE counters SET todaydate='$todaydate', todaycount='0'");
    }

  7. #7

    Thread Starter
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Let me rephrase that, and i have some reason to think that this is server related (maybe either the server or mysql have the wrong time)...

    Its supposed to reset at MIDNIGHT, any other time, its supposed to increment. But for some weird reason it resets at some awkward times! I verified that my code isnt the problem, but maybe the mysql or the unix machine! I am now contacting the admin and trying to get things to work out.

    Thanks everyone for your quick responses! I really appreciate it!
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by MoMad
    Let me rephrase that, and i have some reason to think that this is server related (maybe either the server or mysql have the wrong time)...

    Its supposed to reset at MIDNIGHT, any other time, its supposed to increment. But for some weird reason it resets at some awkward times! I verified that my code isnt the problem, but maybe the mysql or the unix machine! I am now contacting the admin and trying to get things to work out.

    Thanks everyone for your quick responses! I really appreciate it!
    Yeah, I'm sure.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    yeah when my code doesn't work I always blame it on the server.

  10. #10
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065
    Good idea

    LOL
    Wayne

  11. #11

    Thread Starter
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Its FIXED!! PHP returns in GMT time so I just made a custom function that will convert date() to date() - 7hours
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by MoMad
    Its FIXED!! PHP returns in GMT time so I just made a custom function that will convert date() to date() - 7hours
    It took you this long to fix it?

    The first thing I would have done would be to check the server's timezone just be echoing the date...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13

    Thread Starter
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    why didnt i think of that?
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

Posting Permissions

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



Click Here to Expand Forum to Full Width