acidpunk Posted August 18, 2012 Share Posted August 18, 2012 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"; } } } } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 18, 2012 Share Posted August 18, 2012 What is the error message? Quote Link to comment Share on other sites More sharing options...
acidpunk Posted August 18, 2012 Author Share Posted August 18, 2012 there isn't any, it just continues to echo out until my browser gets an "oh snap" error. just doesn't stop executing. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 18, 2012 Share Posted August 18, 2012 Umm, yeah. Just indented your code to make sense of it, and I see you're running queries in nested loops SIX LEVELS deep. It's far too late here for me to start trying to dissect that. Quote Link to comment Share on other sites More sharing options...
acidpunk Posted August 18, 2012 Author Share Posted August 18, 2012 thats what im sayin :/ my eyes hurt, im going to have to start go around 3 at it. Quote Link to comment Share on other sites More sharing options...
acidpunk Posted August 18, 2012 Author Share Posted August 18, 2012 actually just fixed it, it was a very simple mistake. i had the " . $totalattack['SUM(attack)'] set, it should have been $rowattack['SUM(attack)'] set, however, now the }else{ isn't working. last fix and we're ok. my eyes are bleeding hahaha. Quote Link to comment Share on other sites More sharing options...
acidpunk Posted August 18, 2012 Author Share Posted August 18, 2012 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. Quote Link to comment Share on other sites More sharing options...
acidpunk Posted August 18, 2012 Author Share Posted August 18, 2012 nevermind, its broken now out of nowhere, same code as i just poster, new and old. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 18, 2012 Share Posted August 18, 2012 That's one of the reasons we always recommend people to: [*]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. Quote Link to comment Share on other sites More sharing options...
acidpunk Posted August 18, 2012 Author Share Posted August 18, 2012 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 Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 18, 2012 Share Posted August 18, 2012 Yes, as previously stated: http://mysqljoin.com/ http://dev.mysql.com/doc/refman/5.0/en/join.html Quote Link to comment Share on other sites More sharing options...
acidpunk Posted August 18, 2012 Author Share Posted August 18, 2012 never used these before, just a little confused Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.