Example #1
0
<?php

require '../wp-load.php';
if (isset($current_user) && $current_user->ID > 0) {
    if (!isset($_REQUEST['comment_id']) || !isset($_REQUEST['comment_vote'])) {
        echo "errorparam";
    } else {
        if ($_REQUEST['action_type'] != 'changehelpful') {
            //Add new
            $comment_id = $_REQUEST['comment_id'];
            $comment_vote = $_REQUEST['comment_vote'];
            $data = helpfulVote($comment_id, $comment_vote);
            if ($data > 0) {
                $userdat = addUserVote($current_user->ID, $comment_id, $comment_vote);
                if ($userdat > 0) {
                    echo $comment_vote . "-success";
                } else {
                    echo $comment_vote . "-success-none";
                }
            } else {
                echo "error";
            }
        } else {
            //Alter Vote
            $comment_id = $_REQUEST['comment_id'];
            $comment_vote = $_REQUEST['comment_vote'];
            $data = helpfulVote($comment_id, $comment_vote, "change");
            $changeUserVote = alterUserVote($current_user->ID, $comment_id, $comment_vote);
            if ($changeUserVote > 0) {
                echo $comment_vote . "-success";
            } else {
Example #2
0
function voteForCard($id, $username)
{
    if (checkVotes($username)) {
        try {
            $con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
            $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "SELECT votes FROM card WHERE id =:id";
            $stmt = $con->prepare($sql);
            $stmt->bindValue("id", $id, PDO::PARAM_STR);
            $stmt->execute();
            $votes = $stmt->fetchColumn(0);
            $votes = $votes + 1;
            $sql = "UPDATE card SET votes = :votes WHERE id = :id";
            $stmt = $con->prepare($sql);
            $stmt->bindValue("id", $id, PDO::PARAM_STR);
            $stmt->bindValue("votes", $votes, PDO::PARAM_STR);
            $stmt->execute();
            addUserVote($id, $username);
            //            echo "{\"error\": [{ \"type\": \"alert\", \"votes\":\"$votes\"}]}";
            echo "{\"votes\":{$votes}}";
        } catch (PDOException $e) {
            echo json_encode($e->getMessage());
        }
    } else {
        echo "{\"votes\":{$votes}}";
        echo "{\"error\": [{ \"type\": \"alert\", \"msg\":\"You are out of votes.\"}]}";
    }
}