-1

I'm really new at back-end stuff and I'm building a Facebook app with multiple photo entries with voting mechanics. You can vote one or multiple entries once per day and it also detects your Facebook ID so that when you vote, the database detects your Facebook ID, the entry number you voted, and the date you voted. When you vote during the current date, a pop-up will appear that says "you successfully voted". Then when you try again the same date, the pop-up's message will change and instead, it will say "try to vote again tomorrow". It also shows the number of votes on the entry page.

My only problem is that when you're a first time user, it works fine, you can vote all the entries on the current date, but then when you come back the next day, when you vote, the pop-up will still say that you already voted, and the vote count will not update.

Here's the code for the PHP page: (edited this, already solved the problem, thanks for the help)

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();
    }

1 Answers1

1

$last_vote_date AND $current_date are string representations of a date, convert them to time and it works:

strotime($last_vote_date) < strotime($current_date)

Beware that this will always return true, because you want them to not use it for a day, so it would be:

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

to add 24 hours.

Garytje
  • 874
  • 5
  • 11
  • For example, if I vote 11:00pm today, will it let me vote 12:10am the next day? – Jeorge Philip Mar 31 '16 at 10:02
  • yes, do you understand the answer i gave you? What is unclear so i can help you understand it? To test it, alter a database record and set the date back 24 hours. – Garytje Mar 31 '16 at 10:05
  • I don't know where to put the code on my PHP file. And how will I convert my date to time? Do I need to change type of database column date_voted to TIME or DATETIME or TIMESTAMP? It's currently on DATE type. – Jeorge Philip Mar 31 '16 at 10:51
  • You don't need to change anything in your database. I was under the impression that you wrote the php file yourself. i've edited my answer to the complete if statement. – Garytje Mar 31 '16 at 11:14
  • Ah yes, I already tested it yesterday, I assumed that the code would be placed there. Someone only guided me on what to do because I really haven't done projects with back-end. He's somehow super busy that's why I asked here. Btw, it still doesn't work, on the database I have a vote last 2 days ago, then when I vote that same entry today, it still says I've already voted and the vote count doesn't update. – Jeorge Philip Apr 01 '16 at 02:29
  • if you echo the $last_vote_date for that record, what do you get? – Garytje Apr 01 '16 at 07:43
  • Already tweaked this, thank you for your help sir! :) See above edit on my question :D – Jeorge Philip Apr 28 '16 at 07:49