I have a HTML page that is sprinkled with PHP output giving me totals etc, an example such as:
<?PHP print $row[month_equiv_1]; ?>
The above would result in something like... £134.36
What I need to do is take the above number and multiply it by the output of another number generated in a similar fashion. Effectively what I'm trying to do is:
<?PHP print $row[month_equiv_1]; ?> * <?PHP print $row[week_equiv_1]; ?> = X
Then take X and subtract it from one last PHP print output.
I'm sure there's an easy way to do this but I'll be damned if I can see it. I could be wrong of course!!!!
Simple Maths Question I Hope
Moderator: General Moderators
Re: Simple Maths Question I Hope
How about this:
Then you can use that anywhere on the page. Of course you don't even have to set that as a variable, you can just use following wherever you want to:
To me, it's easier to use declare $result at the top of the page. It keeps the html below a little easier to read. Also, you don't have to go hunting for it later if you need to make changes. All your variables are set at the top.
Code: Select all
<?php
$result = row[month_equiv_1] * row[week_equiv_1];
?>Code: Select all
<?php row[month_equiv_1] * row[week_equiv_1];
?>Re: Simple Maths Question I Hope
I've tried out the above along with various other themes of the same ilk. I think it's the £ sign causing the trouble. It's being drawn in with the number answer, quite rightly so but it would appear to be the cause of my trouble. If I'm right in what I'm saying then this is not a simple maths type question at all.
Does that theory hold water or am I barking up the wrong tree? (woof woof!!!)
Does that theory hold water or am I barking up the wrong tree? (woof woof!!!)
Re: Simple Maths Question I Hope
Post some code example so we can see what you're doing.
Re: Simple Maths Question I Hope
As I said before, I have a HTML page that is sprinkled with PHP output that contains nothing more than print statements. I can't easily access the parent script that fires off the page that I can see/manipulate. Besides, the parent script is almost 2,000 lines long and I wouldn't know where to start looking for something of relevance to my issue.
But it would appear that the £ sign is the thing giving me the trouble so with that in mind I'm using:
$new_string = ereg_replace("[^0-9.]", "", $string);
to strip out everything expect the numbers and the decimal point and it seems to work. I'm making the assumption that this is the best way forward but if you know better don't hold back.
But it would appear that the £ sign is the thing giving me the trouble so with that in mind I'm using:
$new_string = ereg_replace("[^0-9.]", "", $string);
to strip out everything expect the numbers and the decimal point and it seems to work. I'm making the assumption that this is the best way forward but if you know better don't hold back.
-
lessthanphil
- Forum Newbie
- Posts: 1
- Joined: Wed Jun 02, 2010 1:31 pm
Re: Simple Maths Question I Hope
You're probably better off storing your currency values as a decimal without the string '£' saved with them....That way you can perform any mathematical calculations you need to without having issues such as the one you're already having.
You can append the '£' to them whenever you need to actually display their values.
I'm not sure where your data is coming from, so can't offer more advice than that.
You can append the '£' to them whenever you need to actually display their values.
I'm not sure where your data is coming from, so can't offer more advice than that.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Simple Maths Question I Hope
The regex replacement should be this:
ereg_replace() is depreciated.
Code: Select all
preg_replace("/[^0-9\.]/", "", $string);