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
Yes. you can declare a variable inside any loop(includes do while loop.
The while loop is not run because the condition is not met. After the running the for loop the value of variable i is 5, which is greater than three. To fix this you should reassign the value before running the while loop (simply add var i=1; between the for loop and the while loop).
PHP do-while loop can be used to traverse set of code like php while loop. The PHP do-while loop is guaranteed to run at least once. The PHP do-while loop is used to execute a set of code of the program several times.
Different Types of Loops in PHP while — loops through a block of code as long as the condition specified evaluates to true. do… while — the block of code executed once and then condition is evaluated. If the condition is true the statement is repeated as long as the specified condition is true.
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:
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...)
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With