Topic: Contest based on number of times page is accessed
*Seperate from punbb
Okay, I am creating a contest where the winner will be #x viewer to the page. But.. it gets a bit more complicated than that.
For example let's say:
- This page is accessed from another page using a form
- People can attempt at winning every 24 hours
- People are registered users
- The 100th person is the winner
Now I believe I would need two tables to do this. A users table and a contest table. The contest table would create a new row that includes the username, ip adress, and current time, only if 24 hours have passed, since the last time that user had created a row.
Now I know my code is completely wrong on this one. I do plan on rewriting it. Don't focus on the code too much. The comments in it are okay, I am mainly looking for suggestions on how to go about creating this little script.
<?php
//Get the required db connection info
require 'config.php';
//Make sure they use the form to access this page
if (isset($_POST['submit'])
{
//Query the database for the user viewing this page
$query = 'SELECT * FROM users WHERE username='.$username;
$result = mysql_query($query) or die ('Could not get username:' . mysql_error());
//If the field last_try is not empty and the current time since that field has been modified is greater than 24 hours we update
if (!empty($result['last_try'] && $result['last_try'] >= //24 HOURS????)
{
}
//If last_try is empty we will insert the current time
if (empty($result['last_try'])
{
$query = 'INSERT INTO users (last_try) VALUES (NOW())';
$result = mysql_query($query) or die ('Could not insert last try:' . mysql_error());
}
//Query the database for all last_try values
$query = 'SELECT * FROM users';
$result = mysql_query($query) or die ('Could not get users:' . mysql_error());
//If there are the right number of attempts at the challenge, the user wins
if (num_rows($result['last_try']) == $win)
{
function win;
}
//If there are not the right number of attempts, the user does not win
if(num_rows($result['last_try'] != $win)
{
function loss;
}
}
//If this page was not accessed using the form on previous page then give an error message
else
{
echo 'This page must be accessed from the '.$pagename.' page';
}
?>