6

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

dWinder
  • 11,597
  • 3
  • 24
  • 39
Asesha George
  • 2,232
  • 2
  • 32
  • 68
  • it's impossible to debug without seeing the code or returned values of `membercount()`, `getUserArray()`, `getSmallestID()` functions. Consider updating your question with their returned values instead of calls, so provide MVCE with hardcoded values and check if there is still error with them – Alexey Nov 18 '18 at 21:13
  • But my code is working fine I am getting values. My concern is $looparray array size is becoming 0 when I check on top of code. But I am getting correct results at bottom of code in else condition. – Asesha George Nov 18 '18 at 21:41
  • as said, if you're looking for help we need the values that these functions return, i.e. MCVE – Alexey Nov 18 '18 at 21:47
  • please see the result which I am getting at the bottom of the code when I use print_r, I am unable to access at top of the code to reuse – Asesha George Nov 19 '18 at 03:22
  • I could able to access the variable out the loop but the only thing is unable to access at top of the loop – Asesha George Nov 19 '18 at 04:02
  • We need to understand what are you doing with the `$arraymer` variable and the logic in your `for-each` loop. You have many repetitions and unnecessary process consumption on your script. Once you edited your question with access to such information we will be able to help properly – Diogo Santo Nov 23 '18 at 12:01
  • 2
    Also consider following `PSR-x` in the future and please realise that `variable naming` can escalate your code understanding **A LOT**. Never underestimate the **power of semantics** in the code. – Diogo Santo Nov 23 '18 at 12:02
  • please check my code. – Asesha George Nov 23 '18 at 12:57
  • As per my opinion inside do 1st if else has no use 2nd thing if member count is less then 3 then exit from while loop other wise it goes to infinity loop, because $firstsponarray, $asmallestsponid remains same for every loop; – Sachin Nov 27 '18 at 05:54

3 Answers3

5

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);
dWinder
  • 11,597
  • 3
  • 24
  • 39
0

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

Alexey
  • 3,414
  • 7
  • 26
  • 44
  • but my condition is it should only exit if $loopend is equaled to 1 else it should go to top and execute $looparray. – Asesha George Nov 19 '18 at 08:28
  • membercount is fine it's returning the value and satisfying the condition when it comes to else I assigned an array to $looparry while I print it's showing the result I want to use this array at the top so it will do the next step if I use "for" loop I could able to get the $lopparray but in do_while the size of array is zero – Asesha George Nov 19 '18 at 08:34
  • I don't understand. If it goes into `if(membercount($asmallestsponid)<3)` there is a break statement causing to go out of the do/while loop completely if it gets there once. If it gets to else , there is no change to `$loopend` so your do/while can only work once – Alexey Nov 19 '18 at 09:40
  • yes, you are correct if it goes into if(membercount($asmallestsponid)<3) it will exit the loop. but if the condition is not satisfied it will go to else so there is no break statement in else. if member count is less then 3, it will insert a row in my database if it's greater then 3 its will go to else and fetch all the id numbers into an array and go to the top and execute the array. i am getting the array in my $looparray my only concern is how to send this array top of the loop. i hope you understand – Asesha George Nov 19 '18 at 10:02
  • I'm telling you that if it goes into else, it won't change the value of `$loopend` to 1 and this will end the loop as well – Alexey Nov 19 '18 at 19:23
0

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.

José A. Zapata
  • 1,187
  • 1
  • 6
  • 12