Subtract related field problem

Discussion in 'PHP' started by Bobolasko, Jun 21, 2009.

  1. #1
    Hello, I got small problem with subtract related script and not so where to look for solution, I was looking for "do while" kinda loops but couldnt get it working.

    $result = mysql_query("UPDATE user SET hp = hp - $random2 WHERE name='$name'") 
    Code (markup):
    is basic idea, random2 comes from generator script.

    I have user and monster tables, both has same values and their HP is subtracted depending random number generator. Theres php file included which used different number generator to decide ID of monster which user is going to attack.

    Problem: In order to subtract I have to refresh page, which again calls different ID from db and making whole thing start again, example even when ID 1 monster is dead script calls ID 2 by random and user can continue without using "attack button" again.

    What Im looking: Something that subtract hp "automatic" by number generator values until HP is 0, this way theres no need to do refreshing and user cannot press F5 or hit back button to escape death.

    So, is "while do" or "for" loopings way to go or something different, since I havent got loops working at all and have been googling examples for these couple days :( Thanks for reading.

    Im sorry if explaining wasnt smooth, my english isnt top notch :rolleyes: If wanna more information, do ask and I try to provide.
     
    Bobolasko, Jun 21, 2009 IP
  2. manjifera

    manjifera Well-Known Member

    Messages:
    232
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    118
    #2
    $result = mysql_query("UPDATE user SET hp = hp - $random2 WHERE name='$name'")
    =======
    $sub = hp - $random2;

    "update user set hp = '".$sub."' where name = '".$name."'";
    will work fine i guess!!
     
    manjifera, Jun 21, 2009 IP
  3. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #3
    I'm not 100% sure what your going for here, but i'll give it a shot. It's my understanding that you want HP to decrease until it's 0? If so, have a look at this code:

    
    // Set our initial values
    $hp           = 100; // I assume HP is Health?
    $random    = rand(0, 100); // Random hit from 0 pts to 100 pts
    
    // Loop through, subtracting from $hp, as long as it's not 0. When it hits 0
    // It will leave the loop and you'll have $hp = 0
    while($hp >= 0)
    {
        $hp -= $random; // The Minus-Equals (-=) is shorthand for
                                  // $hp = $hp - $random;
    
        // Update the database to reflect new HP
       mysql_query("update user set hp = '$hp' where name = '$name'");
    }
    
    PHP:
    Hope that helps! Feel free to contact me if you have any problems :)
     
    Louis11, Jun 21, 2009 IP
  4. Bobolasko

    Bobolasko Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    All right, I try and provide more clear example...

    <?php { $mobran = rand(1,2);} ?>
     
    <?php If ($numrows3  > 2) {
    $random = rand(13,26);}
    elseif ($numrows3  < 1) {
    $random = rand(8,12);} ?>
    
    <?php If ($numrows3  > 2) {
    $random2 = rand(3,12);}
    elseif ($numrows3  < 1) {
    $random2 = rand(2,9);} ?>
    
    Code (markup):
    depending numrows hp is subtracted like...

    <?php 
    $result = mysql_query("UPDATE user SET hp = hp - $random2 WHERE name='$name'") 
    or die(mysql_error());
    $resulthp = mysql_query("SELECT * FROM user WHERE name='$name' AND hp") or die(mysql_error());  
    $row = mysql_fetch_array( $resulthp );
    $numrows1 = $row['hp'];
    If ($numrows1  > 0) {
    echo "<br><br><b>";  
    echo "Logged as ";
    echo $row['name']; 
    echo "<br>Your HP is ";
    echo $row['hp']; 
    echo "</b><br><br>";}
    elseif ($numrows1  < 0) {
    print("<span>You´re dead</span>");} ?>
    
    <?php 
    $result2 = mysql_query("UPDATE monster SET hp = hp - $random WHERE id='$mobran'") 
    or die(mysql_error());
    $result3 = mysql_query("SELECT * FROM monster WHERE id='$mobran' AND hp") or die(mysql_error());  
    $row2 = mysql_fetch_array( $result3 );
    $numrows2 = $row2['hp'];
    
    If ($numrows2  > 0) {
    echo "<br><b>";  
    echo $row2['mobname']; 
    echo " HP is ";
    echo $row2['hp']; 
    echo "</b><br><br>";}
    elseif ($numrows2  < 0) {
    print("<br><br><span>Enemy is dead</span></td></tr></table>");}
    ?> 
    Code (markup):
    As you can see theres problem, in order to subtract page has to be refresh and user can simply exit page before HP has reach to 0, I would need to make it so that result is provided right away after page loads.

    I tried Louis11 solution but got it to only working so that is takes away user and monster hp to 0 leaving both dead.
     
    Bobolasko, Jun 22, 2009 IP