Be a Supporter!

Php: Voting System (from 1 To 5)

  • 2,428 Views
  • 9 Replies
New Topic Respond to this Topic
Greeley
Greeley
  • Member since: Aug. 30, 2005
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Php: Voting System (from 1 To 5) 2006-02-15 00:59:12 Reply

PHP: Voting System (From 1 to 5)

Back to: PHP: Main

You should know:
You should already know HTML/XHTML and have the most basic of an understanding of PHP and how it works. If you need to learn any of these, just head over to http://www.w3schools.com

Very recomended you know mysql or phpMyAdmin and fairly well

The voting panel

First we need to make clickable numbers that allows the users to click and vote. You can make the numbers using image links but for simplicity sake, we'll use text links in this. So here is how you'll create the numbers in html. Replace all [ with < and ] with >

[a href="website.com/processvote.php?vote=1"]
1[/a]
[a href="website.com/processvote.php?vote=2"]
2[/a]
[a href="website.com/processvote.php?vote=3"]
3[/a]
[a href="website.com/processvote.php?vote=4"]
4[/a]
[a href="website.com/processvote.php?vote=5"]
5[/a]

Okay so now we've got that created. I'll explain the reason for why this is the way it is when we create the processvote.php.

MySQL Table
I'm going to assume you use phpMyAdmin or know how to create mySQL tables in mySQL. All we really need is a column for is the current score (call it current_score) and an autoincriment (called id) incase you want to create a voting panel for more than one piece of content on your site. Have it's default set to 0. Everything starts at 0 and when people vote, it'll work it's way up.

The processvote.php file
So now let's create the processvote.php file.

<?php

// connect to your database
include('dbconnect.php');

// for starters lets use $_GET to get the number in the URL that person voted. For example, we are getting the number represented by x:
[a href="website.com/processvote.php?vote=x]1
[/a]
Replace x with the same number that the person was to click in the html code. So here goes nothing

$vote = $_GET['vote'];
// we just got the value that's in the URL that's called vote like I have already stated

// now let's check to see if it's a number
if(!is_numeric($vote)){
echo 'Vote must be a number';
die();
}

// okay, now let's make sure that it's equal to or between 1 and 5
if($vote < 1 || $vote > 5){
echo 'You can only vote between 1 and 5!';
die();
}

// now let's get the current score from the datbase and average it with new vote. Let's assume that the id in the database for this particular voting panel is 1.

$currentscore = mysql_fetch_array(mysql_query("SELECT * FROM table WHERE id='1'"));
$currentscore = $currentscore['current_score'];
$newscore = ($currentscore + $vote) / 2;

// now let's submit it into the database.

$submitscore = mysql_query("UPDATE table SET current_score=$newscore WHERE id='1'");

?>

Displaying the score

Now the easy part. Displaying the score.
<?php
$score = mysql_query("SELECT score FROM table WHERE id='1'");
echo 'Score: '.number_format($score, 2).'%';
?>

And badabing... there you have it. Hope you've learned something. This is a little sloppy but i was just giving it a shot!

Shaun
Shaun
  • Member since: Jan. 1, 2002
  • Offline.
Forum Stats
Member
Level 42
Blank Slate
Response to Php: Voting System (from 1 To 5) 2006-02-15 01:30:40 Reply

At 2/15/06 12:59 AM, Greeley wrote: $newscore = ($currentscore + $vote) / 2;

the maths here seems a little off :/
that would take an average of the current score and the vote..
but the vote shouldnt have that much effect should it?


// Sig Makers // WWE Fans // Tumblr //

BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to Php: Voting System (from 1 To 5) 2006-02-15 01:57:31 Reply

yeah, I noticed that part too, the current score isn't an average of the currentscore and the vote, it's an average of all past scores and the current vote which isn't hard to code, on the ng system it's pretty much the same thing only it also saves voting power taken

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Member
Level 51
Blank Slate
Response to Php: Voting System (from 1 To 5) 2006-02-15 10:51:01 Reply

And you might want to use exit; instead of die(). They both do the same, but since you're outputting the error already with echo there's no need for die. Exit will just do fine. :)
For the rest it looks nice, basic, but nice.

fuzz
fuzz
  • Member since: Jun. 2, 2004
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to Php: Voting System (from 1 To 5) 2006-02-15 15:53:09 Reply

Thank you!! so so so much!!!
i realy needed this as you probablly saw

Greeley
Greeley
  • Member since: Aug. 30, 2005
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to Php: Voting System (from 1 To 5) 2006-02-15 18:09:02 Reply

At 2/15/06 01:57 AM, Inglor wrote: yeah, I noticed that part too, the current score isn't an average of the currentscore and the vote, it's an average of all past scores and the current vote which isn't hard to code, on the ng system it's pretty much the same thing only it also saves voting power taken

Yeah I knew it was a little off but it works. If you were to have voting power it'd be pretty simple. Say someone had a voting power of 5 you'd do something like this:

$votingpower = 5;
$newscore = ($vote + ($currentscore * $votingpower)) / $votingpower

I think it's a little complicated to do a straight up average of the entires. So I'm just going to stick to it being like this.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Voting System (from 1 To 5) 2006-02-16 11:19:03 Reply

Pretty.

Seems good.

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to Php: Voting System (from 1 To 5) 2006-02-16 17:08:23 Reply

You should make the script a lot more dynamic. I'd suggest cleaning up the URL so people can't just send the link to 100 people and mass vote without even going to the entry page, plus you need a dynamic ID locator.

Greeley
Greeley
  • Member since: Aug. 30, 2005
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to Php: Voting System (from 1 To 5) 2006-02-16 20:16:46 Reply

At 2/16/06 05:08 PM, Pilot-Doofy wrote: You should make the script a lot more dynamic. I'd suggest cleaning up the URL so people can't just send the link to 100 people and mass vote without even going to the entry page, plus you need a dynamic ID locator.

Yeah, I could but I was just covering the basics. You could do alot of things like only let them vote when they are logged in or log there ip address so they can only vote once a day. I wasn't worrying too much about that stuff in this tutorial. That could come in later tutorials by someone... who knows.

DarkFireball
DarkFireball
  • Member since: Sep. 11, 2001
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to Php: Voting System (from 1 To 5) 2008-09-04 19:48:42 Reply

Old but I need to bump this.

I have voting code that works, but I'm not sure how to save the user's IP so he can only vote once a day.

First problem, I dont know how to do any time functions on databases so Im not sure how I can tell the server to reset this variable ever 24 hours.
And I also dont know how I should go about logging an IP.

An IP can be logged as a field inside that particular submission's row in the table, but what about multiple users? Can I store an array inside one field of a table, then have it check for that IP? I honestly have thought about this a while but im not sure at all how to go about doing it. Seems a submission's field in the table would fill up REALLY quick


WARHAMMER ONLINE SCENARIO TRICK
Earn THOUSANDS more experience!!
http://www.progressiongames.com