Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. <?php
  2.  
  3. //DB-varaibles
  4. $dsn = "mysql:dbname=dbirkemalm_com;host=dbirkemalm.com.mysql;";
  5. $username = "dbirkemalm_com";
  6. $password ="8ejxQ4Pn";
  7. $db_name = "dbirkemalm_com";
  8. $vote_table = 'votes';
  9.  
  10. //Variables
  11. $ip = $_SERVER['REMOTE_ADDR'];
  12. $vote = $_GET['id'];
  13.  
  14. //Connect to DB
  15. try {
  16.     $dbh = new PDO($dsn, $username, $password);
  17.     }   catch (PDOException $e) {
  18.     echo 'Connection failed: ' . $e->getMessage();
  19. }
  20.  
  21. $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  22.  
  23. //Check if IP exists in DB
  24. $ipsql = "SELECT * FROM $vote_table WHERE ip = :ip";
  25. $ipq = $dbh->prepare($ipsql);
  26. $ipq->bindParam(':ip', $ip, PDO::PARAM_STR);
  27. $ipq->execute();
  28. $ipexist = $ipq->rowCount();
  29.  
  30. if($ipexist != 1){
  31.    
  32. //Insert vote and IP
  33. $ipinsql = "INSERT INTO $vote_table (ip, vote) VALUES(:ip, :vote)";
  34. $ipinsq = $dbh->prepare($ipinsql);
  35. $ipinsq->bindParam(':ip', $ip, PDO::PARAM_STR);
  36. $ipinsq->bindParam(':vote', $vote, PDO::PARAM_STR);
  37. $ipinsq->execute();
  38.  
  39. }
  40. //Total number of votes
  41.  
  42. $totalsql = "SELECT * FROM $vote_table";
  43. $totalq = $dbh->prepare($totalsql);
  44. $totalq->execute();
  45. $total = $totalq->rowCount();
  46.  
  47.  
  48. //Get number of A-votes
  49. $bar1sql = "SELECT * FROM $vote_table WHERE vote = 'A'";
  50. $bar1q = $dbh->prepare($bar1sql);
  51. $bar1q->execute();
  52. $bar1count = $bar1q->rowCount();
  53.  
  54. //Get number of B-votes
  55. $bar2sql = "SELECT * FROM $vote_table WHERE vote = 'B'";
  56. $bar2q = $dbh->prepare($bar2sql);
  57. $bar2q->execute();
  58. $bar2count = $bar2q->rowCount();
  59.  
  60. //Get number of C-votes
  61. $bar3sql = "SELECT * FROM $vote_table WHERE vote = 'C'";
  62. $bar3q = $dbh->prepare($bar3sql);
  63. $bar3q->execute();
  64. $bar3count = $bar3q->rowCount();
  65.  
  66. //Get number of D-votes
  67. $bar4sql = "SELECT * FROM $vote_table WHERE vote = 'D'";
  68. $bar4q = $dbh->prepare($bar4sql);
  69. $bar4q->execute();
  70. $bar4count = $bar4q->rowCount();
  71.  
  72.  
  73. //Mathematics bar 1
  74. $bar1length = abs($bar1count/$total*100);
  75.  
  76. //Mathematics bar 2
  77. $bar2length = abs($bar2count/$total*100);
  78.  
  79. //Mathematics bar 3
  80. $bar3length = abs($bar3count/$total*100);
  81.  
  82. //Mathematics bar 4
  83. $bar4length = abs($bar4count/$total*100);
  84.  
  85. $totals = json_encode(array("total"=>$total, "bar1"=>$bar1length, "bar2"
  86. =>$bar2length, "bar3"=>$bar3length, "bar4"=>$bar4length));
  87. echo $totals;
  88.  
  89.  
  90. /* Testing count
  91. echo "Total: ",$total, "<br />";
  92. echo "Bar 1: ",$bar1count, "<br />";
  93. echo "Bar 2: ",$bar2count, "<br />";
  94. echo "Bar 3: ",$bar3count, "<br />";
  95. echo "Bar 4: ",$bar4count, "<br />";
  96. */
  97. //print_r($array);
  98. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement