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.
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"
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
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>
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.
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;
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 !
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.
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!
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?
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:
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?
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
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.
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.
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.
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