PHP results list

Discussion in 'PHP' started by kostas11, Dec 12, 2007.

  1. #1
    Hi to all,

    I want to implement a function in PHP however I really don't know where to start.
    Basically all I need is a textbox that the user inputs his username and a button which when the user clicks should get the time/date and post the results/times in a scoreboard like list.

    For example: user 1 has completed the task at 19:00 12/12
    user 2 has completed the task at 20:02 12/12

    user 1 is the winner.

    I am thinking that probably when the user has submitted his username and clicked the button his time will be written to an external file and along with other user times will be displayed as a list, 1st 2nd etc.. sort the 10 best times

    All I need is to know when was the exact time that someone has completed the task and his time to be displayed in a list with other users times.

    Has anyone implemented something like this before or knows a good tutorial I would be grateful.

    Thank you for your time,
    Any help is greatly appreciated.
     
    kostas11, Dec 12, 2007 IP
  2. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you have access to a mysql database it will be very easy to do, simply insert a row when the form is submitted (button) clicked that logs the users name and logs the time using time(). Then you simply call the database using "select * from results order by time desc limit 10"
     
    Gawk, Dec 12, 2007 IP
  3. kostas11

    kostas11 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Gawk,

    This is not so simple for me.. would you be interested to do this of course get paid as well. Yes I have access to a mysql database. Please let me know if you could help me with this.

    Thanks
     
    kostas11, Dec 12, 2007 IP
  4. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes I can help you, I'll do it in stages but they will be very raw and you will need to make them fit your pages...

    Stage 1) Create the page which holds the form to submit, here is a basic form code...

    <form action="record.php" method="post">
    <input type="text" name="username">
    <input type="submit" value="Submit">
    <input type="hidden" name="action" value="record_time">
    </form>
     
    Gawk, Dec 12, 2007 IP
  5. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Stage 2) Create a web page called record.php - I'll give you some code to put in this in a minute
     
    Gawk, Dec 12, 2007 IP
  6. kostas11

    kostas11 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I really cant express my gratitude with words. Thank you so much.

    I really, really appreciate it.
     
    kostas11, Dec 12, 2007 IP
  7. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Put this code in the record.php...

    
    <?
    define('db_host', 'your_mysql_host');
    define('db_user', 'mysql_username');
    define('db_pass', 'mysql_password');
    define('db_database', 'mysql_database');
    $db = mysql_connect(db_host, db_user, db_pass);
    mysql_select_db(db_database);
    
    if ($_POST["action"] == "record_time") {
    	$sql = "insert into results set username = \"" . addslashes(strip_tags(htmlspecialchars($_POST["username"]))) . "\", whattime = \"" . time() . "\"";
    	$update = mysql_query($sql);
    }
    else {
    	$get_res = mysql_query("select * from results order by whattime desc limit 10");
    	if (mysql_num_rows($get_res) > 0) {
    		echo "<table>";
    		echo "<th>Username</th>";
    		echo "<th>Date & Time</th>";
    		while ($row = mysql_fetch_array($get_res)) {
    			echo "<tr>";
    			echo "<td>" . $row["username"] . "</td>";
    			echo "<td>" . date("d/m/Y H:i",$row["whattime"]) . "</td>";
    			echo "</tr>";
    		}
    		echo "</table>";
    	}
    	else {
    		echo "No results to show";
    	}
    }
    ?>
    
    PHP:
    You will need to modify the database connections as required, the database table structure comes next.
     
    Gawk, Dec 12, 2007 IP
  8. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Here is the code to create the table structure...

    CREATE TABLE `results` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `username` VARCHAR( 100 ) NOT NULL ,
    `whattime` INT NOT NULL
    ) ENGINE = innodb;
     
    Gawk, Dec 12, 2007 IP
  9. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #9
    So to the form page submits to record.php, this then records the time and username into the database, if you view record.php without submitting the form it will list the top ten results.

    Actually use this code in record.php as it notifies the user that you have recored the result...

    
    <?
    define('db_host', 'your_mysql_host');
    define('db_user', 'mysql_username');
    define('db_pass', 'mysql_password');
    define('db_database', 'mysql_database');
    $db = mysql_connect(db_host, db_user, db_pass);
    mysql_select_db(db_database);
    
    if ($_POST["action"] == "record_time") {
    	$sql = "insert into results set username = \"" . addslashes(strip_tags(htmlspecialchars($_POST["username"]))) . "\", whattime = \"" . time() . "\"";
    	$update = mysql_query($sql);
    }
    else {
    	$get_res = mysql_query("select * from results order by whattime desc limit 10");
    	if (mysql_num_rows($get_res) > 0) {
    		echo "<table>";
    		echo "<th>Username</th>";
    		echo "<th>Date & Time</th>";
    		while ($row = mysql_fetch_array($get_res)) {
    			echo "<tr>";
    			echo "<td>" . $row["username"] . "</td>";
    			echo "<td>" . date("d/m/Y H:i",$row["whattime"]) . "</td>";
    			echo "</tr>";
    		}
    		echo "</table>";
    		echo "<br><br>";
    		echo "Thank you " . addslashes(strip_tags(htmlspecialchars($_POST["username"]))) . " we have recorded your result.";
    		echo "Please <a href=\"record.php\"></a>click here</a> to see the latest results"; 
    	}
    	else {
    		echo "No results to show";
    	}
    }
    ?>
    
    PHP:
    Hope you can make some sense of it all !
     
    Gawk, Dec 12, 2007 IP
  10. kostas11

    kostas11 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I don't know what to say. I am speechless. Once again Thank you.
    I cant stress enough how much I appreciate your help.

    I would also like to include a link to your website or the company you are working for as a thank you for helping me. Please let me know of your company's url or your personal website. This is the least I can do.
     
    kostas11, Dec 12, 2007 IP
  11. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #11
    You are welcome ;)

    My company is www.webstart.co.uk, you can link to this or any of the links in my signature, or all three them - heh heh.

    Hope the code works for you!
     
    Gawk, Dec 12, 2007 IP
  12. kostas11

    kostas11 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I've created a database and the structure, I also used the php code but when I try to access the record.php I get a blank page. I obviously must be doing something wrong. I suppose that paths may be a problem, I've tried several times but still no luck. Any ideas?
     
    kostas11, Dec 13, 2007 IP
  13. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #13
    put
    echo mysql_error();
    PHP:
    after each mysql_query line to see if the database is throwing up any errors.

    Have you filled in these details with your own
    
    define('db_host', 'your_mysql_host');
    define('db_user', 'mysql_username');
    define('db_pass', 'mysql_password');
    define('db_database', 'mysql_database');
    
    PHP:
     
    Gawk, Dec 13, 2007 IP
  14. kostas11

    kostas11 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hi Gawk,

    When I login to mysql database I can see the usernames that I have submitted but the record.php is still blank, it doesn't return any results. I am confused. Why do you think that record.php remains blank?
     
    kostas11, Dec 13, 2007 IP
  15. kostas11

    kostas11 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    For example

    I can see: id: 1 / username: user1 / whatime: 1197540934
    id:2 / username: user2 / whatime: 1197546876
    etc.
    But record.php does not display anything
     
    kostas11, Dec 13, 2007 IP
  16. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Does it say no results to display or just a blank screen?

    Have a look at the page source to see if any error messages show up there.
     
    Gawk, Dec 13, 2007 IP
  17. kostas11

    kostas11 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    It doesn't say anything, its just a blank screen.

    I've also looked at the page source and still cant find any errors, actually the source of record.php is blank.

    I will use again echo mysql_error(); after each mysql_query line to see if it returns any errors.
     
    kostas11, Dec 13, 2007 IP
  18. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #18
    It won't say anything when you post a new time because I put the thank you message in the wrong place, try this slightly modified version...

    if ($_POST["action"] == "record_time") {
       $sql = "insert into results set username = \"" . addslashes(strip_tags(htmlspecialchars($_POST["username"]))) . "\", whattime = \"" . time() . "\"";
       $update = mysql_query($sql);
       	echo "<br><br>";
    	echo "Thank you " . addslashes(strip_tags(htmlspecialchars($_POST["username"]))) . " we have recorded your result.";
    	echo "<br><br>";
    	echo "Please <a href=\"record.php\"></a>click here</a> to see the latest results";
    }
    else {
        $get_res = mysql_query("select * from results order by whattime desc limit 10");
    	if (mysql_num_rows($get_res) > 0) {
    	    echo "<table>";
    		echo "<th>Username</th>";
    		echo "<th>Date & Time</th>";
    		while ($row = mysql_fetch_array($get_res)) {
    			echo "<tr>";
    			echo "<td>" . $row["username"] . "</td>";
    			echo "<td>" . date("d/m/Y H:i",$row["whattime"]) . "</td>";
    			echo "</tr>";
    		}
    		echo "</table>";
    	}
    	else {
    		echo "No results to show";
    	}
    }
    PHP:
    If you view record.php on it's own (not by posting a result to it) it should show the results in a table.
     
    Gawk, Dec 13, 2007 IP
  19. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #19
    Try this code:

    <?
    define('db_host', 'your_mysql_host');
    define('db_user', 'mysql_username');
    define('db_pass', 'mysql_password');
    define('db_database', 'mysql_database');
    	
    	// Check and open the db connection
    	if ( !mysql_connect(db_host, db_user, db_pass) )
    		die( mysql_errno() . ": " . mysql_error() );
    	
    	// Check and select the db	
    	if ( !mysql_select_db(db_database) )
    		die( mysql_errno() . ": " . mysql_error() );
    
    if ($_POST["action"] == "record_time") {
    	$sql = "insert into results (username,whattime) VALUES ('" . mysql_real_escape_string($_POST["username"]) . "','" . time() . "')";
    	
    	// Run the quesry and check for errors
    	if ( !mysql_query($sql) )
    		die( mysql_errno() . ": " . mysql_error() );
    }
    else {
    
        if ( ($get_res = mysql_query("select * from results order by whattime desc limit 10")) == false )
    		die( mysql_errno() . ": " . mysql_error() );
    	
        if (mysql_num_rows($get_res) > 0) {
            echo "<table>";
            echo "<th>Username</th>";
            echo "<th>Date & Time</th>";
            while ($row = mysql_fetch_array($get_res)) {
                echo "<tr>";
                echo "<td>" . $row["username"] . "</td>";
                echo "<td>" . date("d/m/Y H:i",$row["whattime"]) . "</td>";
                echo "</tr>";
            }
            echo "</table>";
        }
        else {
            echo "No results to show";
        }
    }
    ?>
    PHP:
    There was a mistake the first sql statement.

    Brew
     
    Brewster, Dec 13, 2007 IP
  20. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Really, what was it?
     
    Gawk, Dec 13, 2007 IP