Question set off alarm at specific time ( SitePoint Forums PHP ) Updated: 2010-07-21 05:50:03 (3) |
|
set off alarm at specific time
Hi all,
I have a calendar installed on my website. When adding events, it is possible to specify an exact time when the even will be beginning.
I have create a php script that will check the database where the events are help, and will output all the event that are on the current day. How would i go about checking the exact time of the event? Im sure that I will require some kind of loop, checking the time of the event and the currenct time, then when the are equal, load a page say alarm.php.
How would I go about checking this time in the background of each php page withing my website?
Cheers
|
|
| Answers: set off alarm at specific time ( SitePoint Forums PHP ) |
|
set off alarm at specific time
What about using javascript?
Write a list of event times into a javascript array when the page loads, then check the current time vs. this array (with javascript) once a minute and either pop up an alert box or force a page re-load to your alarm.php page when current time equals one of the alarm times.
Otherwise, unless you want to set up a cron job on your server that will force a page re-load every minute, I can't think of a good php-only solution.
milenko1054
|
|
set off alarm at specific time
I have attached the following code to all my php pages within my website usong the include php function
<?
include './alarm.php';
?>
ALARM.PHP FILE:
$username = "username";
$password = "password";
$database = "database";
$sql = "SELECT * FROM webcal_entry WHERE cal_create_by = '".$bewiseusername."'";
#CONNECT TO MYSQL
$dbcnx = @mysql_connect('mysql5.streamline.net',$username,$password) or die("Unable to connect to MySQL");
#CONNECT TO DATABASE
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query($sql);
$nrows = mysql_num_rows($result);
$d = date("d");
$m = date("m");
$y = date("Y");
$date = $y.$m.$d;
$noevents = 0;
while ($row = mysql_fetch_array($result)) {
if ($date == $row['cal_mod_date']){
$events[$noevents] = $row['cal_time'];
$noevents ++;
}
}
}
//At this point, all events that are today, the time of each are saved into a php array called $events[]
$noalarm = 0;
print '<SCRIPT language="JavaScript">';
print 'var alarm= new Array('.$noevents.') ';
while ($noalarm < $noevents){
print 'alarm['.$noalarm.']= "'.$events[$noalarm].'"; ';
$noalarm ++;
}
print 'var x=0; ';
print 'for (x=0; x<5; x++) ';
print '{ ';
print 'alert(alarm[x]); ';
print '} ';
print '</SCRIPT> ';
END OF ALARM.PHP FILE
im having difficulties saving the values in $events[] into the javascript array alarm[]. No alerts will display on the screen. Any ideas where im going wrong?
Also, how would I go about checking the javascript array against the current time every minute and redirecting to another php file?
sorry about this, but dont know much javascript1
Much appreciated
dazi_behappy
|
|
set off alarm at specific time
Your logic looks good, one typo in your js though:
print 'var alarm= new Array('.$noevents.') ';
should be
print 'var alarm= new Array('.$noevents.') ;';
I'm a little short on time today so I can't provide code for the timer (search for javascript timer in google) but here's the logic:
- Start the timer and set for 60 seconds.
- When timer fires, create a javascript date object then call a function that will compare date.getHour() and date.getMinutes() to the values in your javascript array of alarm times.
- If there's a match, pop up the alert or redirect (window.location="url") to your alarm.php page.
- If no match, start the timer again.
Hope that helps...
milenko1054
|
|
- Source: - Previous Question: SitePoint Forums PHP - Next Question: SitePoint Forums PHP |
|
|