Jump to content

Division by zero problem


Colleen78

Recommended Posts

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
Share on other sites

"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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.