Forum Moderators: coopster
As an example, during 45 sec the function should make a query from the database in every 5 sec and check the field 'control' if it has changed from '0' to '1' or not.
If the value is '1', the cycle should end in there and launch 'function first()' and also start a new cycle for 45 sec that queries in every 5 sec for the value '1' in the database field 'control2'. Again, if the value is '1' it should end the current cycle, launch 'function second()' and end the script.
If during the first cycle no changes in the database were detected 'function three()' should be launched and a new cycle should be started too.
If during the second cycle no changes in the database were detected 'function three()' should be launched and the script should end its work.
Script would be launched with 'exec()'
<?php
$times = 9;
$count = 0;
while ($count < $times) {
$count++;
$query=mysql_query("SELECT devices FROM control");
flush();
sleep(5);
}
?>
- create a function (eg acme_mega_function()) that does the looping/querying (setup a loop that iterates 9 times, on each iteration run the query, as you have above. If the query is successful return true, otherwise false.)
- create a procedure that calls the acme_mega_function() and calls the first, second and third functions you mention depending on the return value.
Is this what you mean?
This isn't tested, neither is it the most elegant solution to this but it should do roughly what you are asking for:
<?php
function foo($control){
for($x=0; $x<9; $x++){
$query = mysql_query("SELECT $control FROM table WHERE this = that");
if(mysql_num_rows($query) > 0){
return true;
}
else {
sleep(5);
}
}
return false;
}
$flag = 0;
do {
if (foo('control')){
first();
if (foo('control2')){
second();
$flag = 1;
}
else {
three();
$flag = 1;
}
}
else {
three();
$flag = 0;
}
} while ($flag == 0);
?>