php scoring system
I'm in a bit of a pickle at the minute, I'm new to PHP and I need some help.
I put a scoring system on my site so that when a user logs in, they gain points and when they gain a certain amount of points they go up a level and gain access to more features. It's kind of like newgrounds' only I have one little problem. I only want them to get points for logging in twice a day. And I want them to get points for completing competitions, and I only want them to get points once for finishing the competition (they have to click a link to get the points for the comp). Speaking of which, how do give them points for clicking links?
This post is suitable for vegetarians
IanD posted this at 21:40 — 22nd June 2005.
They have: 222 posts
Joined: Sep 1999
I'm going to assume you're using a MySQL database, PHP5, the MySQLi extension and OO notation. I'd add Points (SMALLINT unsigned) and Rank (TINYINT unsigned) fields to your accounts table. A rank of 0 could mean no access to anything, 1 would mean access to some things, 2 more things, etc.
Write a function to update the points of an account. It would need to take a username (or ID, whichever you're using) and the number of points to increment as parameters. So, something like
<?php
function AddPoints($username, $increment)
{
global $databaseLink;
// Increment Points. \"accounts\" is name of db table w/ account info, \"points\" is name of field w/ point info
$result = $databaseLink->query(\"SELECT points FROM accounts WHERE username=$username\");
$row = $result->fetch_array(MYSQLI_ASSOC);
$currentPoints = $row['points'];
$result->close();
$currentPoints += $increment
$databaseLink->query(\"UPDATE accounts SET points=$currentPoints WHERE username=$username\");
// Update Rank
switch ($currentPoints)
{
case ($currentPoints >= 0 && $currentPoints < 100):
$databaseLink->query(\"UPDATE accounts SET rank=1 WHERE username=$username\");
break;
case ($currentPoints >= 100 && $currentPoints < 200):
$databaseLink->query(\"UPDATE accounts SET rank=2 WHERE username=$username\");
break;
case ($currentPoints >= 200):
$databaseLink->query(\"UPDATE accounts SET rank=3 WHERE username=$username\");
break;
}
}
?>
On the page that contains each feature, you'd check their rank and display the feature or not accordingly.
The points for logging in twice a day can be handled several ways depending on what you want? Assuming you only care about twice a day, I'd have a field in my accounts table w/ the timestamp from the last time they logged in. Each time they log in, check that and if it was within 24 hours, then add points.
When they complete a competition, call the addpoints fuction w/ however many points you want to add.
You'll have to use a JavaScript to detect clicking on the link, or just have a script on the page that was linked to.
To prevent people from taking competitions more than once, I'd have a CompetitionResults table in the database listing the ID number of the competition, and the username of the person who completed it. Only add to the table when someone completes a competition. Then you can check against it before letting someone take a competition
Fighting for a Lost Cause.net
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.