2016-03-31 41 views
-1

我真的很新,在後端的東西,我正在建立一個Facebook應用程序與多個照片條目與投票機制。您可以每天投一次或多次投票,並且它還會檢測您的Facebook ID,這樣當您投票時,數據庫會檢測您的Facebook ID,您投票的參賽號碼以及您投票的日期。當您在當前日期投票時,會出現一個彈出窗口,顯示「您已成功投票」。然後,當你再次嘗試同一日期時,彈出框的消息將會改變,相反,它會顯示「明天再試一次」。它還顯示了入口頁面上的投票數量。如何每天限制一次Facebook應用程序的投票功能?

我唯一的問題是,當你是第一次用戶時,它可以正常工作,你可以在當前日期投票所有條目,但是當你第二天回來時,當你投票時,你仍然會說你已經投了票,而選票數不會更新。

下面是PHP的頁面代碼:(編輯這,已經解決了這個問題,感謝您的幫助)

date_default_timezone_set('Asia/Manila'); 

     // Get last vote date: fetch_date_voted returns none if user voted for first time 
     $current_date = date('Y-m-d'); 
     $fetch_date_return = $this->fetch_date_voted($entry_id, $facebook_id, $table_prefix, $conn); 
     $last_vote_date = $fetch_date_return['last_date']; 


     $return['date_last_voted'] = $fetch_date_return; 

     // Below is a code i got from stackoverflow - 844641242329946 (kristina id) 
     /*if($last_vote_date && ($last_vote_date == "none" || (strtotime($last_vote_date) + (60*60*24)) < strtotime($current_date))){*/ 
     // Below is the original code 
     if($last_vote_date == "none" || $last_vote_date < $current_date) { 
      $table_name = $table_prefix . '_voter'; 
      $query = $conn->query(" 
       INSERT INTO $table_name 
        (facebook_id, entry_id, date_voted) 
       VALUES 
        ($facebook_id, $entry_id, '$current_date') 
      "); 
      // 

      // After doing INSERT, the variable $query will return a TRUE status 
      if ($query) { 

       // If the date fetched is TRUE, it will UPDATE 
       $update = $this->update_count($entry_id, $table_prefix, $conn); 

       // If update is successful, it will fetch the latest vote_count 
       if($update) { 

        $fetched_data = $this->fetch_current_count($entry_id, $table_prefix, $conn); 

        if ($fetched_data['status']) { 
          $return['status']   = true; 
          $return['vote_count']  = $fetched_data['vote_count']; 

        } else { 
         $return['status'] = false;       
        } 
       } else { 
        $return['status'] = false; 
       } 

      } else { 

       die('Error : ('. $conn->errno .') '. $conn->error); 
       $return['status']   = false; 
       $return['error_response'] = $conn->error; 

      } 
     } else { 
      $return['status'] = false; 
      $return['error_response'] = 'date_invalid'; 

     } 


     echo json_encode($return); 

     //$query->close(); 
     $conn->close(); 
    } 

回答

0

$ last_vote_date和$ CURRENT_DATE是日期的字符串表示,將它們轉換爲時間和它的工作原理:

strotime($last_vote_date) < strotime($current_date) 

當心,這將始終返回true,因爲你想讓他們不使用它了一天,所以這將是:

if($last_vote_date && ($last_vote_date == "none" || (strtotime($last_vote_date) + (60*60*24)) < strtotime($current_date))){ 

添加24小時。

+0

例如,如果我今天晚上11點投票,它會讓我在次日12:10投票嗎? –

+0

是的,你明白我給你的答案嗎?什麼不清楚,所以我可以幫你理解它?要測試它,請更改數據庫記錄並將日期設置爲24小時。 – Garytje

+0

我不知道將代碼放在我的PHP文件的哪裏。我將如何將日期轉換爲時間?我是否需要將數據庫列date_voted的類型更改爲TIME或DATETIME或TIMESTAMP?它目前處於DATE類型。 –

相關問題