Asesha George
Asesha George

Reputation: 2268

assigned variable is not working in do while loop in using PHP

I am working on Do While loop in my project its working fine first time.

Before while statement, I assigned a value to an array I could able to print the array successfully at bottom of the code, BUT its become 0 when I check at top of the loop.

Code:

$looparray = array();
$loopend = 0;
$arraymer = array();
$poolafirtsid = $previous_array_values; //previous array values

do {
   if (sizeof($looparray) == 0) {
       $firstsponarray = $poolafirtsid;
   } else {
       $firstsponarray = $looparray;
   }

   $firstsponarray = getUserArray($poolafirtsid);
   //get user arraylist of first

   foreach ($firstsponarray as $avalue) {
       $rooparray = membercount($avalue);
       $bsponarray = getUserArray($avalue);
       //get second users arraylist 9
       if (sizeof($bsponarray > 0)) {
           $barraymer = array_merge($barraymer, $bsponarray);
       }

       $aarraylist[$avalue] = $rooparray;
   }

   $asmallestsponid = getSmallestID($aarraylist);
   //get smallest id in the array

   if (membercount($asmallestsponid) < 3) {
       $loopend = 1;
   } else {
       global $pooldata;
       if (count($barraymer) > 0) {
           $pooldata = $barraymer;
       }
       print_r($pooldata);

   }
} while ($loopend == 1);

When I print in else its working but I am unable to print starting of do loop its showing array size is 0

Upvotes: 6

Views: 1050

Answers (3)

dWinder
dWinder

Reputation: 11642

I will ignore all the name issues but address your while loop issue:

$loopend =0;
do {
    ...
    if(membercount($asmallestsponid)<3) {
         $loopend = 1;
    }else{
      ...
    }
while ($loopend == 1);

There are 2 options:

  1. The if condition is true: if so, $loopend will get 1 so the loop continue (which not seem fit the call him "loop end" but what ever...)

  2. The if condition is false: then the $loopend stay the same (init as 0) so the loop will stops

IMHO - this will simplify your loop:

do {
    ...
while (membercount($asmallestsponid)<3);

Upvotes: 5

Jos&#233; A. Zapata
Jos&#233; A. Zapata

Reputation: 1297

There are several problems with your code. First of all, this does nothing:

if (sizeof($looparray)==0) {
    $firstsponarray= $poolafirtsid;
} else {
    $firstsponarray= $looparray;
}

since the very next line after that piece of code is:

$firstsponarray= getUserArray($poolafirtsid);

which overrides any prior assignment of $firstsponarray.

Second, the value of $looparray doesn't change at all in the do loop, so it'll always be an empty array. I found this line:

$rooparray=membercount($avalue);

which I assume is a typo and the correct line is $looparray=membercount($avalue);. Same with the line $aarraylist[$avalue]=$rooparray;. However, changing that also does nothing since $firstponarray will never be equal to $looparray for the reason I described at the top.

Try debugging your code first, and if the problem persists post the updated code.

Upvotes: 0

Alexey
Alexey

Reputation: 3484

We don't know what membercount($asmallestsponid) returns, but it appears (most likely) that on the 1st pass it gets into

} else {
                $looparray = $arraymer;
                //print_r($looparray);
}

the value of $loopend doesn't change, i.e. remains at 0 and on the first pass it compares 0 to 1 and decides to exit the do {} while loop as 0 != 1

Upvotes: 0

Related Questions