Jump to content

Two infinite while loops, not sure how to connect them?


Recommended Posts

I have two while loops that should run infinitely.

 

while loop #1 runs during 9:30am - 4pm, monday through thursday

while loop #2 runs when its not 9:30am-4pm, monday through thursday.

 

$hourmin = date("Gi", time(now));

$current = getdate();

 

while ( $hourmin > 829  && $current[hours] < 15  && $current[wday] > 0 && $current[wday] < 6  ):      // while loop #1

while ( $hourmin < 830  || $current[hours] > 14  || $current[wday] = 0 || $current[wday] = 6      ):          // while loop #2

 

 

 

as you can see the while condition is the easy part, but I'm not sure how to set this up. I was going to do this using a combination of if statements and the goto function but my version of php does not support goto.

 

I just want it to switch back and forth between these these two while statements infinitely. Is this possible in PHP?

Sounds to me like you don't need your two while loops, just one loop and a conditional.

 

while (true){ 
   if (/* 9:30am - 4pm, monday through thursday */){ 
      bodyOfLoop1();
   }
   else {
      bodyOfLoop2();
   }
}

 

What is the goal of this code?  Might be a better alternative.

I'm writing to an html file. If its during stock market hours write X, if its not during market hours write Y.

 

while (true){

  if (/* 9:30am - 4pm, monday through thursday */){

      bodyOfLoop1();

  }

  else {

      bodyOfLoop2();

  }

}

 

 

 

kicken:  wouldn't your code stop running after it goes back into market hours for the first time? i don't see how it knows to go back to bodyofloop1() after bodyofloop2() is executed in the else portion of the if.

 

What is it writing to the html file?  What is the purpose of the file.  Is it continuously writing data?  It seems that this is a script or even 2 that could be scheduled using cron, which is already designed to handle scheduling. 

I'm writing to an html file.

 

it is continuously writing data. its the price of gold. and it updates the price every 10 seconds or so.

 

Are you only updating the file with the current price or are you keeping a running log of the price?

 

If you are only updating the file with the current price then I think you are taking the entirely wrong approach. You don't need any script running all the time. Instead, change the page that the users access to dynamically get the price. If that process is time consuming, then you can implement a process in that page to get the value from a cache. It would first check the age of the cache and if it is older than a certain time limit then get a current value and update the cache. It makes no sense to constantly update the file every 10 seconds if the page is not being accessed every few seconds.

 

If you are wanting a running log, the you should probably be updating a database with the values and the "HTML" page should be a script to get the values from the database.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.