Colleen78 Posted March 6, 2008 Share Posted March 6, 2008 I have a plugin for my vbulletin forum but the author appears to have abandoned supporting the plugin. I don't know when this issue started, but it was reported by my members today. It's a stock trader plugin, when you go to view a members stocks, any stocks they have with no change, ie; 0, are causing this error at the top of the page: Warning: Division by zero in /vbtrade.php on line 112 And that error is repeated for each stock with 0 change. You can view what I mean, here: http://www.webtalkforums.com/vbtrade.php?do=viewportfolio&userid=2898 (I don't think you need to be logged in to view it, if you do, let me know) This is line 112 in vbtrade.php $gainper = number_format((($gain/$mktval)*100),3); I'd like to try and fix this, any help/guidance is appreciated, thank you. Link to comment https://forums.phpfreaks.com/topic/94803-division-by-zero-problem/ Share on other sites More sharing options...
deadonarrival Posted March 6, 2008 Share Posted March 6, 2008 "vBulletin Message Invalid Action Specified" Couldn't you just put in (just before the line you showed) if($mktval <= "0") $mktval = 1; If the market value is zero or lower. (Replace the <= with == if numbers less than 0 are used) it will just display the gain (since "anything" divided by 1 is just "anything"). If you don't want it to just display the gain, you could just have it display en error message. if($mktval <= "0") echo "None"; or similar. Link to comment https://forums.phpfreaks.com/topic/94803-division-by-zero-problem/#findComment-485509 Share on other sites More sharing options...
Colleen78 Posted March 6, 2008 Author Share Posted March 6, 2008 Thanks for the response. I'm not a programmer at all, so did I do this wrong? if($mktval <= "0") echo "None"; { $gainper = number_format((($gain/$mktval)*100),3); } Cause it's still displaying the error, but also now with "none" in front of the error. Link to comment https://forums.phpfreaks.com/topic/94803-division-by-zero-problem/#findComment-485521 Share on other sites More sharing options...
deadonarrival Posted March 6, 2008 Share Posted March 6, 2008 A bit of both, I missed out a line and you used it wrong anyway This should work. <?php if($mktval <= "0") { echo "None"; } else { $gainper = number_format((($gain/$mktval)*100),3); } ?> Link to comment https://forums.phpfreaks.com/topic/94803-division-by-zero-problem/#findComment-485526 Share on other sites More sharing options...
Colleen78 Posted March 6, 2008 Author Share Posted March 6, 2008 Thanks again, one last thing, the error is gone, but "none" is being printed at the top of the page, instead of in the table td row it's meant to be in, which is here "<td align=right>$change</td>" I need to somehow get it to echo "None" there instead of outside the whole page, should I post more code or anything? Link to comment https://forums.phpfreaks.com/topic/94803-division-by-zero-problem/#findComment-485535 Share on other sites More sharing options...
deadonarrival Posted March 6, 2008 Share Posted March 6, 2008 No, that's just me not paying enough attention (sorry, a little tired) <?php if($mktval <= "0") { $gainper = "None"; } else { $gainper = number_format((($gain/$mktval)*100),3); } ?> Should do the trick now. *Has another read through* - yeah, should do. Link to comment https://forums.phpfreaks.com/topic/94803-division-by-zero-problem/#findComment-485544 Share on other sites More sharing options...
Colleen78 Posted March 6, 2008 Author Share Posted March 6, 2008 Thank you, you guys are like miracle workers here. I had to make a minor adjustment to get it to work. if($mktval <= "0") { $change = "None"; } else { $gainper = number_format((($gain/$mktval)*100),3); } I had to change the first instance of "$gainper" to the value called in the td row, "$change" and now "None" is displaying there correctly. Link to comment https://forums.phpfreaks.com/topic/94803-division-by-zero-problem/#findComment-485548 Share on other sites More sharing options...
deadonarrival Posted March 6, 2008 Share Posted March 6, 2008 Ah okay, is there a line somewhere that has $change = something($gainper); or $change = $gainper * $quantity; or something similar? I assume there must be if $gainper is the gain per unit of stock and the user has more than one stock. Anyway, glad it's working Link to comment https://forums.phpfreaks.com/topic/94803-division-by-zero-problem/#findComment-485549 Share on other sites More sharing options...
Colleen78 Posted March 6, 2008 Author Share Posted March 6, 2008 Well there's this bit, which looks like it was meant to handle the situation if there was no change: if ($shareinfo['change'] == 0) $change = "$changenum ($shareinfo[perchange])"; else if ($shareinfo['perchange'] > 0) $change = "<font color=green>$changenum ($shareinfo[perchange])</font>"; else if ($shareinfo['perchange'] < 0) $change = "<font color=red>$changenum ($shareinfo[perchange])</font>"; And there's this for the gainper part. $gaintxt = number_format($gain,3); $gainpertxt = number_format($gainper,3); if ($gain > 0) { $gaintxt = "<font color=green>$gaintxt</font>"; $gainpertxt = "<font color=green>$gainpertxt%</font>"; } elseif ($gain < 0) { $gaintxt = "<font color=red>$gaintxt</font>"; $gainpertxt = "<font color=red>$gainpertxt%</font>"; } else { $gainpertxt .= '%'; } Link to comment https://forums.phpfreaks.com/topic/94803-division-by-zero-problem/#findComment-485561 Share on other sites More sharing options...
deadonarrival Posted March 6, 2008 Share Posted March 6, 2008 Doesn't look like that's it, but either way what you did fixed it Link to comment https://forums.phpfreaks.com/topic/94803-division-by-zero-problem/#findComment-485573 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.