Chciałbym ograniczyć możliwość głosowania do jednego. Teraz skrypt działa tak, że głosować można bez końca.

glosowanie.php
  1. <?php
  2. require_once('connect.php');
  3. require_once('functions.php');
  4. ?>
  5. <?php
  6.  
  7.  
  8. function getAllVotes($id)
  9. {
  10. /**
  11. Returns an array whose first element is votes_up and the second one is votes_down
  12. **/
  13. $votes = array();
  14. $q = "SELECT * FROM shity WHERE id = $id";
  15. $r = mysql_query($q);
  16. if(mysql_num_rows($r)==1)//id found in the table
  17. {
  18. $row = mysql_fetch_assoc($r);
  19. $votes[0] = $row['votes_up'];
  20. $votes[1] = $row['votes_down'];
  21. }
  22. return $votes;
  23. }
  24.  
  25. function getEffectiveVotes($id)
  26. {
  27. /**
  28. Returns an integer
  29. **/
  30. $votes = getAllVotes($id);
  31. $effectiveVote = $votes[0] - $votes[1];
  32. return $effectiveVote;
  33. }
  34.  
  35. $id = $_POST['id'];
  36. $action = $_POST['action'];
  37.  
  38. //get the current votes
  39. $cur_votes = getAllVotes($id);
  40.  
  41. //ok, now update the votes
  42. if (isset($_SESSION['user_id']) and isset($_SESSION['login'])){
  43. if($action=='vote_up') //voting up
  44. {
  45. $votes_up = $cur_votes[0]+1;
  46. $q = "UPDATE shity SET votes_up = $votes_up WHERE id = $id";
  47. }
  48. elseif($action=='vote_down') //voting down
  49. {
  50. $votes_down = $cur_votes[1]+1;
  51. $q = "UPDATE shity SET votes_down = $votes_down WHERE id = $id";
  52. }
  53. }
  54. $r = mysql_query($q);
  55. if($r) //voting done
  56. {
  57. $effectiveVote = getEffectiveVotes($id);
  58. echo $effectiveVote." votes";
  59. }
  60. elseif(!$r) //voting failed
  61. {
  62. echo "Zaloguj się!";
  63.  
  64. }
  65.  
  66. ?>


index.php

  1. <?php
  2. require_once('connect.php');
  3. require_once('functions.php');
  4. ?>
  5.  
  6. <html>
  7. <head>
  8. <title>Votes</title>
  9.  
  10. <style type='text/css'>
  11.  
  12.  
  13.  
  14.  
  15. .entry {
  16. width: 710px;
  17. background: #ffffff;
  18. padding:8px;
  19. border:1px solid #bbbbbb;
  20. margin:5px auto;
  21. -moz-border-radius:8px;
  22. }
  23.  
  24. span.link a {
  25. font-size:150%;
  26. color: #000000;
  27. text-decoration:none;
  28. }
  29.  
  30. a.vote_up, a.vote_down {
  31. display:inline-block;
  32. background-repeat:none;
  33. background-position:center;
  34. height:16px;
  35. width:16px;
  36. margin-left:4px;
  37. text-indent:-900%;
  38. }
  39.  
  40. a.vote_up {
  41. background:url("images/thumb_up.png");
  42. }
  43.  
  44. a.vote_down {
  45. background:url("images/thumb_down.png");
  46. }
  47. </style>
  48.  
  49. <script type='text/javascript' src='jquery.pack.js'></script>
  50. <script type='text/javascript'>
  51. $(function(){
  52. $("a.vote_up").click(function(){
  53. //get the id
  54. the_id = $(this).attr('id');
  55.  
  56. // show the spinner
  57. $(this).parent().html("<img src='images/spinner.gif'/>");
  58.  
  59. //fadeout the vote-count
  60. $("span#votes_count"+the_id).fadeOut("fast");
  61.  
  62. //the main ajax request
  63. $.ajax({
  64. type: "POST",
  65. data: "action=vote_up&id="+$(this).attr("id"),
  66. url: "glosowanie.php",
  67. success: function(msg)
  68. {
  69. $("span#votes_count"+the_id).html(msg);
  70. //fadein the vote count
  71. $("span#votes_count"+the_id).fadeIn();
  72. //remove the spinner
  73. $("span#vote_buttons"+the_id).remove();
  74. }
  75. });
  76. });
  77.  
  78. $("a.vote_down").click(function(){
  79. //get the id
  80. the_id = $(this).attr('id');
  81.  
  82. // show the spinner
  83. $(this).parent().html("<img src='images/spinner.gif'/>");
  84.  
  85. //the main ajax request
  86. $.ajax({
  87. type: "POST",
  88. data: "action=vote_down&id="+$(this).attr("id"),
  89. url: "votes.php",
  90. success: function(msg)
  91. {
  92. $("span#votes_count"+the_id).fadeOut();
  93. $("span#votes_count"+the_id).html(msg);
  94. $("span#votes_count"+the_id).fadeIn();
  95. $("span#vote_buttons"+the_id).remove();
  96. }
  97. });
  98. });
  99. });
  100. </script>
  101.  
  102. </head>
  103. <body>
  104.  
  105. <?php
  106.  
  107. /**
  108. Display the results from the database
  109. **/
  110. $q = "SELECT * FROM baza";
  111. $r = mysql_query($q);
  112.  
  113. if(mysql_num_rows($r)>0): //table is non-empty
  114. while($row = mysql_fetch_assoc($r)):
  115.  
  116. $net_vote = $shit['votes_up'] - $shit['votes_down']; //this is the net result of voting up and voting down
  117.  
  118. ?><?php endwhile; ?>
  119.  
  120. <div class='entry'>
  121.  
  122. <span class='link'>
  123. <a href='<?php echo $row['link']; ?>'> <?php echo $row['title']; ?> </a>
  124. </span>
  125.  
  126. <span class='votes_count' id='votes_count<?php echo $shit['id']; ?>'><?php echo $net_vote." votes"; ?></span>
  127.  
  128. <span class='vote_buttons' id='vote_buttons<?php echo $shit['id']; ?>'>
  129. <a href='java script:;' class='vote_up' id='<?php echo $shit['id']; ?>'>Vote Up!</a>
  130. <a href='java script:;' class='vote_down' id='<?php echo $shit['id']; ?>'>Vote Down!</a>
  131. </span>
  132.  
  133. </div>
  134.  
  135.  
  136. <?php
  137.  
  138. endif;
  139.  
  140. ?>
  141.  




Tylko zalogowany może głosować, jak teraz zrobić, aby sprawdzało, zapisywało adres IP osoby, która glosowala w danym artykule, oraz cookies? i zabraniała ponownego glosowania?