Forum Moderators: coopster

Message Too Old, No Replies

Remove quotes

NOT a coder!

         

akmac

6:02 pm on Oct 15, 2015 (gmt 0)

10+ Year Member



How to edit this line of code:

echo 'ecomm_totalvalue: \'' . $this->page_totalvalue . '\'' . "\n";

To get this (remove quotes):
ecomm_totalvalue: 1,000.00


Instead of this:
ecomm_totalvalue: '1,000.00'

LifeinAsia

6:12 pm on Oct 15, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Is this what you mean?
echo 'ecomm_totalvalue: ' . $this->page_totalvalue . '\n';

akmac

6:16 pm on Oct 15, 2015 (gmt 0)

10+ Year Member



That gave me this result:

ecomm_totalvalue: 1,715.00\n};

I'm sure I'm neglecting to provide some necessary additional information...

akmac

6:26 pm on Oct 15, 2015 (gmt 0)

10+ Year Member



Ah-this seems to work:

echo 'ecomm_totalvalue: ' . $this->page_totalvalue . "\n";

Thanks for the help!

lucy24

9:03 pm on Oct 15, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah-this seems to work:

Ah, the infamous Single Quote Issue. There are a couple of spots in php where double vs. single quotes leads to entirely different output; this is one of them.

:: idly noting that it looks as if the only reason the \n is there at all is to produce prettier html, and here I was thinking I'm the only person in the world who does that :) ::

akmac

10:06 pm on Oct 15, 2015 (gmt 0)

10+ Year Member



Yes, it's almost as though php is another language. ;-)

Oddly enough, once the quotes were removed (per instructions from google) another invisible issue appeared, and they needed me to change the line again to:

echo 'ecomm_totalvalue: parseFloat(\'' . $this->page_totalvalue . '\')' . "\n";

We'll see what exciting issues this new edit can create...

whitespace

11:32 am on Oct 16, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



the only reason the \n is there at all is to produce prettier html


Or a prettier JavaScript object literal by the looks in this case. Although I think I'd still favour the "prettier" originating source code to be honest.

echo 'ecomm_totalvalue: parseFloat(\'' . $this->page_totalvalue . '\')' . "\n";


If I think I know what you are trying to do then this is not going to work. If $this->page_totalvalue returns strings like "1,750.00" (as mentioned above) then ecomm_totalvalue is going to end up with the value 1 (one). That comma is a problem. (But you're not going to realise that until further down the line... when the code that receives this object outputs the wrong values - if indeed you realise the values are wrong! This could be difficult to debug. You are better off doing the conversion in PHP.)

This PHP object should really be providing direct access to the numeric value of "page_totalvalue"? In order to get the formatted string (above), it is already processing the numeric value (possibly according to a locale?). It is not good to have to convert a formatted numeric string back into a floating point number. Because of the number of different ways a float could be formatted (in different locales) this is prone to error.

If you have no option, and thousand separators are always commas then try something like:


echo 'ecomm_totalvalue: ' . str_replace(',','',$this->page_totalvalue) . "\n";


The above code simply removes the thousands separator, so JavaScript will see it as a numeric value (and not an error as you probably had originally).

akmac

5:33 pm on Oct 16, 2015 (gmt 0)

10+ Year Member



This ended up being the winning combination:

echo 'ecomm_totalvalue: parseFloat(\'' . $this->page_totalvalue . '\'.replace(/[^0-9.]/g,\'\'))' . "\n";

It's all Geek to me.