Jump to content

Why Isnt it Looping?


supanoob

Recommended Posts

[code]while ( $health > 0 AND $health2 > 0 )
{

echo "You Hit Dragon for $truedam.<br>";

// update dragon health
$newhealth2=($health2-$truedam);
$sql2="UPDATE players SET health='$newhealth2' WHERE user='Dragon'";
if(mysql_query($sql2))

if($health2 < 1)
{
echo "you have defeated the dragon";

die();
}

echo "Dragon Hit You For $truedam2.<br>";


// update player health
$newhealth=($health-$truedam2);
$sql2="UPDATE players SET health='$newhealth' WHERE user='$user'";
if(mysql_query($sql2))

if($health < 1)
{
echo "Dragon Has Killed You UNLUCKY";

die();

}

die();
}[/code]

can someone tell me why it isnt looping until health is less than 1
Link to comment
Share on other sites

not having your tables or other info I built this that works until zero, maybe you can peice the parts you need back in.

I added variable to help me test it.

[code]
<?php
$h1 = "5";
$h2 = "4";
$damage = "1";

while($h1 > 0 && $h2 > 0)
{    
    $h1 = ($h1 - $damage);
    print "The dragon hit you for $damage<br/>";
    print "Your health is:$h1<br/><br/>";
    
    if($h1 == 0 )
    {
        print "The Dragon Has Killed You";
        exit;
    }
    
    $h2 = ($h2 - $damage);
    print "You hit the Dragon for $damage<br/>";
    print "The Dragon's health is:$h2 <br/><br/>";    
    
    if ($h2 == 0)
    {
        print "You have killed the Dragon<br/>";
        print "Your health is now:$h1";
        exit;
    }
}
?>
[/code]
Link to comment
Share on other sites

Do you have this in a function or a class? I tried to build this into a funtion and got the same results. Post up the full function if you can I'd like to take a look at it.

If you built this into a function remember you need to bring the damage variable in, as it is not taking anything away and just looping forever.

Link to comment
Share on other sites

[code]
while ( $health > 0 AND $health2 > 0 )
{

echo "You Hit Dragon for $truedam.<br>";

// update dragon health
$newhealth2=($health2-$truedam);
$sql2="UPDATE players SET health='$newhealth2' WHERE user='Dragon'";
$query = mysql_query($sql2);

if($health2 < 1)
{
echo "you have defeated the dragon";

die();
}


echo "Dragon Hit You For $truedam2.<br>";

// update player health
$newhealth=($health-$truedam2);
$sql2="UPDATE players SET health='$newhealth' WHERE user='$user'";
$query2 = mysql_query($sql2);

if($health < 1)
{
echo "Dragon Has Killed You UNLUCKY";

die();

}
}
[/code]

Try that.
Link to comment
Share on other sites

[!--quoteo(post=378180:date=May 29 2006, 02:35 PM:name=ale_jrb)--][div class=\'quotetop\']QUOTE(ale_jrb @ May 29 2006, 02:35 PM) [snapback]378180[/snapback][/div][div class=\'quotemain\'][!--quotec--]


Try that.
[/quote]


Well, that does loop, but it doesnt stop when life hits 0 :S and it only does one round. he hit me for 73 and it did like loads of You hit dragon for 10, he hit you for 73, and it only took 73 life off :S
Link to comment
Share on other sites

[!--quoteo(post=378151:date=May 29 2006, 01:12 PM:name=homchz)--][div class=\'quotetop\']QUOTE(homchz @ May 29 2006, 01:12 PM) [snapback]378151[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Do you have this in a function or a class? I tried to build this into a funtion and got the same results. Post up the full function if you can I'd like to take a look at it.

If you built this into a function remember you need to bring the damage variable in, as it is not taking anything away and just looping forever.
[/quote]


the full php+html is below:

[code]<?php
session_start();

require_once('header.php');

if (!$_SESSION['valid_user'])

{ echo 'you are not logged in';

die();
}
$user=$_SESSION['valid_user'];

//queries the things needed for the player.

$query="select playerid, user, name, email, gold, fatigue, lose, win, maxfat, power, speed, dex, intel, health, maxhealth, gender, battle from players where user='$user'";
$result=mysql_query($query);
if (!$result)
{
die (mysql_error());
}

//queries the things need for the NPC in this case the dragon.

$query2="select playerid, user, name, email, gold, fatigue, maxfat, power, speed, dex, intel, health, maxhealth, gender from players where user='Dragon'";
$result2=mysql_query($query2);
if (!$result2)
{
die (mysql_error());
}

$num_rows=mysql_num_rows($result);

//fetching the rows needed for the player.

$row=mysql_fetch_array($result);


$playerid=($row['playerid']);
$name=($row['name']);
$gold=($row['gold']);
$user=($row['user']);
$fatigue=($row['fatigue']);
$maxfat=($row['maxfat']);
$power=($row['power']);
$speed=($row['speed']);
$dex=($row['dex']);
$intel=($row['intel']);
$health=($row['health']);
$maxhealth=($row['maxhealth']);
$gender=($row['gender']);
$battle=($row['battle']);
$lose=($row['lose']);
$win=($row['win']);

$_SESSION['valid_user'] = $user;

//fetching the rows needed for the NPC in this case the Dragon

$row=mysql_fetch_array($result2);

$name2=($row['name']);
$power2=($row['power']);
$speed2=($row['speed']);
$dex2=($row['dex']);
$health2=($row['health']);
$maxhealth2=($row['maxhealth']);
?>



<HTML>
<HEAD>
<style type="text/css" media="all">
        @import "css.php";
    </style>
    <TITLE>TWOTTK.COM</TITLE>
</HEAD>
<TABLE width=100% border=0 cellpadding=0 cellspacing=0>
<TR>
    <TD colspan=5>
        <p align="center">
        <img src="http://www.twottk.com/game/banner.jpg" width="730" height="160"></p>
        <TABLE border=0 cellpadding=0 cellspacing=0>
            <TR><TD>
              
              </TD></TR>
        </TABLE>
<center><p><nobr><a href="home.php">Home</a> |
<a href="town.php">Town</a> |
<a href="search.php">Search</a> |
<a href="hospital.php">Hospital</a> |
<a href="gym.php">Gym</a> |
<a href="http://www.twottk.com/forum/">Forum</a> |
<a href="account.php">Account</a> |
<nobr><a href="logout.php">Logout</a></nobr> </center>
    </TD>
</TR>
<TR height=20><TD colspan=5 align=left><!-- SPACER --></TD></TR>
<TR>
    <TD align=left width=5><!-- SPACER --></TD>
    <TD align=left WIDTH=20% valign=top>
        <TABLE border=0 cellpadding=0 cellspacing=0 height="85" width="164">
        <TR>
            <TD colspan=2 width=164 height="17"><hr></TD>
        </TR>
        <TR>
        <?php
require_once('cssloggedin.php');

?>

                        <TR>
            <TD colspan=2 width=164 height="30">
            Game Stats:<br>
        Name:
           <?php
        echo "$name ($playerid)";
        ?><br>
        Gender:
        <?php
           echo "$gender";
        ?><br>
        Gold:
        <?php
           echo "$gold";
        ?><br>
            Health:
            <?php
            echo "$health / $maxhealth";
            ?><br>
            Fatigue:
            <?php
            echo "$fatigue / $maxfat";
            ?><hr>
            <p></TD>
        </TR>

        <TR height=20>
            <TD height="1" width="164"><!-SPACER-></TD>
        </TR>

        <TR>
            <TD colspan=2 width=164 height="1">
            </TD>
        </TR>
        <TR>
            <TD width=164 bgcolor=#FFFFFF valign=top height="12"><samp>Logged
            In:
            <?php
            echo date ('jS F'); ?><p></samp></TD>
            <TD width=1 bgcolor=#FFFFFF height="12"></TD>
        </TR>
        <TR>
            <TD colspan=2 width=164 height="12">
<hr>            </TD>
        </TR>
        </TABLE>

        <BR>

    </TD>

    <TD align=left width=1%><!-- SPACER --></TD>

    <TD align=left WIDTH=78% valign=top>
    <TABLE width=744 border=0 cellpadding=0 cellspacing=0 height="12">
            <TR>
                <TD valign=top align=left width=4 height="12"></TD>
                <TD valign=top align=center background=templates/default/images/menuback.gif width="736" height="12"><hr></TD>
                <TD valign=top align=right width=4 height="12"></TD>
            </TR>
        </TABLE><TABLE width=740 border=0 cellpadding=0 cellspacing=0>
            <TR>
                <TD width=1 bgcolor=#FFFFFF></TD>
                <TD bgcolor=#FFFFFF width="733" >

<?php

$i=0;

// Battle forumla for Player
$damage=($power*3);
$rand=rand(1,10);
$rand=($rand/10);
$dex3=($dex2*$rand);
$dex2=($dex*2);
$truedam=($damage-$dex2);

// battle forumla for dragon
$damage4=($power2*3);
$rand=rand(1,10);
$rand=($rand/10);
$dex3=($dex2*$rand);
$truedam2=($damage4-$dex3);



while ( $health > 0 AND $health2 > 0 )
{

echo "You Hit Dragon for $truedam.<br>";

// update dragon health
$newhealth2=($health2-$truedam);
$sql2="UPDATE players SET health='$newhealth2' WHERE user='Dragon'";
$query = mysql_query($sql2);

if($newhealth2 < 1)
{
echo "you have defeated the dragon";

die();
}


echo "Dragon Hit You For $truedam2.<br>";

// update player health
$newhealth=($health-$truedam2);
$sql2="UPDATE players SET health='$newhealth' WHERE user='$user'";
$query2 = mysql_query($sql2);

if($newhealth < 1)
{
echo "Dragon Has Killed You UNLUCKY";

die();

}
}

?>

                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</TD>
            </TR>
        </TABLE>
        <p>&nbsp;</p>
        <TABLE width=748 border=0 cellpadding=0 cellspacing=0 height="39">
            <TR>
                <TD valign=top align=left width=4 height="39">&nbsp;</TD>
                <TD align=center background=templates/default/images/menu_bottom.gif width="740" height="39"><hr></TD>
                <TD valign=top align=right width=4 height="39">&nbsp;</TD>
            </TR>
        </TABLE>
        <br><CENTER>

        <p></p>

        </CENTER>
    </TD>
    <TD align=left width=10><!-- SPACER --></TD>
</TR>
</TABLE>


</BODY>

</HTML>[/code]
Link to comment
Share on other sites

You do not seem to be redefining the health variables in the where statement.

Try this:
[code]
<?php
session_start();

require_once('header.php');

if (!$_SESSION['valid_user'])

{ echo 'you are not logged in';

die();
}
$user=$_SESSION['valid_user'];

//queries the things needed for the player.

$query="select playerid, user, name, email, gold, fatigue, lose, win, maxfat, power, speed, dex, intel, health, maxhealth, gender, battle from players where user='$user'";
$result=mysql_query($query);
if (!$result)
{
die (mysql_error());
}

//queries the things need for the NPC in this case the dragon.

$query2="select playerid, user, name, email, gold, fatigue, maxfat, power, speed, dex, intel, health, maxhealth, gender from players where user='Dragon'";
$result2=mysql_query($query2);
if (!$result2)
{
die (mysql_error());
}

$num_rows=mysql_num_rows($result);

//fetching the rows needed for the player.

$row=mysql_fetch_array($result);


$playerid=($row['playerid']);
$name=($row['name']);
$gold=($row['gold']);
$user=($row['user']);
$fatigue=($row['fatigue']);
$maxfat=($row['maxfat']);
$power=($row['power']);
$speed=($row['speed']);
$dex=($row['dex']);
$intel=($row['intel']);
$health=($row['health']);
$maxhealth=($row['maxhealth']);
$gender=($row['gender']);
$battle=($row['battle']);
$lose=($row['lose']);
$win=($row['win']);

$_SESSION['valid_user'] = $user;

//fetching the rows needed for the NPC in this case the Dragon

$row=mysql_fetch_array($result2);

$name2=($row['name']);
$power2=($row['power']);
$speed2=($row['speed']);
$dex2=($row['dex']);
$health2=($row['health']);
$maxhealth2=($row['maxhealth']);
?>



<HTML>
<HEAD>
<style type="text/css" media="all">
        @import "css.php";
    </style>
    <TITLE>TWOTTK.COM</TITLE>
</HEAD>
<TABLE width=100% border=0 cellpadding=0 cellspacing=0>
<TR>
    <TD colspan=5>
        <p align="center">
        <img src="http://www.twottk.com/game/banner.jpg" width="730" height="160"></p>
        <TABLE border=0 cellpadding=0 cellspacing=0>
            <TR><TD>
              
              </TD></TR>
        </TABLE>
<center><p><nobr><a href="home.php">Home</a> |
<a href="town.php">Town</a> |
<a href="search.php">Search</a> |
<a href="hospital.php">Hospital</a> |
<a href="gym.php">Gym</a> |
<a href="http://www.twottk.com/forum/">Forum</a> |
<a href="account.php">Account</a> |
<nobr><a href="logout.php">Logout</a></nobr> </center>
    </TD>
</TR>
<TR height=20><TD colspan=5 align=left><!-- SPACER --></TD></TR>
<TR>
    <TD align=left width=5><!-- SPACER --></TD>
    <TD align=left WIDTH=20% valign=top>
        <TABLE border=0 cellpadding=0 cellspacing=0 height="85" width="164">
        <TR>
            <TD colspan=2 width=164 height="17"><hr></TD>
        </TR>
        <TR>
        <?php
require_once('cssloggedin.php');

?>

                        <TR>
            <TD colspan=2 width=164 height="30">
            Game Stats:<br>
        Name:
           <?php
        echo "$name ($playerid)";
        ?><br>
        Gender:
        <?php
           echo "$gender";
        ?><br>
        Gold:
        <?php
           echo "$gold";
        ?><br>
            Health:
            <?php
            echo "$health / $maxhealth";
            ?><br>
            Fatigue:
            <?php
            echo "$fatigue / $maxfat";
            ?><hr>
            <p></TD>
        </TR>

        <TR height=20>
            <TD height="1" width="164"><!-SPACER-></TD>
        </TR>

        <TR>
            <TD colspan=2 width=164 height="1">
            </TD>
        </TR>
        <TR>
            <TD width=164 bgcolor=#FFFFFF valign=top height="12"><samp>Logged
            In:
            <?php
            echo date ('jS F'); ?><p></samp></TD>
            <TD width=1 bgcolor=#FFFFFF height="12"></TD>
        </TR>
        <TR>
            <TD colspan=2 width=164 height="12">
<hr>            </TD>
        </TR>
        </TABLE>

        <BR>

    </TD>

    <TD align=left width=1%><!-- SPACER --></TD>

    <TD align=left WIDTH=78% valign=top>
    <TABLE width=744 border=0 cellpadding=0 cellspacing=0 height="12">
            <TR>
                <TD valign=top align=left width=4 height="12"></TD>
                <TD valign=top align=center background=templates/default/images/menuback.gif width="736" height="12"><hr></TD>
                <TD valign=top align=right width=4 height="12"></TD>
            </TR>
        </TABLE><TABLE width=740 border=0 cellpadding=0 cellspacing=0>
            <TR>
                <TD width=1 bgcolor=#FFFFFF></TD>
                <TD bgcolor=#FFFFFF width="733" >

<?php

$i=0;

// Battle forumla for Player
$damage=($power*3);
$rand=rand(1,10);
$rand=($rand/10);
$dex3=($dex2*$rand);
$dex2=($dex*2);
$truedam=($damage-$dex2);

// battle forumla for dragon
$damage4=($power2*3);
$rand=rand(1,10);
$rand=($rand/10);
$dex3=($dex2*$rand);
$truedam2=($damage4-$dex3);



while ( $health > 0 AND $health2 > 0 )
{

echo "You Hit Dragon for $truedam.<br>";

// update dragon health
$newhealth2=($health2-$truedam);
$health2 = $newhealth2;
$sql2="UPDATE players SET health='$newhealth2' WHERE user='Dragon'";
$query = mysql_query($sql2);

if($health2 == 0)
{
echo "you have defeated the dragon";

die();
}


echo "Dragon Hit You For $truedam2.<br>";

// update player health
$newhealth=($health-$truedam2);
$health = $newhealth;
$sql2="UPDATE players SET health='$newhealth' WHERE user='$user'";
$query2 = mysql_query($sql2);

if($health == 0)
{
echo "Dragon Has Killed You UNLUCKY";

die();

}
}

?>

                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</TD>
            </TR>
        </TABLE>
        <p>&nbsp;</p>
        <TABLE width=748 border=0 cellpadding=0 cellspacing=0 height="39">
            <TR>
                <TD valign=top align=left width=4 height="39">&nbsp;</TD>
                <TD align=center background=templates/default/images/menu_bottom.gif width="740" height="39"><hr></TD>
                <TD valign=top align=right width=4 height="39">&nbsp;</TD>
            </TR>
        </TABLE>
        <br><CENTER>

        <p></p>

        </CENTER>
    </TD>
    <TD align=left width=10><!-- SPACER --></TD>
</TR>
</TABLE>


</BODY>

</HTML>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.