Jump to content

Recommended Posts

if ( $user->agility > $enemy->agility)

{

$firstattack = '1';

}

else

{

$firstattack = '2';

}

 

while ($user->hp > 0 && $enemy->hp > 0)

{

if ($firstattack == 1)

{

$dmg = ($user->attack) - ($enemy->defense);

mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $_GET[iD]");

echo "$user->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$enemy->display_name";

}

if ($firstattack == 2)

{

$dmg = ($enemy->attack) - ($user->defense);

mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $user->ID");

echo "$enemy->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$user->display_name";

}

if ($firstattack != 1)

{

$dmg = ($user->attack) - ($enemy->defense);

mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $_GET[iD]");

echo "$user->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$enemy->display_name";

}

if ($firstattack != 2)

{

$dmg = ($enemy->attack) - ($user->defense);

mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $user->ID");

echo "$enemy->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$user->display_name";

}

}

 

 

thats my code, its my first time  using the while() function, and im not very clear with it.

 

no errors show up, and nothing happens, i mean, the page stays blank.  can anyone help me?

 

is it the &&?

Link to comment
https://forums.phpfreaks.com/topic/64649-solved-weird-while-problem/
Share on other sites

  • Replies 82
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

OK add this

if ( $user->agility > $enemy->agility)
{
$firstattack = '1';
}
else
{
$firstattack = '2';
}

while ($user->hp > 0 && $enemy->hp > 0)
{
die("IN the WHILE LOOP ");//ADD THIS
echo "attack<br>"; //add
flush(); //add
if ($firstattack == 1)
{
$dmg = ($user->attack) - ($enemy->defense);
mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $_GET[iD]");
echo "$user->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$enemy->display_name";
}
if ($firstattack == 2)
{
$dmg = ($enemy->attack) - ($user->defense);
mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $user->ID");
echo "$enemy->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$user->display_name";
}
if ($firstattack != 1)
{
$dmg = ($user->attack) - ($enemy->defense);
mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $_GET[iD]");
echo "$user->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$enemy->display_name";
}
if ($firstattack != 2)
{
$dmg = ($enemy->attack) - ($user->defense);
mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $user->ID");
echo "$enemy->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$user->display_name";
}
}

 

if you dont get a message "IN the WHILE LOOP " then your values are lower than 0,

comment that line out and see what happens

use the # button (on the toolbar) AKA


[quote author=MadTechie link=topic=154575.msg669149#msg669149 date=1187009615]
if you dont get a message "IN the WHILE LOOP " then your values are lower than 0,
comment that line out and see what happens
[/quote]

add to the start
[code]
echo "User  HP:".$user->hp;
echo "<br>Enemy HP:".$enemy->hp;

 

your see the theirs no value

are you including the class ?

you are right, there is no value for enemy hp, but i set it...

 

 

 

<?php
include('inc/header.php');





if (isset($_GET[iD]));
{
$result2 = @mysql_query("SELECT * FROM users WHERE ID = '$_GET[iD]'"); 
$temp3 = @mysql_fetch_object($result2);
$enemy = $temp3; 
echo "$user->display_name"; echo ' VS. ';echo "$enemy->display_name<br>";
}
if ($enemy->ID == $user->ID)
{
echo' You cant attack yourself!';
}
elseif ($user->hp <= 0)
{echo ' You are in NO condition to do battle';
}
elseif ($enemy->hp = 0)
{
echo ' Your Enemy Is Already Dead!';
}
elseif ($user->energy <= 4)
{
echo ' Not enough energy!';
}
elseif ($user->attack < $enemy->defense)
{
echo ' You look at your opponent, and wet your pants at his/her strength.  You lose the battle.<br><br><br><br><br><br><br><br><br>';
mysql_query("UPDATE `users` SET hp = 0 WHERE ID = '$user->ID'");
}
elseif ($user->defense > $enemy->attack)
{
echo' Your opponent takes one look at you, and dies in fear<br><br><br><br><br><br><br><br><br>';
mysql_query("UPDATE `users` SET hp = 0 WHERE ID = '$_GET[iD]'");
}
else
{
echo' Welcome to the Battle Arena.';
if ( $user->agility > $enemy->agility)
{
$firstattack = '1';
}
else
{
$firstattack = '2';
}
echo "User  HP:".$user->hp;
echo "<br>Enemy HP:".$enemy->hp;
while ($user->hp > 0 && $enemy->hp > 0)
{

echo "attack<br>"; //add
flush(); //add
if ($firstattack == 1)
{
$dmg = ($user->attack) - ($enemy->defense);
mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $_GET[iD]");
echo "$user->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$enemy->display_name";
}
if ($firstattack == 2)
{
$dmg = ($enemy->attack) - ($user->defense);
mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $user->ID");
echo "$enemy->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$user->display_name";
}
if ($firstattack != 1)
{
$dmg = ($user->attack) - ($enemy->defense);
mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $_GET[iD]");
echo "$user->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$enemy->display_name";
}
if ($firstattack != 2)
{
$dmg = ($enemy->attack) - ($user->defense);
mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $user->ID");
echo "$enemy->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$user->display_name";
}
}
}
include('inc/footer.php');
?>

 

 

Thats the full code on the page

 

 

 

on the page, it show:

 

GOD VS. CatNCobra

Welcome to the Battle Arena.User HP:190

Enemy HP:0

 

which means that enemy->display_name is working, because the enemies name is there, right?

change the

$enemy->ID

to

$enemy['ID']

 

$enemy->hp

to

$enemy['hp']

etc etc etc etc

 

try a few and check it works then continue

 

 

EDIT AHHH wait a sec..

 

echo "$user->display_name"; echo ' VS. ';echo "$enemy->display_name<br>";

 

worked !

 

*thinking*

 

try print_r($enemy), and post the result from that

ok try this

<?php
include('inc/header.php');



$eID = $enemy->ID;
$uID = (int)$_GET['ID'];
if ($uID > 0) //changed
{
$enemysql = @mysql_query("SELECT * FROM users WHERE ID = '$eID'"); 
$enemy = @mysql_fetch_object($enemysql);
$usersql = @mysql_query("SELECT * FROM users WHERE ID = '$uID'"); 
$user = @mysql_fetch_object($usersql);

echo "$user->display_name"; echo ' VS. ';echo "$enemy->display_name<br>";
}else{
die("error");
}
if ($enemy->ID == $user->ID)
{
echo' You cant attack yourself!';
}
elseif ($user->hp <= 0)
{
echo ' You are in NO condition to do battle';
}
elseif ($enemy->hp = 0)
{
echo ' Your Enemy Is Already Dead!';
}
elseif ($user->energy <= 4)
{
echo ' Not enough energy!';
}
elseif ($user->attack < $enemy->defense)
{
echo ' You look at your opponent, and wet your pants at his/her strength.  You lose the battle.<br><br><br><br><br><br><br><br><br>';
mysql_query("UPDATE `users` SET hp = 0 WHERE ID = '$user->ID'");
}
elseif ($user->defense > $enemy->attack)
{
echo' Your opponent takes one look at you, and dies in fear<br><br><br><br><br><br><br><br><br>';
mysql_query("UPDATE `users` SET hp = 0 WHERE ID = '$_GET[iD]'");
}
else
{
echo' Welcome to the Battle Arena.';
if ( $user->agility > $enemy->agility)
{
	$firstattack = '1';
}else{
	$firstattack = '2';
}
echo "User  HP:".$user->hp;
echo "<br>Enemy HP:".$enemy->hp;
while ($user->hp > 0 && $enemy->hp > 0)
{
	$enemysql = @mysql_query("SELECT * FROM users WHERE ID = '$uID'"); 
	$enemy = @mysql_fetch_object($enemysql);
	$usersql = @mysql_query("SELECT * FROM users WHERE ID = '$uID'"); 
	$user = @mysql_fetch_object($usersql);

	echo "attack<br>"; //add
	flush(); //add
	if ($firstattack == 1)
	{
		$dmg = ($user->attack) - ($enemy->defense);
		mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $_GET[iD]");
		echo "$user->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$enemy->display_name";
	}
	if ($firstattack == 2)
	{
		$dmg = ($enemy->attack) - ($user->defense);
		mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $user->ID");
		echo "$enemy->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$user->display_name";
	}
	if ($firstattack != 1)
	{
		$dmg = ($user->attack) - ($enemy->defense);
		mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $_GET[iD]");
		echo "$user->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$enemy->display_name";
	}
	if ($firstattack != 2)
	{
		$dmg = ($enemy->attack) - ($user->defense);
		mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $user->ID");
		echo "$enemy->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$user->display_name";
	}
}
}
include('inc/footer.php');
?>

CatNCobra VS. CatNCobra

You cant attack yourself!This page took 0.000095 seconds to load.

 

 

why is he attacking himself now?

 

$enemysql = @mysql_query("SELECT * FROM users WHERE ID = '$uID'");

$enemy = @mysql_fetch_object($enemysql);

$usersql = @mysql_query("SELECT * FROM users WHERE ID = '$uID'");

$user = @mysql_fetch_object($usersql);

 

 

thats the problem

 

*edit* user is set in the header include on the top of the code

 

why set it again, and the same value as enemy?

more problems:

 

u didnt set eID at all

 

and this is what shows up:

 

CatNCobra VS.

Your opponent takes one look at you, and dies in fear

 

*Edit*

like i said previously, USER is set in the header file, so should i remove that part of codE?

 

 

 

noticed ur error, the get[id] gets the enemie's ID from the url

and the user is always included in the file header

 

i modified ur code into this:

 

$uID = $user->ID;
$eID = (int)$_GET['ID'];
if ($uID > 0) //changed
{
$enemysql = @mysql_query("SELECT * FROM users WHERE ID = '$eID'"); 
$enemy = @mysql_fetch_object($enemysql);
$usersql = @mysql_query("SELECT * FROM users WHERE ID = '$uID'"); 
$user = @mysql_fetch_object($usersql);

echo "$user->display_name"; echo ' VS. ';echo "$enemy->display_name<br>";
}else{
die("error");

 

and this shows up

 

GOD VS. CatNCobra

Welcome to the Battle Arena.User HP:190

Enemy HP:0

 

i wrote stuff on my previous post, after changing ur code a bit, i think its starting to work, up until the while() code

 

 

 

lol, sure, anything that can help!

 

(PS thanks for helping me so much already, and thanks for sticking with this.)

 

i ono why the enemy HP is showing as 0, ID 9 in mysql is 100 hp

OK

<?php
include('inc/header.php');

$eID = (int)$_GET['ID'];
$uID = $user->ID;

if ($uID > 0)
{
#commented out if called from header!
#$usersql = @mysql_query("SELECT * FROM users WHERE ID = '$uID'"); 
#$user = @mysql_fetch_object($usersql);
$enemysql = @mysql_query("SELECT * FROM users WHERE ID = '$eID'"); 
$enemy = @mysql_fetch_object($enemysql);


echo "$user->display_name"; echo ' VS. ';echo "$enemy->display_name<br>";
}else{
die("error");
}
if ($enemy->ID == $user->ID)
{
echo' You cant attack yourself!';
}
elseif ($user->hp <= 0)
{
echo ' You are in NO condition to do battle';
}
elseif ($enemy->hp = 0)
{
echo ' Your Enemy Is Already Dead!';
}
elseif ($user->energy <= 4)
{
echo ' Not enough energy!';
}
elseif ($user->attack < $enemy->defense)
{
echo ' You look at your opponent, and wet your pants at his/her strength.  You lose the battle.<br><br><br><br><br><br><br><br><br>';
mysql_query("UPDATE `users` SET hp = 0 WHERE ID = '$user->ID'");
}
elseif ($user->defense > $enemy->attack)
{
echo' Your opponent takes one look at you, and dies in fear<br><br><br><br><br><br><br><br><br>';
mysql_query("UPDATE `users` SET hp = 0 WHERE ID = '$enemy->ID'");
}
else
{
echo' Welcome to the Battle Arena.';
if ( $user->agility > $enemy->agility)
{
	$firstattack = '1';
}else{
	$firstattack = '2';
}
echo "User  HP:".$user->hp;
echo "<br>Enemy HP:".$enemy->hp;
while ($user->hp > 0 && $enemy->hp > 0)
{		
	echo "attack<br>"; //add
	flush(); //add
	if ($firstattack == 1)
	{
		$dmg = ($user->attack) - ($enemy->defense);
		mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $user->ID");
		echo "$user->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$enemy->display_name";
	}
	if ($firstattack == 2)
	{
		$dmg = ($enemy->attack) - ($user->defense);
		mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $enemy->ID");
		echo "$enemy->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$user->display_name";
	}
	if ($firstattack != 1)
	{
		$dmg = ($user->attack) - ($enemy->defense);
		mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $user->ID");
		echo "$user->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$enemy->display_name";
	}
	if ($firstattack != 2)
	{
		$dmg = ($enemy->attack) - ($user->defense);
		mysql_query("UPDATE `users` SET hp = hp-($dmg) WHERE ID = $enemy->ID");
		echo "$enemy->display_name"; echo' has done '; echo "$dmg"; echo ' damage against '; echo "$user->display_name";
	}

	//get new values
	$enemysql = @mysql_query("SELECT * FROM users WHERE ID = '$uID'"); 
	$enemy = @mysql_fetch_object($enemysql);
	$usersql = @mysql_query("SELECT * FROM users WHERE ID = '$eID'"); 
	$user = @mysql_fetch_object($usersql);

}
}
include('inc/footer.php');
?>

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.