frijole Posted March 5, 2008 Share Posted March 5, 2008 does anyone know how to go about doing an up/down voting system with PHP? do I need to use Java or something else? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 5, 2008 Share Posted March 5, 2008 to answer your question, "do I need to use Java or something else?" no. you can do it with PHP. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 5, 2008 Share Posted March 5, 2008 like a thumbs up/thumbs down system? if you are limiting a person to 1 vote per account make a table Votes ItemID (BigInt) UserID (BigInt) Vote (Bool) then when a person votes simply have the value pass a 0 or 1 and then use the query <?php $item = $_GET['ItemId']; $userid = $_SESSION['UserID']; $vote = $_GET['vote']; $q = "Select vote from `votes` where ItemId = '".$itemid."' and UserId = '".$userid."'"; $r = mysql_query($q) or die(mysql_error()); if(mysql_num_rows($r) >0){ $q = "Update `votes` set vote = '".$vote."' where ItemId = '".$itemid."'" and UserId = '".$userid."'"; } else{ $q = "insert into `votes` vote, ItemID, UserID VALUES('".$vote."', '".$itemid."', '".$userid."')"; } mysql_query($q) or die(mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
frijole Posted March 5, 2008 Author Share Posted March 5, 2008 that was fast! well done. In this system how would I keep track of the total votes for a certain comment for others to see? Quote Link to comment Share on other sites More sharing options...
frijole Posted March 5, 2008 Author Share Posted March 5, 2008 I am confused about: Where does the true/false vote come from? A button? $vote = $_GET['vote']; And, in order to make sure someone has not voted on the same item before would you query the votes table and do something like: SELECT vote FROM votes WHERE itemid = '$itemid' && userid = '$userid' and then. If ($vote != 1) // execute vote and add to database else // "you have already voted on this item!" does that make sense? Quote Link to comment Share on other sites More sharing options...
frijole Posted March 5, 2008 Author Share Posted March 5, 2008 Can anyone show me an example of an input that would give the $_GET['vote'] value? i.e. what is on the page that a user interacts with in order to vote? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 5, 2008 Share Posted March 5, 2008 you can pass the vote any way you want it just needs to be a 1 for thumbs up or 1 for thumbs down in my example. you can use ajax a form a link etc. to recall the results you can say select Sum(Vote) as Vote Total, Count(Vote) as Count from `votes` where ItemID = $itemid; and then you have the total of the 0s and 1s combined and number of votes so if the sum = 50 and you have 80 votes you have 50 thumbs up 30 thumbs down Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.