Jump to content

help please


acidpunk

Recommended Posts

does anyone mind helping me with 104 lines of code, wheres theres some error. It's a shown error, it just continues to echo out until my browser shuts down, which is a } somewhere. im exhausted of it, im posting it, someone guide me please. a second eye will do me better here

 

 


include 'head.php';

//global for current raid
$currentraidd= mysql_query("select * from current_crew_raids where timeleft=1");
while ($currentraid = mysql_fetch_array($currentraidd)) {

//global for crew
$gangg= mysql_query("select * from crews where id=$currentraid[crew]");
while ($gang = mysql_fetch_array($gangg)) {



//global for raid.
$raidd= mysql_query("select * from crewraids where id=$currentraid[against]");
while ($raid = mysql_fetch_array($raidd)) {


//create a query using SUM, we need to get all users in raid and add up they're hp.
$query = "SELECT hp, SUM(hp) FROM users WHERE in_raidid='$currentraid[against]'"; 
$result = mysql_query($query) or die(mysql_error());

//result
while($rowhp = mysql_fetch_array($result)){
//echo " ". $row['SUM(hp)'] ." "; //test to check if SUM(hp) works.
        


$crewhp = " ". $rowhp['SUM(hp)'] . " ";


$raidhp = " ". $raid['hp'] . " ";


//} //end result.


//the loop.
while ($crewhp >= 0 AND $raidhp >= 0 AND !$won) {



if (!$won) {

$select34=mysql_query("select * from users where crewid='$gang[id]' and in_raid='Yes' AND in_raidid=$currentraid[against]");
print "<center>";
while($select3=mysql_fetch_array($select34)){

$atk = $select3[attack]+rand(3,10); //user attack damage.

echo " ". $select3[name] . " attacks for " . number_format($atk) . " <br />";


} //end.
} //end.

//create a query using SUM, we need to get all users in raid and add up they're attack.
$query = "SELECT attack, SUM(attack) FROM users WHERE in_raidid='$currentraid[against]'"; 
$result = mysql_query($query) or die(mysql_error());

//result
while($rowattack = mysql_fetch_array($result)){
$totalAttack = 0;

$totalAttack = " ". $totalattack['SUM(attack)'] . " ";



$enemyhp=$raidhp-$totalAttack;


if ($raidhp <= 0) {
$won=yes;

}
}
}
}

if (!$won) {



$raidatk=rand("$raid[attack_low]","$raid[attack_high]")+rand(3,10);


//raid attacks.
print "<br /> " . $raid[name] . " defends for " . number_format($raidatk) . " <br />";

}
}

if ($raidhp <= 0) {

echo "you win";

}else{

echo "you loose";
}
}
}
}

?>

 

Link to comment
Share on other sites

my new code is

 

 

<?php

include 'header.php';

//global for current raid
$currentraidd= mysql_query("select * from current_crew_raids where timeleft=1");
while ($currentraid = mysql_fetch_array($currentraidd)) {

//global for crew
$gangg= mysql_query("select * from crews where id=$currentraid[crew]");
while ($gang = mysql_fetch_array($gangg)) {



//global for raid.
$raidd= mysql_query("select * from crewraids where id=$currentraid[against]");
while ($raid = mysql_fetch_array($raidd)) {


//create a query using SUM, we need to get all users in raid and add up they're hp.
$query = "SELECT hp, SUM(hp) FROM users WHERE in_raidid='$currentraid[against]'"; 
$result = mysql_query($query) or die(mysql_error());









//result
while($rowhp = mysql_fetch_array($result)){
//echo " ". $row['SUM(hp)'] ." "; //test to check if SUM(hp) works.
        


$crewhp = " ". $rowhp['SUM(hp)'] . " ";


$raidhp = " ". $raid['hp'] . " ";


//} //end result.








//the loop.
while ($crewhp >= 0 AND $raidhp >= 0 AND !$won) {



if (!$won) {

$select34=mysql_query("select * from users where crewid='$gang[id]' and in_raid='Yes' AND in_raidid=$currentraid[against]");
print "<center>";
while($select3=mysql_fetch_array($select34)){

$atk = $select3[attack]+rand(3,10); //user attack damage.

echo " ". $select3[name] . " attacks for " . number_format($atk) . " <br />";


} //end.






//create a query using SUM, we need to get all users in raid and add up they're attack.
$query = "SELECT attack, SUM(attack) FROM users WHERE in_raidid='$currentraid[against]'"; 
$result = mysql_query($query) or die(mysql_error());

//result
while($rowattack = mysql_fetch_array($result)){
$totalAttack = 0;

$totalAttack = " ". $rowattack['SUM(attack)'] . " ";



$raidhp=$raidhp-$totalAttack;


if ($raidhp <= 0) {
$won=yes;


}
}





if (!$won) {


a
$raidatk=rand("$raid[attack_low]","$raid[attack_high]")+rand(3,10);


//raid attacks.
print "<br /> " . $raid[name] . " defends for " . number_format($raidatk) . " <br /><br />";

}

if ($raidhp <= 0) {



echo "you win";




}

}

}
}
}
}
}
?>

 

 

i want to add an elseif($crewhp <= 0){ echo "you loose";  } at the end, or a simple }else{ echo "you loose"; } at the end, however, everytime i try, it adds another error. if you can simply fix this with me, try to help, thank you.

Link to comment
Share on other sites

That's one of the reasons we always recommend people to:

  1. [*]Indent their code properly

[*]Never use queries in loops

[*]"Exit early"

That said, I see where your logic is flawed, but you really need to scrap all of those loops. There are 5 completely unnecessary loops in here, and a huge number of wasted queries. All of which are adding to the confusion, and making the script harder to read than needed. So, use a single query with JOIN, add proper indentation and clean up the newlines (unless that's the PHP BB-code bug). Once you've done that, you should be able to see the flaw in your logic. If you still have it, that is.

Link to comment
Share on other sites

I did manage to fix it, it works fine (I think anyways) I tested it with as many possibilities as I could.

 

heres what i got:

 

<?php

include 'header.php';

//global for current raid
$currentraidd= mysql_query("select * from current_crew_raids where timeleft=1");
while ($currentraid = mysql_fetch_array($currentraidd)) {

//global for crew
$gangg= mysql_query("select * from crews where id=$currentraid[crew]");
while ($gang = mysql_fetch_array($gangg)) {

//global for raid.
$raidd= mysql_query("select * from crewraids where id=$currentraid[against]");
while ($raid = mysql_fetch_array($raidd)) {

//create a query using SUM, we need to get all users in raid and add up they're hp.
$query = "SELECT hp, SUM(hp) FROM users WHERE in_raidid='$currentraid[against]'"; 
$result = mysql_query($query) or die(mysql_error());
while($rowhp = mysql_fetch_array($result)){
//echo " ". $row['SUM(hp)'] ." "; //test to check if SUM(hp) works.
        
//set HP for raid & crew.
$crewhp = " ". $rowhp['SUM(hp)'] . " ";
        $raidhp = " ". $raid['hp'] . " ";


//the loop.
while ($crewhp >= 0 AND $raidhp >= 0 AND !$won) {

if (!$won) {
        	$select34=mysql_query("select * from users where crewid='$gang[id]' and in_raid='Yes' AND in_raidid=$currentraid[against]");
	while($select3=mysql_fetch_array($select34)){

		$atk = $select3[attack]+rand(3,10); //user attack damage.
		echo " ". $select3[name] . " attacks for " . number_format($atk) . " <br />\n";

	} //end while

} //end if


//we need to get every user in the raid, and add their attack, for a total attack.
$query = "SELECT attack, SUM(attack) FROM users WHERE in_raidid='$currentraid[against]'"; 
$result = mysql_query($query) or die(mysql_error());
while($rowattack = mysql_fetch_array($result)){

//users total attack.
$totalAttack = 0;
$totalAttack = " ". $rowattack['SUM(attack)'] . " ";
$raidhp=$raidhp-$totalAttack; //subtract the raid's HP from the crews total attack.

if ($raidhp <= 0) {
	$won=yes;

} //end if
} //end while


if (!$won) {

$raidatk=rand("$raid[attack_low]","$raid[attack_high]")+rand(3,10); //raids total attack.
echo "<center><br /> " . $raid[name] . " defends for " . number_format($raidatk) . " <br /><br /></center>";

$crewhp = $crewhp-$raidatk;
$crewhp = round($crewhp);
$raidhp = $raidhp;
$raidhp = round($raidhp);

} //end if.

if ($raidhp <= 0) {
	echo '<br /><br />';
	echo "$raid[name] is DEAD";
	echo '<br /><br />';
	echo "you win";

} //end if.

elseif($crewhp <= 0){
	$won=no;
	echo "you loose";

} //end elseif

} //end while loop

} //end $rowhp while

} //end $raid while

} //end $gang while

} //end $currentraid

?>

 

any suggestions on a better route so I can "scrape all of those loops" I understand they are their, even when I wrote it, I just can't help it, haha, even after a bunch of years of this, i'm still not there. But however; I am trying. So any advice would be nice. Thank you. Asking for help is just a way to learn :)

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.