Forum Moderators: coopster

Message Too Old, No Replies

timed functions and cycles

How to end the Cycle Earlier and do Something in Such Cases?

         

smarty

4:31 pm on Dec 29, 2003 (gmt 0)

10+ Year Member



I want to create a function that would do queries from a database during a given time period.

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);
}
?>

lorax

5:39 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



At first glance it sounds like you're looking to update the client's browser with some information that is dependent upon that 'control' field being switched. If that's the case I'd suggest you write a javascript timer that initiates the query to the db.

What are you trying to achieve?

smarty

5:49 pm on Dec 29, 2003 (gmt 0)

10+ Year Member



The solution has to be browser independent, meaning that although it will be launched by submitting a form, it will continue running on its own in the server when the browser is closed or crashes etc.

lorax

6:10 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you run a Cron job?

coopster

6:13 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



lorax, this thread stalled a couple weeks ago, maybe this additional information will help...
[webmasterworld.com...]

smarty

6:20 pm on Dec 29, 2003 (gmt 0)

10+ Year Member



Yes I can run a Cron job

mogwai

6:30 pm on Dec 29, 2003 (gmt 0)

10+ Year Member



This is how I'd attempt something like this.

- 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?

lorax

6:30 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks coopster - that helps.

smarty, what's wrong with the code you posted other than it has no if...then...else logic? It seems you could turn into a function and use it.

smarty

12:00 am on Dec 31, 2003 (gmt 0)

10+ Year Member



Yes mogwai I mean that. Can You show me more in detail.

mogwai

1:28 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



Hi,

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);
?>