1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need Help with a PHP Function

Discussion in 'PHP' started by epic1231, Oct 19, 2009.

  1. #1
    Hey guys and girls, have this script setup to when a user visits the site they are given a score.. The users have an id already with them and that ID+score+date gets put into my MySQL DB

    I am trying to make it so the user can only get the score one time per 24 hours (I want to make it a set time like midnight EST or CMT) so for instance

    I go to site and get a score of 7, it is already entered into the DB

    if i go back to the site an hour later it tells me I already received my score today and have to wait until midnight to get a new one.. When I go back tomorrow I get a new score and it is added to the database (I don't want the previous scores to stay in the database also)

    I got this much so far (this top code is located in my top file)

    
    	// Check if users exists
    	if( !check_user($user) ) {
    [B]not sure what to enter here[/B]
    	}
    
    Code (markup):
    my check user function in my userinc file

    
    function check_user( $u ) {
    
    	$res = query("SELECT * FROM `users` WHERE `userid` = $u");
    
    	list($total_rows) = mysql_fetch_array($res);
    
    	if( $total_rows > 0 ) return true;
    	else return false;
    
    }
    
    Code (markup):
    then this is where I show the score (aka karma) (this is located in my index)

    
    	$karmarandom = rand(50,200);
    	$date = date("Y-m-d");
    	if( !check_user($user) ) {
    	$res = query("UPDATE `users` SET `karma`=(`karma`+$karmarandom) WHERE `userid`=$user");
    		echo 'Your Daily Karma Today Is '.$karmarandom.''; 
    	}
    	else {
    	$res = query("INSERT INTO users VALUES ('$uid', '$user', '$karmarandom', '$date')");
    		echo 'Your Daily Karma Today Is '.$karmarandom.''; 
    	}
    
    	if ($row[date] > time()) {
    //Limit the times a user can get Karma.
         $query = query("SELECT * FROM 'karma' WHERE userid = '$user'");
         if (mysql_num_rows($query) >= 1) {
            echo 'You have received your Daily Karma Today, it was '.$karmarandom.', Come Back Tomorrow for New Karma!';
                }
    	}
    
    Code (markup):
    What it currently does is everytime you refresh the page it gives you a new score entering that into the database, today I refreshed hte page 4 times and have 4 scores in the database..

    Any suggestions?

    Thanks,
    Bill
     
    epic1231, Oct 19, 2009 IP
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    My brain is fuzzy tonight. I can't read your code. Allow me to ramble and maybe I can inspire you.

    When the user is created their score is 0 or -1 and the date and time is inserted into 2 fields.
    Each time they visit if their score is not == to the defalut and 24 hours has passed they get a new score.
    Their score is updated as is the time and date. Updated not inserted.
    If their score is == to the default they update as is the time and date. (for first time users)

    You have to branch your actions based on the score && the elasped time.

    //if user does not exist take them to the register page

    I hope this inspires you. :)
     
    Colbyt, Oct 19, 2009 IP
    epic1231 likes this.
  3. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #3
    have it check to see if the date and year are a match .. if it is , dont add any more points , if there isn't , then its their first time that day.
     
    ezprint2008, Oct 19, 2009 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    so far what i have understood, you can do it with single query as follows..

    I hope uid is primary key and if not, you can either make it primary or create a unique key index onto it depending on the structure of the table.

    insert into karma (uid, dt, karma) values (1, current_date, 3) on duplicate key update karma = if(dt = current_date, karma, 3), dt = current_date
    Code (markup):
    let me know if this solves purpose.
     
    mastermunj, Oct 19, 2009 IP
    epic1231 likes this.
  5. epic1231

    epic1231 Peon

    Messages:
    680
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i got it guys, using inspiration from colbyt and the code from mastermun and taking a day off from looking at things it finally worked out..

    Thanks for your help it is much appreciated! + rep for both of ya
     
    epic1231, Oct 21, 2009 IP