Shane Geist
Shane Geist

Reputation: 31

In php, I need a while statement to complete before running a second while statement

In php, I need while 1 to complete before running while 2. While 1 creates an array based on table 1. While 2 deletes data from table 2 based on what is NOT in the array from while 1.

My problem is that while 2 is running prior to completion of the array being created in while 1. I could do this by using a time delay, but that really does not assure that the array is complete, plus it also requires an otherwise unnecessary delay.

I think I need to do something like an interval in javascript, which I think I could do. However, for this to work I need some flag indicating that while 1 is complete. Basically, my problem seems to be that I need this flag, but I don't know how to do a flag that is true only when while 1 is complete.

Here is the code I am working on:

$data01 = mysql_query("SELECT * FROM crxcrops")or die(mysql_error());

while($r01 = mysql_fetch_array($data01)) {

    $cropsarray[] = $r01['cropsid_crp'];

}

//Need to pause here, not proceed until $cropsarray is complete. This pause does not seem to happen. The intent is to delete rows from crxplugs (below) that have a cropsid not found in crxcrops (above). I am not getting consistent results, which led me to believe that there is not a pause here. However, maybe I have some error?? Maybe I cannot run the DELETE while still in the SELECT??

$data00 = mysql_query("SELECT * FROM crxplugs")or die(mysql_error());

while($r00 = mysql_fetch_array($data00)) {

    $key = array_search($r00['cropsidplg'], $cropsarray);

    if($key==false){

        mysql_query("DELETE FROM crxplugs WHERE cropsidplg=$ r00['cropsidplg']") or die(mysql_error());

    }

}

Upvotes: 0

Views: 80

Answers (1)

Jafar Akhondali
Jafar Akhondali

Reputation: 1527

I think all you need is using a flag, but try to provide some code it's hard to write something we are not sure about.

$flag=false;

//first while
while (1==1){

//do first job
/*
if (first job has finished)
$flag=true;
*/


//second while
if(flag){
while(/*second while statement*/){
//do second job
}
break;
}



}

Upvotes: 1

Related Questions