| | |
Problems with While Loop
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2006
Posts: 20
Reputation:
Solved Threads: 0
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.
[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.
•
•
Join Date: Apr 2006
Posts: 20
Reputation:
Solved Threads: 0
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.
[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.
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??
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??
•
•
Join Date: Apr 2006
Posts: 20
Reputation:
Solved Threads: 0
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.
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
![]() |
Similar Threads
- Reading multiple lines from a txt file (C++)
- Real Quick HW question (C)
- Having trouble displaying an Arrays. Please help. (C)
- code problems (C++)
- Forum lurkers, introduce yourself ... !! (Community Introductions)
- loop and class problem (C++)
- List <char *> Problem (C)
- norton disk doctor reboot for repair in endless loop (Windows NT / 2000 / XP)
- error mgs when trying to compute "for loop" (C++)
Other Threads in the PHP Forum
- Previous Thread: return a value from javascript
- Next Thread: Arrays within $_POST[]?
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube





