Current location: Hot Scripts Forums » Programming Languages » PHP » having Loop issues, help... Advanced


having Loop issues, help... Advanced

Reply
  #1 (permalink)  
Old 02-26-06, 11:38 PM
todayscoffee todayscoffee is offline
Newbie Coder
 
Join Date: Oct 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
having Loop issues, help... Advanced

i have a main 'for' loop and within it i have 4 'while' loops. each sub loop compares a date ( 1 to 4 ) with an $OKdate and makes sure that the OKdate is not before any of the 4 dates, and if the OKdate is then it will add a day (864000 seconds) to the OKdate. since the dates may vary i want to make sure that all dates work. so my question is this one: from the example below am i on the right track if not please HELP, and two how do i get these 4 loops to go back and check all of them until they all work and then end the loop and continue?

example:
PHP Code:



$date1 
0;
$date2 0;
$date3 0;
$date4 0;
$allOK 0;

for(
$allOK 0$allOK 1; ){

if(
$OKdate $date1){
while(
$OKdate $date1){
$OKdate 864000;
$date1 0;
 }
$date1 1;
}
if(
$OKdate $date2){
while(
$OKdate $date2){
$OKdate 864000;
$date2 0;
 }
$date2 1;
}
if(
$OKdate $date3){
while(
$OKdate $date3){
$OKdate 864000;
$date3 0;
 }
$date3 1;
}
if(
$OKdate $date4){
while(
$OKdate $date4){
$OKdate 864000;
$date4 0;
 }
$date4 1;
}

if((
$date1 $date2 $date3 $date4) == 4){
$allOK 1;
}


Please help, i'm totally lost here
__________________
One may say "They're losing their marbles." I would ask, "Who gave them the marbles in the first place?"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-27-06, 01:02 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
1) The initial for() loop is not valid syntax and is actually not the way to do a loop like this. You should use a do-while -
PHP Code:

$exit_flag == 0;

do {

// your logic changes the $exit_flag to a non-zero value when it is done

} while ($exit_flag 0); 
2) From your description, it sounds like you want to increment $OKdate by a day until it is just greater than the highest $datex variable ("makes sure that the $OKdate is not before any of the 4 dates" - which I read to mean, do this until it is higher than all of the 4 dates.) Let us know if this statement is correct and someone can come up with code to do this.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 02-27-06 at 01:30 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-27-06, 01:36 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Just in case statement #2 is true, here is some code to do this -
PHP Code:

// generate some dates

$date1 time() + rand(09999999); // some random future date in time
$date2 time() + rand(09999999); // some random future date in time
$date3 time() + rand(09999999); // some random future date in time
$date4 time() + rand(09999999); // some random future date in time
$OKdate time() + rand(09999999); // some random future date in time

// determine the maximum 
$max_date max($date1$date2$date3$date4);

// let us see the starting values
echo "d1 = $date1, d2 = $date2, d3 = $date3, d4 = $date4, max = $max_date, OKdate = $OKdate<br>";

// reset the exit flag
$exit_flag 0

// loop until the flag is set to a 1
do {
    if(
$OKdate $max_date){
        
// value < maximum, add one day
        
$OKdate += 864000;
        echo 
"adding a day, OKdate = $OKdate<br>";
    } else {
        
// value >= maximum, done, set the exit flag
        
$exit_flag 1;
        echo 
"done<br>";
    }
} while (
$exit_flag == 0); 
Or more simply (since there is only one thing going on inside the loop, and with some additional delta info) -
PHP Code:

// generate some dates

$date1 time() + rand(09999999); // some random future date in time
$date2 time() + rand(09999999); // some random future date in time
$date3 time() + rand(09999999); // some random future date in time
$date4 time() + rand(09999999); // some random future date in time
$OKdate time() + rand(09999999); // some random future date in time

// determine the maximum 
$max_date max($date1$date2$date3$date4);

// let us see the starting values
echo "d1 = $date1, d2 = $date2, d3 = $date3, d4 = $date4, max = $max_date, OKdate = $OKdate<br>";

while (
$OKdate $max_date){
    
$delta $max_date $OKdate;
    
$OKdate += 864000;
    echo 
"OKdate is $delta less than the highest date, adding a day, OKdate is now = $OKdate<br>";
}
$delta $OKdate $max_date;
echo 
"done, OKdate is $delta more than the highest date<br>"

Last edited by mab; 02-27-06 at 02:25 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Total mysql_num_rows on while loop dihan PHP 2 08-21-05 04:31 PM
Having a problem with a large while loop. . . Spreegem PHP 13 05-03-05 06:53 AM
While loop jaishalg PHP 1 11-23-04 04:36 PM
Loop - Convert number into day of the week dihan PHP 12 06-30-04 05:28 AM
type mismatch and update loop - HELP! seala ASP 1 09-22-03 06:27 PM


All times are GMT -5. The time now is 07:25 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.