phpchick Posted December 8, 2011 Share Posted December 8, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/252725-two-infinite-while-loops-not-sure-how-to-connect-them/ Share on other sites More sharing options...
gizmola Posted December 8, 2011 Share Posted December 8, 2011 What is going to be happening inside these while loops? Quote Link to comment https://forums.phpfreaks.com/topic/252725-two-infinite-while-loops-not-sure-how-to-connect-them/#findComment-1295613 Share on other sites More sharing options...
kicken Posted December 8, 2011 Share Posted December 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252725-two-infinite-while-loops-not-sure-how-to-connect-them/#findComment-1295626 Share on other sites More sharing options...
phpchick Posted December 8, 2011 Author Share Posted December 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252725-two-infinite-while-loops-not-sure-how-to-connect-them/#findComment-1295735 Share on other sites More sharing options...
phpchick Posted December 8, 2011 Author Share Posted December 8, 2011 Oh I see now, since the loop will be true forever, it will cycle back to the beginning everytime. right, your code is much better than what I had in mind. Quote Link to comment https://forums.phpfreaks.com/topic/252725-two-infinite-while-loops-not-sure-how-to-connect-them/#findComment-1295738 Share on other sites More sharing options...
gizmola Posted December 8, 2011 Share Posted December 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252725-two-infinite-while-loops-not-sure-how-to-connect-them/#findComment-1295911 Share on other sites More sharing options...
phpchick Posted December 8, 2011 Author Share Posted December 8, 2011 it is continuously writing data. its the price of gold. and it updates the price every 10 seconds or so. Quote Link to comment https://forums.phpfreaks.com/topic/252725-two-infinite-while-loops-not-sure-how-to-connect-them/#findComment-1295946 Share on other sites More sharing options...
Psycho Posted December 8, 2011 Share Posted December 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252725-two-infinite-while-loops-not-sure-how-to-connect-them/#findComment-1296000 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.