00:00
00:00
Newgrounds Background Image Theme

TheArmoredAvian just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Php: Voting System (from 1 To 5)

3,583 Views | 8 Replies
New Topic Respond to this Topic

Php: Voting System (from 1 To 5) 2006-02-15 00:59:12


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!

Response to Php: Voting System (from 1 To 5) 2006-02-15 01:30:40


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

Response to Php: Voting System (from 1 To 5) 2006-02-15 01:57:31


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

Response to Php: Voting System (from 1 To 5) 2006-02-15 15:53:09


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

Response to Php: Voting System (from 1 To 5) 2006-02-15 18:09:02


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.

Response to Php: Voting System (from 1 To 5) 2006-02-16 11:19:03


Pretty.

Seems good.

Response to Php: Voting System (from 1 To 5) 2006-02-16 17:08:23


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.


Merkd.com - It Pays to Play

Earn real money by betting and investing; or, sponsor, challenge, compete,

recruit, communicate, network, earn money playing games, and much more.

Response to Php: Voting System (from 1 To 5) 2006-02-16 20:16:46


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.

Response to Php: Voting System (from 1 To 5) 2008-09-04 19:48:42


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