Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class db_connect {
- // ---------------------------------------------------------------
- // Public functions
- // ---------------------------------------------------------------
- public function updateDatabase($entry_id, $facebook_id, $table_prefix) {
- $return['status'] = "";
- $return['error_response'] = "";
- $return['vote_count'] = "";
- $return['date_last_voted'] = "";
- //echo json_encode($entry_id);
- /* Connect to mysql */
- $servername = "servername";
- $username = "username";
- $password = "password";
- $db_name = "db_name";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $db_name);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- die("Error : ( $mysqli->connect_errno ) " . $mysqli->connect_error);
- }
- // Main codes here
- 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');
- $last_vote_date = $this->fetch_date_voted($entry_id, $facebook_id, $table_prefix, $conn);
- $return['date_last_voted'] = $last_vote_date;
- if( $last_vote_date && ($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 inserting, the variable $query will return a true status
- if ( $query ) {
- //If the data fetched is right, it will update
- $update = $this->update_count($entry_id, $table_prefix, $conn);
- //if condition is successful, it will update the 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();
- }
- public function fetchDatabase($entry_id, $table_prefix) {
- $return['status'] = "";
- $return['error_response'] = "";
- $return['vote_count'] = "";
- /* Connect to mysql */
- $servername = "servername";
- $username = "username";
- $password = "password";
- $db_name = "db_name";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $db_name);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- die("Error : ( $mysqli->connect_errno ) " . $mysqli->connect_error);
- }
- $fetched_data = $this->fetch_current_count($entry_id, $table_prefix, $conn);
- $return['status'] = true;
- if ($fetched_data['status']) {
- $return['vote_count'] = $fetched_data['vote_count'];
- } else {
- $return['vote_count'] = 0;
- }
- echo json_encode($return);
- //$query->close();
- $conn->close();
- }
- // ---------------------------------------------------------------
- // Private functions
- // ---------------------------------------------------------------
- function fetch_current_count($entry_id, $table_prefix, $conn){
- $return['status'] = "";
- $return['vote_count'] = "";
- $table_name = $table_prefix . '_entry';
- $select = $conn->query("SELECT * FROM $table_name WHERE entry_id = $entry_id");
- if ($select->num_rows > 0) {
- $return['status'] = true;
- while($row = $select->fetch_assoc()) {
- $return['vote_count'] = $row["vote_count"];
- }
- } else {
- $return['status'] = false;
- }
- return $return;
- }
- function update_count($entry_id, $table_prefix, $conn ){
- $table_name = $table_prefix . '_entry';
- $update = $conn->query("
- INSERT INTO $table_name( entry_id, vote_count )
- VALUES ( $entry_id, 1 )
- ON DUPLICATE KEY UPDATE vote_count = vote_count + 1
- ");
- if ($update) {
- return true;
- } else {
- return false;
- }
- }
- function fetch_date_voted($entry_id, $facebook_id, $table_prefix, $conn) {
- $table_name = $table_prefix . '_voter';
- $query = $conn->query("
- SELECT max(date_voted) FROM $table_name
- WHERE facebook_id=$facebook_id AND entry_id=$entry_id
- ");
- if ( $query ) {
- $row = $query->fetch_array(MYSQLI_NUM);
- if ( $row[0] ) {
- // do something
- return $row['date_voted'];
- }
- else {
- // do something else
- return "none";
- }
- } else {
- die('Error : ('. $conn->errno .') '. $conn->error);
- }
- return "none";
- }
- }
- // This will check if there is a POST data value
- // If there's a value, it will initialize then call the'updateDatabase'
- // then will pass the data of 'ENTRY ID' and FACEBOOK ID
- if( isset( $_POST['mode'] ) ) {
- switch( $_POST['mode'] ) {
- case 'update':
- if(isset( $_POST['entry_id'] ) && isset( $_POST['facebook_id'] ) && isset( $_POST['table_prefix'] ) ) {
- $updateClass = new db_connect();
- $entry_id = $_POST['entry_id'];
- $facebook_id = $_POST['facebook_id'];
- $table_prefix = $_POST['table_prefix'];
- $updateClass->updateDatabase($entry_id, $facebook_id, $table_prefix);
- } else {
- echo json_encode("Incomplete Parameters");
- }
- break;
- case 'fetch':
- if(isset( $_POST['entry_id'] ) && isset( $_POST['table_prefix'] ) ) {
- $retrieveClass = new db_connect();
- $entry_id = $_POST['entry_id'];
- $table_prefix = $_POST['table_prefix'];
- } else {
- echo json_encode("Incomplete Parameters");
- }
- $retrieveClass->fetchDatabase($entry_id, $table_prefix);
- break;
- }
- } else {
- echo json_encode("Please provide mode ( update or fetch )");
- }
- ?>
- $(function() {
- initOverlay();
- $('a.share-button').click(function (event){
- postToFeed();
- });
- // Get current votes
- var entry_id = $('.vote-button').attr('rel');
- console.log('------------------ '+entry_id);
- $.ajax({
- method: "POST",
- url: "https://url",
- data: {
- mode: 'fetch',
- table_prefix: 'photo',
- entry_id: entry_id
- },
- success: function(data) {
- //console.log(data[0]['status']);
- console.log( data );
- var val = JSON.parse(data);
- // console.log(val.status);
- if (val.status) {
- $('#vote-count').html( val.vote_count );
- // console.log("VOTE COUNT: " + val.vote_count);
- }
- },
- error: function(e){
- //console.log(e);
- }
- });
- // Here's the click function that will POST data using AJAX
- $('.vote-button').click(function(){
- //These 2 variables will get the entry id at the voter's facebook id
- var facebook_id = $("#facebook_id").val();
- // It will pass the variables on AJAX Call
- $.ajax({
- method: "POST",
- url: "https://url",
- data: {
- mode: 'update',
- table_prefix: 'photo',
- entry_id: entry_id,
- facebook_id: facebook_id
- },
- success: function(data) {
- //console.log(data[0]['status']);
- console.log( data );
- var val = JSON.parse(data);
- //console.log(val.status);
- if (val.status) {
- $('#vote-count').html( val.vote_count );
- setActiveLightbox(0);
- showLightBox(true);
- // console.log("VOTE COUNT: " + val.vote_count);
- } else if ( val.error_response == 'date_invalid' ) {
- setActiveLightbox(1);
- showLightBox(true);
- }
- },
- error: function(e){
- setActiveLightbox(2);
- showLightBox(true);
- }
- });
- })
- });
- strotime($last_vote_date) < strotime($current_date)
- (strtotime($last_vote_date) + (60*60*24)) < strtotime($current_date)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement