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
PHPMySQL ServerSQL

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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.
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
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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)

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
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
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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..
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

Tried to give the best advice based on the trajectory of the question.  Hope it was helpful, ~Ray