Link to home
Start Free TrialLog in
Avatar of Chocula
Chocula

asked on

Simple Rating Script

Hi,

What I am wanting is a simple rate up or rate down script that only allows users to vote once.

I have created the page (see picture and script) which has an up and a down image and also counts how many votes there are.

The Up/Down links to this page however it doesn't update or decrease:

<?
include 'admin/config.php';

$FeedItemID = $_GET['feed'];
$voted = $_GET['voted'];

$sql = 'UPDATE FeedbackScore SET Score = +$voted WHERE FeedItemID = '$FeedItemID'';
 
?>

What i also want to include in this some code so that users can only vote once. I was thinking i could achieve this by having this table:

FeedbackScoreUser
  FeedUserSID
  FeedItemID
  StudentID

and adding to this table when the rating is updated. The student ID is store in the session so to get to it:

$username = $_SESSION['username'];

Hope that all makes sense, i'm extremely tired!
/// add / view votes
 
<?php 
 
 
include 'admin/config.php';
 
 
$FeedItemID = $_GET['feed'];
 
 
echo '<div><p>';
echo '<a href="updateScore.php?voted=1&feed='.$FeedItemID.'"><img src="images/vote_yes.gif" alt="Vote Up" width="13" height="13" /></a>';
echo '   ';
echo '<a href="updateScore.php?voted=-1&feed='.$FeedItemID.'"><img src="images/vote_no.gif" alt="Vite Down" width="13" height="13" /></a>';
echo '</p>';
 
 
 
function get_row($query) {
	   $result = mysql_query($query);
	   $row = mysql_fetch_array($result);
	    return($row);
	}
 
    $score = get_row("SELECT Score
FROM FeedbackScore
WHERE FeedItemID = '$FeedItemID'");
 
 
 echo '<div id="bg">| <b>'.$score['Score'].'</b></div>'; ?>

Open in new window

rating.gif
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chocula
Chocula

ASKER

Well its not a problem adding it all into one page to do the update, i just don't know the code to increment/decrease + add in.
Avatar of Chocula

ASKER

To check whether the user has already rated i guess the code could be something like this:

$username = $_SESSION['username'];

$query = mysql_query("SELECT * FROM FeedbackScoreUser
                                      WHERE FeedItemID = '$FeedItemID'
                                      AND StudentID = '$username'")

if (mysql_num_rows($query) == 0) {
echo '// the rating controls";
}
else {
echo 'You have already rated, thank you';
}


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chocula

ASKER

you got it exactly.

I had in mind the table would look like this (example data included)

FeedbackScore
  FeedScoreID (PK)
  FeedItemID (e.g the questionID you mentioned earlier) (FK)
  UserID
  Score (just wanted to increment it or decrement it by 1)

The reason I thought to have FeedScoreID (auto_increment) is because the same Feedback item (FeedItemID) will be able to be voted on by multiple people (but only one time per person)

Avatar of Chocula

ASKER

* i didn't include the example data

+-----------------+-----------------+-----------+-----------+
| FeedScoreID  |  FeedItemID  |  UserID  |   Score   |
+-----------------+-----------------+-----------+-----------+
|          1            |          3            | abc345  |      1        |  
|          2            |          3            | ssd345  |      -1       |
|          3            |          3            | ssd345  |      -1       |  



looking at that should who has rated be stored differently to the score? So you would just have one row for each of the feedback items with one score
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chocula

ASKER

yeah, i forgot to change the last userID. I'm still very unsure how to continue on the design though...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chocula

ASKER

yeah i am familiar with digg. I was planning on having an iframe with this in and it would be attached to each comment. I am just trying to get my head around the coding to complete those steps..
Tried to give the best advice based on the trajectory of the question.  Hope it was helpful, ~Ray