Jump to content

comment voting system


frijole

Recommended Posts

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());
?>

 

Link to comment
https://forums.phpfreaks.com/topic/94549-comment-voting-system/#findComment-484126
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/94549-comment-voting-system/#findComment-484139
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/94549-comment-voting-system/#findComment-484224
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.