Click to See Complete Forum and Search --> : [RESOLVED] persistent reminder / alert


tjmcd
11-21-2006, 01:58 PM
Hey All,
I have a php script in 'jsAlerts.php' which calls an alert in 'alerts.js' on condition of an empty query result from a specific table.
Upon clicking the resulting alert's 'OK' button users are redirected to the php module wherein the undesired empty table condition may be remedied. However, the alert is triggered with just about every mouse click even while endeavoring to remedy the empty result condition (adding records to the table). How might I alter this script to call the alert -- say... every 5 minutes? //Prep periodAlert query
if ($today == date ('Y-m-d')){
$period_time_end = $timedate->handle_offset(date("Y-m-d H:"), $timedate->dbDayFormat, false);

$select_emailman = " SELECT id, deleted FROM emailman WHERE id !='' AND deleted !=1 ";
$result = mysql_query($select_emailman)or die ("Query failed - select_emailman"); // Run the query.
$numresults=mysql_query($select_emailman);
$numrows=mysql_num_rows($numresults);
// If we have no results, periodAlert
if ($numrows == 0){
$this->periodAlert();
}
}
Here's the alert function periodAlert(){
var r=confirm("Press a button")
if (r==true)
{
window.location = 'index.php?action=DetailView&module=Campaigns&action=index'
}
}
Thanks in advance!

legendx
11-21-2006, 04:24 PM
Give this a shot

Change:
if ($numrows == 0){
$this->periodAlert();
}

to:

if (!isset($_SESSION['alertTime']))
$_SESSION['alertTime'] = 0;

if ($numrows == 0 && (time() * (5*60) > $_SESSION['alertTime']){
$this->periodAlert();
$_SESSION['alertTime'] = time();
}


this requires sessions to be started via session_start() somewhere at the top of every page before any output. But the idea is that each time the alert is triggered it sets the session variable 'alertTime' to the current time (unix timestamp). And every time it sees that $numrows == 0 it will check if the current time is 5 minutes (or 5 * 60 seconds) ahead of alertTime.

I think that should work...

tjmcd
11-21-2006, 07:50 PM
Thanks Mike,
Gave it a shot. Looks promising, but sadly I get a parse error. Now, I'm a total noob here, but it seems to me like -- aren't we missing some braces in the first if statment, and parentheses in the second?
//Prep periodAlert query
if ($today == date ('Y-m-d')){
$period_time_end = $timedate->handle_offset(date("Y-m-d H:"), $timedate->dbDayFormat, false);
$select_emailman = " SELECT id, deleted FROM emailman WHERE id !='' AND deleted !=1 ";
$result = mysql_query($select_emailman)or die ("Query failed - select_emailman"); // Run the query.
$numresults=mysql_query($select_emailman);
$numrows=mysql_num_rows($numresults);
// If we have no results, periodAlert
if (!isset($_SESSION['alertTime']))
$_SESSION['alertTime'] = 0;

if ($numrows == 0 && (time() * (5*60) > $_SESSION['alertTime']){
$this->periodAlert(); /// Parse error: syntax error, unexpected '{' this row
$_SESSION['alertTime'] = time();
}
}
Now, despite what appear to me to be missing braces, the first "if statement" inserts in the code (all by itself) without generating any errors. No struggle. As for the second "if statment" (with or without the first 'if') generates parse errors. I tried juggling the parenteses so I thought it made more sense, but no luck. What am I missing (besides some semblance of a clue that is)?

legendx
11-21-2006, 09:38 PM
Hey, I messed up a little bit on the second if statement. There was a missing ')' and I had the comparison backwards. Try this

//Prep periodAlert query
if ($today == date ('Y-m-d')){
$period_time_end = $timedate->handle_offset(date("Y-m-d H:"), $timedate->dbDayFormat, false);
$select_emailman = " SELECT id, deleted FROM emailman WHERE id !='' AND deleted !=1 ";
$result = mysql_query($select_emailman)or die ("Query failed - select_emailman"); // Run the query.
$numresults=mysql_query($select_emailman);
$numrows=mysql_num_rows($numresults);
// If we have no results, periodAlert
if (!isset($_SESSION['alertTime']))
$_SESSION['alertTime'] = 0;

if ($numrows == 0 && time() > ($_SESSION['alertTime'] + (5*60))){
$this->periodAlert(); /// Parse error: syntax error, unexpected '{' this row
$_SESSION['alertTime'] = time();
}
}

Also, in PHP if there is only one line after an if statement, there is no need for brackets. so..

if ($x > $y )
function();

is ok! but..

if ($x > $y)
function();
function();

will give you an error, it needs to look like

if ($x > $y) {
function();
function();
}

tjmcd
11-22-2006, 05:29 AM
THANK YOU MIKE!
Woohoo!!! Finally! I was two whole days on this silly little alert.
I thought I had better search skills, but even after your help, info
on the date/time arithmetic is hard to come by. Fishing for a clue
I had also searched and posted in three popular forums with out
so much as a nibble. Also, thanks for the clarification on the
brackets. That helps on this and projects WELL in to the future. :D

Thanks Again, and Happy Holidays to you and yours!

agent_x91
11-22-2006, 06:29 AM
PHP Code:
if ($x > $y)
function();
function();


will give you an error,


Actually it won't, it will simply execute the second statement regardless of whether or not the condition is met. I just thought I'd point that out.