944,112 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3015
  • PHP RSS
Oct 16th, 2006
0

Problems with While Loop

Expand Post »
Im having a problem that my while loop is not finishing even when the conditions have been meet.

[php]$turn = "0";
while ($player['health_n'] > "0" OR $opponent['health_n'] > "0" AND $turn < "25"){
$turn = $turn+1;
$hitfor = $youatk / $oppdef;
if ($hitfor <= "0"){
echo "$turn. $opponent[name] blocked.<br />";
}else{
echo "$turn. You hit $opponent[name] for $hitfor damage.<br />";
mysql_query("UPDATE members SET health_n=health_n-$hitfor WHERE id='$opponent[id]' ")or die(mysql_error());
}
$turn = $turn+1;
$atkfor = $oppatk / $youdef;
if ($atkfor <= "0"){
echo "$turn. You blocked.<br />";
}else{
echo "$turn. $opponent[name] hit you for $atkfor damage.<br />";
mysql_query("UPDATE members SET health_n=health_n-$atkfor WHERE id='$player[id]' ")or die(mysql_error());
}
}[/php]

I am getting the data from the database outside of the while loop; if this is any help.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ries is offline Offline
20 posts
since Apr 2006
Oct 16th, 2006
0

Re: Problems with While Loop

try this:
[php]
while ($player['health_n'] || $opponent['health_n'] && $turn < 25)

[/php]
If a variable is zero it can be thought of as FALSE, and also you were looking for $turn that was greater than the string 25. see if that helps.
Reputation Points: 10
Solved Threads: 2
Junior Poster
sn4rf3r is offline Offline
133 posts
since Sep 2006
Oct 17th, 2006
0

Re: Problems with While Loop

That sorta worked except it was checking the right thing, I wanted it to perform the loop if the player health was above 0 OR opponent health was above 0 OR turns where below 25. So something like this

[php]while ($player['health_n'] > "0" || $opponent['health_n'] > "0" || $turn < 25)[/php]

Except it does not work. This happens whenever I do == or > or => or althing like that with the player/opponent health and so your previous example not using any conditional for the arrays worked but it did not create the desired effect.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ries is offline Offline
20 posts
since Apr 2006
Oct 17th, 2006
0

Re: Problems with While Loop

you are checking the value of strings and NOT integers. stop putting the numbers in quotes, you dont need to do that.

Also, this snippet you posted is different, you have three || statements not 2 || and an && statemnt. is that what you wanted to do all along?
Have you tried echoing out the variables in your script, before after and during your loop??
Reputation Points: 10
Solved Threads: 2
Junior Poster
sn4rf3r is offline Offline
133 posts
since Sep 2006
Oct 17th, 2006
0

Re: Problems with While Loop

Yes having 3 || is what I wanted.

EDIT: Ok I've just tried removing the "" and it still carry's on after the turns have reached above 25 and the health is below 0. i'll show you my updated code

[php]while ($player['health_n'] > 0 || $opponent['health_n'] > 0 || $turn < 25){
$turn = $turn+1;
$hitfor = $youatk / $oppdef;
if ($hitfor <= "0"){
echo "$turn. $opponent[name] blocked.<br />";
}else{
echo "$turn. You hit $opponent[name] for $hitfor damage.<br />";

mysql_query("UPDATE members SET health_n=health_n-$hitfor WHERE id='$opponent[id]' ")or die(mysql_error());
}
$turn = $turn+1;
$atkfor = $oppatk / $youdef;
if ($atkfor <= "0"){
echo "$turn. You blocked.<br />";
}else{
echo "$turn. $opponent[name] hit you for $atkfor damage.<br />";
mysql_query("UPDATE members SET health_n=health_n-$atkfor WHERE id='$player[id]' ")or die(mysql_error());

}
}[/php]

Basically this loop is supposed to run until the health of someone reaches 0 or turns get higher than 25 (Whichever comes first.) And then after this I want to give the player bonuses for winning so I have:
[php]if ($player['health_n'] <= "0"){[/php]

Too check if he lost and

[php]}elseif ($opponent['health_n'] <= "0"){[/php]

If he won (Also a if condition to check if the 25 turn limit was reached and so to set it as a draw.)

But whats happening is that while loop above isn't stopping, it carrys on and on.
Last edited by Ries; Oct 17th, 2006 at 3:22 pm. Reason: Dont want to double-post
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ries is offline Offline
20 posts
since Apr 2006
Oct 17th, 2006
0

Re: Problems with While Loop

[php]
while ($player['health_n'] > "0" OR $opponent['health_n'] > "0" AND $turn < "25"){
[/php]
is from your first post. I guess I was confused at what your intentions were...
Reputation Points: 10
Solved Threads: 2
Junior Poster
sn4rf3r is offline Offline
133 posts
since Sep 2006
Oct 17th, 2006
0

Re: Problems with While Loop

Must have not been thinking when I was coding it, sorry for misleading ya and wasting your time.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ries is offline Offline
20 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: return a value from javascript
Next Thread in PHP Forum Timeline: Arrays within $_POST[]?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC