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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.