Newgrounds.com — Everything, By Everyone.

Checking login status…

USERNAME:

PASSWORD:

Logging in…

Logged in as:
.
Logging out…
Inbox My Account Log Out


Forum Topic: score using php/mysql

(189 views • 7 replies)

This topic is 1 page long.

<< < > >>
None

Snubby

Reply To Post Reply & Quote

Posted at: 1/17/08 09:14 PM

Snubby FAB LEVEL 20

Sign-Up: 12/04/04

Posts: 3,123

Hey guys. I want to have registered users vote on the stuff on my website. But I don't know how to do that. I would want each user to only be able to vote once on each submission, so I would need to keep track of which users voted on what. I know how to do the average though, just have a score and number of votes record for each thing. but its the keeping track of who has voted that is stumping me.

Thanks.


None

Shadow00881

Reply To Post Reply & Quote

Posted at: 1/17/08 09:51 PM

Shadow00881 NEUTRAL LEVEL 06

Sign-Up: 08/07/06

Posts: 204

Just create a database, and when a user votes, have the database has a record of it the use has ever voted before. If they have, alert, or something:

<?php
//DB connect code here
function checkVote() { 
$query = mysql_query("SELECT FROM table WHERE username='$user' AND game='$game'");
//Count the rows, if count is greater than 1, echo an error, else, insert into database table
$numrows = mysql_num_rows($query);
if ($numrows > 1) { 
 echo 'You've already voted on this game!';
} else {
 $dbinsert = mysql_query("INSERT INTO table(`username`, `game`) VALUES('$username', '$game')");
 if (!dbinsert) {
  echo '<script>alert("FATAL ERROR!");</script>
 } else {
 echo 'Thank you! You have been inserted into the database!';
}
}
?>

That should work, Note! There are some empty variables that need to be defined!

For the login, search Google for php/mysql login.


None

Snubby

Reply To Post Reply & Quote

Posted at: 1/17/08 10:10 PM

Snubby FAB LEVEL 20

Sign-Up: 12/04/04

Posts: 3,123

I don't think that's even close to what I need. I need to keep track of who has voted on what. Your query just checked if the username was something, and if the game was some game. It didn't really do anything at all.


None

Shadow00881

Reply To Post Reply & Quote

Posted at: 1/17/08 11:49 PM

Shadow00881 NEUTRAL LEVEL 06

Sign-Up: 08/07/06

Posts: 204

it checks to see if the user has already voted on the game


None

Shadow00881

Reply To Post Reply & Quote

Posted at: 1/17/08 11:50 PM

Shadow00881 NEUTRAL LEVEL 06

Sign-Up: 08/07/06

Posts: 204

BTW, sorry, i forgot a closing bracket at the end


None

Snubby

Reply To Post Reply & Quote

Posted at: 1/18/08 12:08 AM

Snubby FAB LEVEL 20

Sign-Up: 12/04/04

Posts: 3,123

so I would need a table with every member and every game? like i could stick every thing i want to rate as a new field into my members table, but then it could end up having hundreds of fields. and vice versa, i could have each user as a feild in each content table, but then im still looking at hundreds of fields. is that the only way of doing it?


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 1/18/08 12:14 AM

Afro-Ninja EVIL LEVEL 35

Sign-Up: 03/02/02

Posts: 13,073

At 1/18/08 12:08 AM, Snubby wrote: so I would need a table with every member and every game? like i could stick every thing i want to rate as a new field into my members table, but then it could end up having hundreds of fields. and vice versa, i could have each user as a feild in each content table, but then im still looking at hundreds of fields. is that the only way of doing it?

you have a users table, and a games table, I'm assuming. So you make a third table- user_game_votes

whenever a user votes on something, add an entry to the user_game_vote table. It will basically consist of four values- id (unique auto_increment), user id, game id, and vote(whatever they rated the game).

before you let someone vote on the game, select count from user_game_vote where userid = their id and game id = the current game's id. If a row is returned then they have already voted, so they can't. Otherwise, add the new row

to calculate score for a game, select all rows from the user_game_vote table where game id= the current game. Add them up an divide by the total rows for your average, and there you go

that's the basic idea

BBS Signature

None

Snubby

Reply To Post Reply & Quote

Posted at: 1/18/08 07:57 AM

Snubby FAB LEVEL 20

Sign-Up: 12/04/04

Posts: 3,123

Thanks man that's perfect.


All times are Eastern Daylight Time (GMT -4) | Current Time: 11:26 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!