Page 1 of 1

Code not working once applying '+' '/' '*' or '-'

Posted: Sat Aug 20, 2011 10:22 am
by Nick11380
I have gold data being reported here goldprices.org.uk (scroll down near the bottom). Recently it broke for no apparent reason. I checked the scraper and everything seems to be OK. The issue is that the gold price in (troy) ounces is being scraped fine - however to work out the price in grams you must multiply by 0.0321 (grams in a troy ounce). The code looks like this:

Code: Select all

$ounce_price = null;
    $grams_price = null;
    if(count($nodes) == 1 && $nodes[0][1]) {
        $ounce_price = $nodes[0][1];
        $grams_price = $ounce_price * 0.0321;
However $ounce_price * 0.0321 breaks the code and returns '0.0321'.

I then tried the code:

Code: Select all

$ounce_price = null;
    $grams_price = null;
    if(count($nodes) == 1 && $nodes[0][1]) {
        $ounce_price = $nodes[0][1];
        $grams_price = $ounce_price + 1;
And the code returned the value '2'. So it appears that when $ounce_price is being multiplied/subtracted etc it reverts to a value of '1'. However if I do $grams_price = $ounce_price the value is the correct ounce price.

I'm so confused as to why when adding an equation to $ounce_price the value reverts to '1' as opposed to equalling the correct number. Any help here would be HUGELY appreciated - I've been stuck for several days and only just decided to ask online :s

Nick

Re: Code not working once applying '+' '/' '*' or '-'

Posted: Sat Aug 20, 2011 11:03 am
by twinedev
Have you echoed out $ounce_price before the last line to make sure it is a value other than 1?

-Greg

Re: Code not working once applying '+' '/' '*' or '-'

Posted: Sat Aug 20, 2011 11:05 am
by Nick11380
Hey Greg, thanks for the reply. I'm not entirely sure how to do that - so no I haven't! Would you be able to point me in the right direction?

Re: Code not working once applying '+' '/' '*' or '-'

Posted: Sat Aug 20, 2011 11:14 am
by twinedev
depending on where the code is at, you may be able to do this:

Code: Select all

echo '[OUNCE PRICE=',$ounce_price,"]\n";
I prefer to wrap it with the square backets, so that if it gets displayed in the middle of other information, you can easily tell where the value of the variable ends.

Sometimes though where the code is executing, you can't just echo it out and see it. In a case like that, what I'll do is have the server mail me the results:

Code: Select all

mail ('my@email.com',__FILE__,'['.__LINE__.'|OUNCE PRICE='.$ounce_price.']');
I use the filename as the subject line, and also put the line number into the message. Why? It's been more than once I've put in debug code and gone down a different path and forgot about it, only to get e-mailed 2 days later when that code was executed. This way, I know what file and what line number to find the debug code.

-Greg

PS. if this is code that would be getting hit often, be carefull, cause you will receive an e-mail for each call. You may want to add the following to limit it to be your IP that triggers it. Go to http://www.whatsmyip.org/ and then put that IP into the if statement:

Code: Select all

if ($_SERVER['REMOTE_ADDR']=='0.0.0.0') { mail ('my@email.com',__FILE__,'['.__LINE__.'|OUNCE PRICE='.$ounce_price.']'); }

Re: Code not working once applying '+' '/' '*' or '-'

Posted: Sat Aug 20, 2011 11:34 am
by Nick11380
I managed to add the initial code and it's returning

[OUNCE PRICE=1,125.4973]

So clearly the price is actually that and not 1. So lost - willing to pay a small amount for someone to run through this and find the problem.

Re: Code not working once applying '+' '/' '*' or '-'

Posted: Sat Aug 20, 2011 12:00 pm
by twinedev
There is the problem, that is not a number, it is a string. When PHP converts a string to a number, it will go until either it hits a non number value (0-9 or a period) or a second period. (oh, and it can also start with a - )

So when it converts it to a number (when you go to do something mathematical on it), it reads 1, then reads the comma. Comma isn't something it sees as a number character, so it stops. Therefore when it converts, it comes to be 1.

You need to do something like:

Code: Select all

$ounce_price = preg_replace('/[^-0-9.]/','',$nodes[0][1]);
to strip out anything other that -,0-9, or .

If the price used to always be under 1,000, that would probably be why it seemed to always work before but stopped now.

-Greg

Re: Code not working once applying '+' '/' '*' or '-'

Posted: Sat Aug 20, 2011 12:21 pm
by Nick11380
Greg, you are a hero! That has solved the problem and fixed everything. And that's exactly what it was, gold price shot up recently; previously it was hanging around the 900-950 mark. I can't tell you how much I appreciate your help! That's been bugging me for several days.

Re: Code not working once applying '+' '/' '*' or '-'

Posted: Sat Aug 20, 2011 12:23 pm
by twinedev
Glad to be able to help.

-Greg