-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.php
69 lines (63 loc) · 1.78 KB
/
ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
if(!isset($_POST["action"]))
exit;
require_once "config.php";
if($_POST["action"] == "getAllVotes")
{
extract($_POST);
$db->bindMore(array("user_id"=>$user_id));
$data = $db->query("SELECT actu_id,mot FROM votes WHERE user_id = :user_id LIMIT 50");
echo json_encode($data);
die();
}else if($_POST["action"] == "vote")
{
if(empty($_POST) || empty($_POST["actu_id"]) || empty($_POST["user_id"]) || empty($_POST["mot"]))
exit;
extract($_POST);
$db->bindMore(array("uid"=>$user_id,"aid"=>$actu_id,"mot"=>$mot));
$hasVoted = $db->query("SELECT COUNT(user_id) as c FROM votes WHERE user_id = :uid AND mot = :mot AND actu_id = :aid");
if(isset($hasVoted[0]) && (int)$hasVoted[0]["c"] != 1)
{
$res = $db->query("DELETE FROM votes WHERE user_id = :uid AND actu_id = :aid", array("uid"=>$user_id,"aid"=>$actu_id));
$res = $db->query("INSERT INTO votes(user_id,actu_id,mot) VALUES(:uid,:aid,:mot)", array("uid"=>$user_id,"aid"=>$actu_id,"mot"=>$mot));
if(!$res)
{
echo json_encode($data);
die();
}
else
{
echo json_encode($data);
die();
}
}else
{
$res = $db->query("DELETE FROM votes WHERE user_id = :uid AND actu_id = :aid", array("uid"=>$user_id,"aid"=>$actu_id));
if(!$res)
{
echo json_encode($data);
die();
}
else
{
echo json_encode($data);
die();
}
}
}else if($_POST["action"] == "getStat")
{
extract($_POST);
$db->bind("aid",$actu_id);
$res = $db->query("SELECT COUNT(user_id) as score,mot FROM votes WHERE actu_id = :aid GROUP BY mot");
echo json_encode($res);
die();
}else if($_POST["action"] == "getMore")
{
extract($_POST);
$lenombre = $number;
$db->bind("lenombre",(int)$number);
$posts = $db->query("SELECT titre, image, guid FROM actus ORDER BY guid DESC LIMIT 50 OFFSET :lenombre");
echo json_encode($posts);
die();
}
?>