SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: dynamic comparison
-
Mar 31, 2009, 06:38 #1
- Join Date
- Aug 2002
- Location
- Perth, Western Australia
- Posts
- 759
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
dynamic comparison
I have a list of servers which I am checking the time period for which they last reported an update. Based on the period they are classified at different levels of online and off line states.
I have these three vars which I need to evulate through an if statement. Based on the one to pass I will know at what status the server is.
Here is my three vars
PHP Code:$updated_timstamp $status_indicator->getComparison() $timestamp
HTML Code:2009-03-31 20:35:02 >= 2009-03-31 20:33:34
PHP Code:if ($updated_timstamp $status_indicator->getComparison() $timestamp)
I have tried to eval the code but nothing I have tried works.
Can anyone tell me what I need to do in order to complete what I am trying to achieve.
-
Mar 31, 2009, 07:03 #2
- Join Date
- Nov 2008
- Location
- Peterborough, Ontario, Canada
- Posts
- 316
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm not entirely sure what your code is trying to do here - but I'm going to take a guess (I have two ideas).
If you are trying to use an operator from a variable PHP allows that - you should be getting an error. (as in you can't store the ">=" as a variable to be used as an operator). You'd have to use nested if statements for each possible operator, or use switch statements. As in:
Code:if($status_indicator->getComparison() == ">="){ if($updated_timestamp >= $timestamp) { //do something } else { //do something else } } elseif($status_indicator->getComparison() == "<=") { if($updated_timestamp <= $timestamp) { //do something } else { //do something else } } else { //... ect...
You could use a switch statement too which would probably make it easier to read. Or you could probably come up with a better solution (but I'm not entirely sure this is what you're trying to accomplish).Have I helped you? You could help me.
Like my business on facebook!
Text message polls with real-time, live results.
-
Mar 31, 2009, 08:33 #3
- Join Date
- Aug 2002
- Location
- Perth, Western Australia
- Posts
- 759
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think your solution is the one I will have to go with. I know the comparison will have to be one of the comparison operators so I guess I can use a switch on all of them until I get a match then perform the comparison.
It's not pretty but I guess it will work.
Been think about it for days so thank you for your suggestion.
-
Mar 31, 2009, 08:39 #4
- Join Date
- Nov 2008
- Location
- Peterborough, Ontario, Canada
- Posts
- 316
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
no problem. you could use ternary operators - but that would probably just make it more complicated and harder to read since you'll have many options. But using a switch will make the code easy to understand.
Have I helped you? You could help me.
Like my business on facebook!
Text message polls with real-time, live results.
-
Mar 31, 2009, 17:57 #5
-
Mar 31, 2009, 18:02 #6
- Join Date
- Nov 2008
- Location
- Peterborough, Ontario, Canada
- Posts
- 316
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'd have to see your code to be able to offer any help - but I'm guessing it's because you haven't instantiated the object or set the variable...
Have I helped you? You could help me.
Like my business on facebook!
Text message polls with real-time, live results.
-
Mar 31, 2009, 18:04 #7
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That's because your object doesn't have such a property. Maybe you meant to call a method, but made a typo?
-
Mar 31, 2009, 22:12 #8
- Join Date
- Aug 2002
- Location
- Perth, Western Australia
- Posts
- 759
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thee full source
PHP Code:<?php
class checkCurrentStatusTask extends sfBaseTask
{
protected function configure()
{
// // add your own arguments here
// $this->addArguments(array(
// new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
// ));
$this->addOptions(array(
new sfCommandoption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'the application that this is running as', 'webmaster'),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
// new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'the connection name', 'propel'),
// new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'the connection name', 'propel'),
new sfCommandOption('verbose', null, sfCommandOption::PARAMETER_REQUIRED, 'Enables verbose output', false),
// add your own options here
));
$this->namespace = 'hopcon';
$this->name = 'check-current-status';
$this->briefDescription = 'This task will check on the current stauts of servers and notify the required people whe the status changes';
$this->detailedDescription = <<<EOF
The [checkCurrentStatus|INFO] task will check on the current stauts of servers and notify the required people whe the status changes.
Call it with:
[php symfony checkCurrentStatus|INFO]
EOF;
//
//
// $this->addArgument('application', sfCommandArgument::REQUIRED, 'webmaster');
// $this->addOption('verbose', null, sfCommandOption::PARAMETER_REQUIRED, 'Enables verbose output', false);
// $this->addOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel');
// $this->addOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The enviroment', 'dev');
}
protected function execute($arguments = array(), $options = array())
{
// initialize the database connection
$databaseManager = new sfDatabaseManager($this->configuration);
// $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : '')->getConnection();
// $databaseManager = new sfDatabaseManager($configuration);
// echo "connection: ".print_r($connection, true)."\n";
// echo "databaseManager: ".print_r($databaseManager, true)."\n";
// echo "configuration: ".print_r($this->configuration, true)."\n";
// mail('nathan@lionslair.net.au', 'debug', print_r($this->configuration, true));
// add your code here
/*
* Get all current status's
*/
$server_users = ServerUsersPeer :: doSelect(new Criteria());
// echo print_r($server_users, true);
$status_indicators = StatusIndicatorPeer :: getAll();
/*
* Only continue if there are server_users
*/
if ($server_users)
{
/*
* for each of records
*/
foreach ($server_users as $server)
{
if ($status_indicators)
{
foreach ($status_indicators as $status_indicator)
{
/*
* find the match
*/
$timestamp = date(sfConfig::get('app_date_format_save'), strtotime("-".$status_indicator->getPeriod()." minutes"));
$updated_timstamp = ServerUsersPeer :: getUpdatedAtByServerKey($server->getServerKey());
// $value = eval("\$updated_timstamp \$status_indicator->getComparison() \$timestamp");
// $value = $updated_timstamp.' '.$status_indicator->getComparison().' '.$timestamp;
$value = "$updated_timstamp $status_indicator->getComparison() $timestamp";
// $compare = constant($status_indicator->getComparison());
// echo 'value: '.$value."\n";
// echo 'comparison: '.$status_indicator->getComparison()."\n";
// echo 'status_indicator: '.print_r($status_indicator, true).'\n';
if ($updated_timstamp $status_indicator->getComparison() $timestamp)
{
// ok now send the email or what ever
// echo print_r(sfConfig :: get('app_mailer_defaults'), true);
// nahoMail::send('My subject', 'My body', 'recipient@mail.com');
ServerStatusLogPeer :: add($server->getId(), $status_indicator->getId());
}
} // end foreach
} // end if
} // end foreach
} // end if
} // end function execute
}
-
Mar 31, 2009, 22:21 #9
- Join Date
- Nov 2008
- Location
- Peterborough, Ontario, Canada
- Posts
- 316
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It's extending another class - so it's calling from code we can't see.
This looks like it's going to be a long debugging project.Have I helped you? You could help me.
Like my business on facebook!
Text message polls with real-time, live results.
-
Mar 31, 2009, 22:29 #10
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That code won't produce such an error message. It will result in a parse error, failing to even execute.
Bookmarks