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.
wink


<?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';
    }
    
?>

Re: Contest based on number of times page is accessed

A couple questions
- where is $username set?
- why not simply keep a counter, increment it, and decide what to show based on the value of that?
- is this seperate from PunBB or does it use PunBB's user system

Re: Contest based on number of times page is accessed

It is as of now seperate, I think I will integrate it into punbb though.
It will be on a site with no forums or things of such nature. I do however quite like a lot of punbb's source so I may do it that way. Any help / advice would be great on this matter.

Re: Contest based on number of times page is accessed

Some fundamentally flawed stuff. A query doesn't return an array of fields, mysql_fetch_assoc() does.