Rayof86 Posted November 9, 2008 Share Posted November 9, 2008 Not sure if I am going to be able to explain this as it kind of pertains to a game also. I have a script that pulls info from a website (WARDB.com) to display a team roster with tooltip on my website. Problem is the maximum rank in the game is 40 so your XP is at maximum which throws the script off and gives Division by zero errors, if anyone has any clue what im talking about could you please look at this? I dont know a ton about PHP but I am sure there has to be some kind of 'IF' statement you can add for the max rank characters on the roster. if 'level' = "40" show XP maxed else run the script normally Errors "Warning: Division by zero in profilegrab.php on line 29" "Warning: Division by zero in classherald.php on line 91" profilegrab.php (I have commented out the effected line just so its easier to find) <html> <head> <title>Profile</title> <script type="text/javascript" src="http://www.wardb.com/js/extooltips.js"></script> </head> <?php include('JSON.php'); include('classHerald.php'); include('config.inc'); $grab = new WARHerald(); $grab->grabhtml( $config['url'], $config['start_tag'], $config['end_tag'] ); echo $grab->error; $string = htmlspecialchars($grab->strip($grab->html[1][0], $config['show_tags'], $config['start_tag'], $config['end_tag'] )); $sub1 = substr($string, 1); $json = substr($sub1,0,-2); $value = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); $out = $value->decode($json); $gender = array(1 => "male", 2 => "female"); echo "Name: " . $out['name'] . "<br>"; echo "Level: " . $out['level'] . "<br>"; echo "Rank: " . $out['renownTitle'] . " (" . $out['renownRank'] . ")<br>"; echo "Race: " . $grab->racetostring($out['race']) . "<br>"; echo "Class: " . $grab->careertostring($out['cclass']) . "<br>"; echo "Gender: " . $gender[$out['gender']] . "<br>"; ##PROBLEM LINE## echo $out['xpEarned'] . "/" . $out['xpNeeded'] . "(" . round(100/$out['xpNeeded']*$out['xpEarned'],0) . "%)" . $grab->xpbalken($out['xpEarned'],$out['xpNeeded']) . "<br>"; echo $out['renownEarned'] . "/" . $out['renownNeeded'] . "(" . round(100/$out['renownNeeded']*$out['renownEarned'],0) . "%)" . $grab->renownbalken($out['renownEarned'],$out['renownNeeded']) . "<br>"; ?> classherald.php (I have commented out the effected line just so its easier to find) <?php /* Warhammer Online Herald Class! Datasource WARDB.com Version 1.0 13.10.2008 - www.legit-gaming.com */ class WARHerald { var $error = ''; var $html = ''; function grabhtml( $url, $start, $end ) { $file = file_get_contents( $url ); if( $file ) { if( preg_match_all( "#$start(.*?)$end#s", $file, $match ) ) { $this->html = $match; } else { $this->error = "Tags cannot be found."; } } else { $this->error = "Site cannot be found!"; } } function strip( $html, $show, $start, $end ) { if( !$show ) { $html = str_replace( $start, "", $html ); $html = str_replace( $end, "", $html ); return $html; } else { return $html; } } //Careerid to String function careertostring($id) { $career_array = array(20 => "Archmage", 5 => "Black Orc", 11 => "Bright Wizard", 13 => "Chosen", 23 => "Disciple of Khaine", 4 => "Engineer", 1 => "Ironbreaker", 16 => "Magus", 14 => "Marauder", 3 => "Rune Priest", 18 => "Shadow Warrior", 7 => "Shaman", 24 => "Sorceress", 8 => "Squig Herder", 17 => "Swordmaster", 12 => "Warrior Priest", 19 => "White Lion", 22 => "Witch Elf", 9 => "Witch Hunter", 15 => "Zealot"); return $career_array[$id]; } //Raceid to Name function racetostring($id) { $race_array = array(7 => "Chaos", 5 => "Dark Elf", 1 => "Dwarf", 6 => "Empire", 3 => "Goblin", 4 => "High Elf", 2 => "Orc"); return $race_array[$id]; } //Build the XP Bar in the Charinfo Tooltip function xpbalken($zaehler,$nenner){ ##PROBLEM LINE## $pro=round(($zaehler/$nenner*100),0); echo echo '<div style="border:1px solid #000000;padding:1px;background:#FFFFFF;width:100px;height:10px"> <div align="left" style="width:'.$pro.'%;height:100%;color:#ffffff;background:#004400;text?align:center;valign:top;font-size: 6pt;"></div> </div>'; } //Build the Renown Bar in the Charinfo Tooltip function renownbalken($zaehler,$nenner){ $pro=round(($zaehler/$nenner*100),0); echo '<div style="border:1px solid #000000;padding:1px;background:#FFFFFF;width:100px;height:10px"> <div align="left" style="width:'.$pro.'%;height:100%;color:#ffffff;background:#440044;text?align:center;valign:top;font-size: 6pt;"></div> </div>'; } function createTimeString($timeStamp, $format="long", $dateFormat="n/j/Y") { // Take the current time and create the difference $timeDifference = time() - $timeStamp; // Check the length of time passed since seconds switch($timeDifference) { // Date is less than an hour old case $timeDifference <= 3600: $minutes = floor($timeDifference / 60); $seconds = $timeDifference - ($minutes * 60); // Choose correct pluralizations if($minutes == 1) { $minuteString = "minute"; } else { $minuteString = "minutes"; } if($seconds == 1) { $secondString = "second"; } else { $secondString = "seconds"; } switch($format) { case "short": return $minutes . "m " . $seconds . "s ago"; break; default: return $minutes . " " . $minuteString . " " . $seconds . " " . $secondString . " ago"; break; } break; // Date is less than a day old case $timeDifference <= 86400: $hours = floor($timeDifference / 60 / 60); $minutes = floor(($timeDifference - ($hours * 60 * 60)) / 60); // Choose correct pluralizations if($hours == 1) { $hourString = "hour"; } else { $hourString = "hours"; } if($minutes == 1) { $minuteString = "minute"; } else { $minuteString = "minutes"; } switch($format) { case "short": return $hours . "h " . $minutes . "m ago"; break; default: return $hours . " " . $hourString . " " . $minutes . " " . $minuteString . " ago"; break; } break; // Date is less than a week old. case $timeDifference <= 604800: $days = floor($timeDifference / 86400); $hours = floor(($timeDifference - ($days * 86400)) / 60 / 60); // Choose correct pluralizations if($days == 1) { $dayString = "day"; } else { $dayString = "days"; } if($hours == 1) { $hourString = "hour"; } else { $hourString = "hours"; } switch($format) { case "short": return $days . "d " . $hours . "h ago"; break; default: return $days . " " . $dayString . " " . $hours . " " . $hourString . " ago"; break; } break; // Older than one week. Just return the absolute date. default: return date($dateFormat, $timeStamp); break; } } function careertoicon($id) { $career_array = array(20 => "http://www.wardb.com/icons/s/20180.png", 5 => "http://www.wardb.com/icons/s/20182.png", 11 => "http://www.wardb.com/icons/s/20183.png", 13 => "http://www.wardb.com/icons/s/20185.png", 23 => "http://www.wardb.com/icons/s/20186.png", 4 => "http://www.wardb.com/icons/s/20187.png", 1 => "http://www.wardb.com/icons/s/20189.png", 16 => "http://www.wardb.com/icons/s/20191.png", 14 => "http://www.wardb.com/icons/s/20192.png", 3 => "http://www.wardb.com/icons/s/20193.png", 18 => "http://www.wardb.com/icons/s/20194.png", 7 => "http://www.wardb.com/icons/s/20195.png", 24 => "http://www.wardb.com/icons/s/20196.png", 8 => "http://www.wardb.com/icons/s/20197.png", 17 => "http://www.wardb.com/icons/s/20198.png", 12 => "http://www.wardb.com/icons/s/20199.png", 19 => "http://www.wardb.com/icons/s/20200.png", 22 => "http://www.wardb.com/icons/s/20201.png", 9 => "http://www.wardb.com/icons/s/20202.png", 15 => "http://www.wardb.com/icons/s/20203.png"); return $career_array[$id]; } } ?> I know this looks like a lot at first glance, but I was thinking these division by zero errors usually have a simple fix. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/132020-division-by-zero-problem/ Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 I know this looks like a lot at first glance, but I was thinking these division by zero errors usually have a simple fix. Thanks guys! They do: check if divisor is not zero before doing division. Quote Link to comment https://forums.phpfreaks.com/topic/132020-division-by-zero-problem/#findComment-685997 Share on other sites More sharing options...
Rayof86 Posted November 10, 2008 Author Share Posted November 10, 2008 Anyway someone could edit the code to correctly work? I understand what you are saying but i don't have the know how to translate that to code Quote Link to comment https://forums.phpfreaks.com/topic/132020-division-by-zero-problem/#findComment-686462 Share on other sites More sharing options...
JasonLewis Posted November 10, 2008 Share Posted November 10, 2008 Here is some pseudo code. If divisor > 0 Perform division Endif Where ever you are performing a division where the number you may be dividing by is 0, ensure that it is NOT 0 before doing the division. Quote Link to comment https://forums.phpfreaks.com/topic/132020-division-by-zero-problem/#findComment-686480 Share on other sites More sharing options...
Rayof86 Posted November 10, 2008 Author Share Posted November 10, 2008 echo $out['xpEarned'] . "/" . $out['xpNeeded'] . "(" . round(100/$out['xpNeeded']*$out['xpEarned'],0) . "%)" . $grab->xpbalken($out['xpEarned'],$out['xpNeeded']) . "<br>"; $pro=round(($zaehler/$nenner*100),0); I dont see where anything is being divided? I really have no clue what is even going on in the script. And the code you are giving would seem to omit any results if the character is lvl40, i need it to just display a different message instead of trying to perform the divison Quote Link to comment https://forums.phpfreaks.com/topic/132020-division-by-zero-problem/#findComment-687264 Share on other sites More sharing options...
Mchl Posted November 11, 2008 Share Posted November 11, 2008 100/$out['xpNeeded']*$out['xpEarned $zaehler/$nenner / is a division operator in PHP Quote Link to comment https://forums.phpfreaks.com/topic/132020-division-by-zero-problem/#findComment-687293 Share on other sites More sharing options...
Rayof86 Posted November 11, 2008 Author Share Posted November 11, 2008 ah ok, I was thinking that was just the displayed / on the page but i guess that would be the "/" which i see on the page, ok ill try to figure this out, thanks Quote Link to comment https://forums.phpfreaks.com/topic/132020-division-by-zero-problem/#findComment-687303 Share on other sites More sharing options...
Mchl Posted November 11, 2008 Share Posted November 11, 2008 ah ok, I was thinking that was just the displayed / on the page but i guess that would be the "/" which i see on the page You're right about that and if you want message displayed when the divisor is 0 you just need if-else statement if($nenner != 0) { // != means "not equal" //perform division } else { //display message } Quote Link to comment https://forums.phpfreaks.com/topic/132020-division-by-zero-problem/#findComment-687307 Share on other sites More sharing options...
Rayof86 Posted November 11, 2008 Author Share Posted November 11, 2008 This is how I did it, probably not best way to do it but I know NOTHING about PHP, but I got this to work. if ($out['level'] > 39) echo "Experience Maxed!"; if ($out['level'] <= 39) echo $out['xpEarned'] . "/" . $out['xpNeeded'] . "(" . round(100/$out['xpNeeded']*$out['xpEarned'],0) . "%)" . $grab->xpbalken($out['xpEarned'],$out['xpNeeded']) . "<br>"; And thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/132020-division-by-zero-problem/#findComment-687337 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.