Page 1 of 1

Loop problem... also need an array but can't put it in right

Posted: Tue Jul 05, 2005 12:44 am
by kcpaige
I need some help with this script.

I want this script to run 100 times, which it does. I want it to echo out $w each time it goes by, and with the variables i've selected, it should say 5 each time and it doesn't.

After that, I need to catch $w and put it in an array. I can't seem to get anything into an array except for the first or last entry so i have removed that section of code. With the variables i have choosen, the final product should be it echoing 5, 100 time, and i have 5 100 times in an array.

I was trying array_push to add $w each time, if that will do the trick, but from what i see it won't. Please help me out with this small part of my script.

x=5
y=100000
z=5

Code: Select all

<?php
$counter=0; 
$maxtimes=100; 
while($counter<$maxtimes) 
{ $counter++;
DO {
$c = 40 * (($_POST[y]++) / $_POST[x]) ;
$num = mt_rand( 1, 100 );
	If ($num <= $c) {
		$w++;
		$_POST[x]++;
		$_POST[z]--;
		$_POST[y]--;
} else {
		$_POST[z]--;
		$_POST[y]--;
	}
} while ($_POST[z] > 0);
Echo "$w<br>";
$w=0;
}
?>

Code: Select all

5
1
1
1
...

Posted: Tue Jul 05, 2005 2:04 am
by timvw
Why do you think it should output 5?

Get yourself some piece of paper... And fill in the numbers yourself..
Easy way to debug and find out where is does things you are probably not expecting...

Code: Select all

|-----|--------------------------------------------------------------|
|line |counter | maxtimes | c | post&#1111;y] | post&#1111;x] | num | w | post&#1111;z]|
|-----|--------------------------------------------------------------|
| 001 |      0 |
| 002 |        |      100 |
| 003 |      
| 004 |      1 | 
...

Re: Loop problem... also need an array but can't put it in r

Posted: Tue Jul 05, 2005 2:05 am
by harrisonad
You are only resetting $w to 0...
kcpaige wrote:$w=0;

Posted: Tue Jul 05, 2005 8:13 am
by kcpaige
after restting $w to zero it should go back thru the loop and become five again... I've debugged it to this point but i don't understand why $w isn't going back up to 5 each time.

Posted: Tue Jul 05, 2005 9:45 am
by kcpaige
Still can't see where the problem is. I tried writing it out, and by my logic it should still echo 5, 100 times. Maybe I just don't understand my while - do while loop or how PHP handles it. :cry:

Posted: Tue Jul 05, 2005 11:56 am
by kcpaige
can anyone help, i don't need the array i just need to find out how to fix my $w...

Posted: Tue Jul 05, 2005 12:08 pm
by timvw
As long as we don't know what it should do we can't help you.... The code does exactly what it is supposed to do (no evil bug in php or what so ever)..

Posted: Tue Jul 05, 2005 12:10 pm
by timvw
Thus -> each cycle $w = $c - $num; // notice that num is random...

Posted: Tue Jul 05, 2005 2:18 pm
by kcpaige

PostPosted: Tue Jul 05, 2005 12:10 pm Post subject:
Thus -> each cycle $w = $c - $num; // notice that num is random...
$w does not = $c - $num.

What I want to have happen, which it should be happening, is the do while loop runs five times because z is posted as 5. When this happens $w is increasd by 5 each time, producing 5 in the echo. I then reset $w to 0 and the loop runs again, $w is 0, and it should run thru the loop 5 more times because z is 5. This should produce 5, 100 times. Is the problem in z???

The reason i am doing this is to make sure $w is what it is "supposed" to be as if doing the loop 100 times. I pciked numbers that will ensure what $w is 5. From this i'll be able to use an array to store the value of $w each time thru, and use the array_sum() function and then dividing that by 100 to give me an average. I need the average of $w because $num is random it changes what $w is with small numbers. This is all a major part of a calculator for a game I play. My only problem is getting $w to run thru the do while loop each time as 0 and the user inputed values to reset to the same as when they were inputed.

Posted: Tue Jul 05, 2005 8:00 pm
by harrisonad
Why not just use this to save you from heavey burden

Code: Select all

for($a=1;$a&lt;=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
;=100;$a++){
echo $a."<br>";
for($b=1;$b<=5;$b++){
/* Rest of the code */
echo &quote; - $b&lt;br&gt;&quote;;
}
}
mp;quote;;
}
}
p;amp;lt;br>";
for($b=1;$b<=5;$b++){
/* Rest of the code */
echo " - $b<br>&quote;;
}
}
php]
for($a=1;$a<=100;$a++){
echo $a."<br>";
for($b=1;$b<=5;$b++){
/* Rest of the code */
echo " - $b<br>";
}
}
[/php:1:8574his to save you from heavey burden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br&his to save you from heavey burden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
[/p 
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
;amp;lt;br>";
for($b=1;$b<=5;$b++){
/* Rest of the code */
echo " - $b<br>";
}
}
om heavey burden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
8574f28652]
for($a=1;$a<=100;$a++){
echo $a.&quote;&lt;br&gt;&quote;;
for($b=1;$b&lt;=5;$b++){
/* Rest of the code */
echo &quote; - $b&lt;br&gt;&quote;;
}
}
8652]gt;";
for($b=1;$b<=5;$b++){
/* Rest of the code */
echo " - $b&am$a<=100;$a++){
echo $a."<br>&quote;;
for($b=1;$b&lt;=5;$b++his to save you from heavey burden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
for($a=1;$a<=100;$a++){
echo $a."<br>";
for($b=1;$b<=5;$b++){
/* Rest of the code */
echo &quote; - $b&lt;br&gt;&quote;;
est of the code */
echo " - $b<br>";
}
}
his to save you from heavey burden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
urden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of th
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code   "<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
his to save you from heavey burden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo "amp;quote;;
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
e; - $b<br>";
}
}
]
for($a=1;$a<=100;$a++){
echo $a."<br>";
for($b=1;$b<=5;$b++){
/* Rest of the code */
echo " - $b<br>";
}
}
e */
echo " - $b<br&amden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
;lt;br>";
for($b=1;$b&lt;=5;$b++){
burden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
his to save you from heavey burden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
code */
echo " - $b<br>";
}
}
mp;amp;gt;";
}
}
$b=1;$b<=5;$b++){
/* Rest of the code */
echo " - $b<br>";
}
}
en

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a.&quote;&lt;br&gt;&quote;;
	for($b=1;$b&lt;=5;$b++){   
		/*      Rest of the code his to save you from heavey burden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
his to save you from heavey burden

Code: Select all

for($a=1;$a<=100;$a++){  
	echo $a."<br>";
	for($b=1;$b<=5;$b++){   
		/*      Rest of the code    */   
		echo " - $b<br>";  
	}  
}
for($a=1;$a<=100;$a++){
echo $a."<br>";
for($b=1;$b<=5;$b++){
/* Rest of the code */
echo " - $b<br>";
}
}