Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKnowledgeWoods Consulting
5,607 PointsNeed Countdown timer, which when active can display promo code but when timer is finish the promo code becomes inactive.
Thanks in advance. I need to create the count down timer. When timer is in active state, the promo code is active but when timer is finish, then promo code becomes inactive. I want to place this timer at my website. I want to display the timer till through database. Please give me some suggestions to start this.
5 Answers
thomascawthorn
22,986 PointsMaybe this will help!
So in JS you can write 'Run the timer until this timestamp'. So for you, it will be running the countdown - change the seconds/minutes/hours up to a certain date.
I imagine you have a database table of promo codes and a database table of customers? If it were me, I would create a new table of customers_promo_codes to join the ID of the customer with the ID of the promo code. On this join, I would also add a field for the expiry date.
When you refresh the page, you'll need to grab the expiry date and use it to work out time remaining. You can then plug in any user interface/countdown clock you want :)
Hope this helps! Please let me know if you have any questions
thomascawthorn
22,986 Pointswhat happens iif the page is refreshed? Should the timer restart?
KnowledgeWoods Consulting
5,607 PointsNo, timer should not restart.
KnowledgeWoods Consulting
5,607 PointsHi Tom, Thanks for the reply. I am able to run the counter but I also want that when timer gets over, the displayed promo code should become deactivate. Please suggest me some approach to do this. Below is my code. Index.php - In this one can insert different date for the timer.
// index.php
<form method="post" action="counter.php">
Year: Value Starts from 0. If you want to display the timer for the same year, then value should be zero.
Suppose for next year, then value should be 1.
<input type="text" name="year"><br />
Month: Enter the month.
<input type="text" name="month"> <br />
Enter the date.
<input type="text" name="day"><br />
<input type="submit" name="date" value="Submit">
</form>
When the form is submitted, it moves to another php page "counter.php" - It includes php code and javascript // code for counter.php
<?php
// include database file
include('db.php');
// select values from database
$select = $conn->query("SELECT * FROM date");
foreach($select as $values){
$day = $values["day"];
$month = $values["month"];
$year = $values["year"];
}
if(isset($_POST["date"])){
$year = $_POST["year"];
$month = $_POST["month"];
$day = $_POST["day"];
// insert into database
$insert = $conn->query("INSERT into date(year, month, day) VALUES('$year', '$month', '$day')");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>jQuery Countdown</title>
<link rel="stylesheet" href="jquery.countdown.css">
<style type="text/css">
#defaultCountdown { width: 240px; height: 45px; }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="jquery.plugin.js"></script>
<script src="jquery.countdown.js"></script>
<script>
$(function () {
var austDay = new Date();
austDay = new Date(austDay.getFullYear() + (<?php echo $year; ?>), (<?php echo $month; ?>) - 1, <?php echo $day;?>);
$('#defaultCountdown').countdown({until: austDay});
$('#year').text(austDay.getFullYear());
});
</script>
</head>
<body>
<h1>jQuery Countdown Basics</h1>
<p>This page demonstrates the very basics of the
<a href="http://keith-wood.name/countdown.html">jQuery Countdown plugin</a>.
It contains the minimum requirements for using the plugin and
can be used as the basis for your own experimentation.</p>
<p>For more detail see the <a href="http://keith-wood.name/countdownRef.html">documentation reference</a> page.</p>
<p>Counting down to 26 January <span id="year">2014</span>.</p>
<div id="defaultCountdown"></div>
</body>
</html>
Please suggest me.
thomascawthorn
22,986 PointsIs there a promo code for each customer?
KnowledgeWoods Consulting
5,607 PointsNo. Promo code is common. If user wants he/she can apply it.