Jump to content

Subtract & loops problem.


CoffeeOD

Recommended Posts

Hello, Im currently trying to teach myself abit php and mysql related coding by looking tutorials and combine ideas to my make own script but Im stuck with this quite simple problem, this is subtract and" do while / for" loops related.

 

$result = mysql_query("UPDATE user SET hp = hp - $random2 WHERE name='$name'") 

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.

 

Second problem would be that users can simply exit page and avoid death since their HP hasnt reached 0 yet, again that "forced refresh" related problem.

 

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 simply couldnt get loops working like I wanted. Any information would be nice, Im just trying to get general idea from phpfreaks.com loops tutorial but is there anything else I should look into? Thanks for reading.

not sure would this belong to general php help forum but since subtract is math related assumed this would right place.

Link to comment
https://forums.phpfreaks.com/topic/163180-subtract-loops-problem/
Share on other sites

All right, user goes to attack.php, first there is random number generator.

 

<?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);} ?>

 

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>");}
?> 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.