Log in

View Full Version : Live Countdown From Database?



joejoe20
04-16-2008, 07:25 PM
Is it possible to do a live countdown from a database?

thetestingsite
04-16-2008, 08:30 PM
You could, but depending on what you mean by "live", I could give a different answer. In other words, by "live" do you mean you want it to automatically update every second, minute, etc or do you want something else? Have you looked in the library for a countdown script (made in javascript) that you could modify for your needs? In any case, if you want a more specific answer, we need a more specific question.

Hope this helps.

joejoe20
04-16-2008, 10:35 PM
Sorry for not giving more explanation.

The script i have will put not allow a user to do certain things for a set time. The script will then put the user's "username" and a certain "time" (depending what was done) in the database.

EXAMPLE: (whats inserted)

id|username|time_left
1| joejoe20 | 120
------------------------

I would like to show on a page this information

Username | Time Left
-------------------------
joejoe20 | 2:00

Then i would like it to countdown to zero.

Hopefully i made more sense. I looked at javascript and i was told javascript wouldnt be able to interact with mysql. (i never scripted javascript)

thetestingsite
04-17-2008, 12:35 AM
You could modify this script to do what you want.

http://www.dynamicdrive.com/dynamicindex6/universalcountdown.htm

Simply use php to write the date to countdown to (based on the time pulled from the db) and insert it into the cdLocalTime function. I would go into more detail, but I'm short on time at the moment.

Hope this helps nonetheless.

joejoe20
04-17-2008, 09:52 AM
Thank You

Im sorry if im asking to much. I have never played around with javascript, i have no clue how to have javascript call a php command to find how much time is left.

thetestingsite
04-20-2008, 06:42 PM
Well, after following the instructions on DD, simply make your PHP script connect to the database, get the time left, add that number to the seconds returned by the time() function, then output that date to the JavaScript. Something like the following should suffice as a basis to what I'm talking about (Not tested).



<?php
include('db_connect.php'); //connect to the db
?>
<table>
<tr>
<td>Username</td>
<td>Time Left</td>
</tr>
$result = mysql_query("SELECT * FROM `table` WHERE `username`='$username'");
while ($row = mysql_fetch_array($result)) {
$futuredate = date('F d, Y H:i:s', time()+$row['time_left']);
?>
<tr>
<td><?php echo $row['username'];?></td>
<td><div id="cdcontainer"></div>

<script type="text/javascript">
//cdLocalTime("ID_of_DIV_container", "server_mode", LocaltimeoffsetMinutes, "target_date", "opt_debug_mode")
//cdLocalTime.displaycountdown("base_unit", formatfunction_reference)

//Note: "launchdate" should be an arbitrary but unique variable for each instance of a countdown on your page:

var launchdate=new cdLocalTime("cdcontainer", "server-php", 0, "<?php echo $futuredate;?>", "debugmode")
launchdate.displaycountdown("days", formatresults2)
</script></td>
</tr>
<?php
}
?>
</table>


Hope this helps.