Advertisement
Guest User

Untitled

a guest
Dec 29th, 2010
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. <?php
  2. include("config.php");
  3.  
  4. function getAllVotes($id)
  5.     {
  6.     /**
  7.     Returns an array whose first element is votes_up and the second one is votes_down
  8.     **/
  9.     $votes = array();
  10.     $q = "SELECT * FROM entries WHERE id = $id";
  11.     $r = mysql_query($q);
  12.     if(mysql_num_rows($r)==1)//id found in the table
  13.         {
  14.         $row = mysql_fetch_assoc($r);
  15.         $votes[0] = $row['votes_up'];
  16.         $votes[1] = $row['votes_down'];
  17.         }
  18.     return $votes;
  19.     }
  20.  
  21. function getEffectiveVotes($id)
  22.     {
  23.     /**
  24.     Returns an integer
  25.     **/
  26.     $votes = getAllVotes($id);
  27.     $effectiveVote = $votes[0] - $votes[1];
  28.     return $effectiveVote;
  29.     }
  30.  
  31. $id = $_POST['id'];
  32. $action = $_POST['action'];
  33.  
  34. //get the current votes
  35. $cur_votes = getAllVotes($id);
  36.  
  37. //ok, now update the votes
  38.  
  39. if($action=='vote_up') //voting up
  40. {
  41.     $votes_up = $cur_votes[0]+1;
  42.     $q = "UPDATE entries SET votes_up = $votes_up WHERE id = $id";
  43. }
  44. elseif($action=='vote_down') //voting down
  45. {
  46.     $votes_down = $cur_votes[1]+1;
  47.     $q = "UPDATE entries SET votes_down = $votes_down WHERE id = $id";
  48. }
  49.  
  50. $r = mysql_query($q);
  51. if($r) //voting done
  52.     {
  53.     $effectiveVote = getEffectiveVotes($id);
  54.     echo $effectiveVote." votes";
  55.     }
  56. elseif(!$r) //voting failed
  57.     {
  58.     echo "Failed!";
  59.     }
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement