21490 Posts in 5783 Topics by 2622 members
| Go to End | Next > | |
| Author | Topic: | 1132 Views |
-
Operations on variables

29 October 2010 at 10:05am
Hi All,
This seems like a really simple thing that I just can't get my head around. I have an object with a currency variable on it. I want to be able to divide that variable by 4 and create a new variable. The code I've tried is below. Help much appreciated.
The $Total variable comes from a many_many relationship managed by the dataobjectmanager.class Flatmate_Controller extends Page_Controller {
function eaPayment($Total) {
$num = 4;
$ea = $Total/$num;
return $ea;
}
} -
Re: Operations on variables

29 October 2010 at 12:31pm
How does this get called and what error do you get?
-
Re: Operations on variables

29 October 2010 at 12:36pm Last edited: 29 October 2010 12:37pm
Hi Blu0.
Are there any error messages?
Are you really,really sure that the variable $Total is of a type that can be divided? (z.B. an Integer...)Cheers,
Christianedit: yay, Hamish was faster...
-
Re: Operations on variables

29 October 2010 at 9:43pm
Hey thanks guys,
I'm trying to 'call' (not sure if i'm using the term correctly) it on the template and have tried $ea, putting it under a <% control eaPayment %> but no luck. I don't get any error message - just no value appears on the template.
The variable is created here: static $db = array ('Total' => 'Currency',); Which stores numbers to two decimal places (eg 123.45) which I assume can be divided - I tried changing it to an 'Int' variable with no luck
Bernard
-
Re: Operations on variables

30 October 2010 at 12:41am
Well, try this one:
function eaPayment() {
$num = 4;
$total = $this->Total;
$ea = $total/$num;
return $ea;
}And in your template:
<% if eaPayment %>
<p>$eaPayment</p>
<% end_if %>This might hopefully work.
Cheers,
Christian -
Re: Operations on variables

30 October 2010 at 1:52pm
By the way - the method should exist on the model (Flatmate), not the controller (Flatmate_Controller). Also, if you add the following to the model, you'll get automagic casting too:
static $casting = array(
'eaPayment' => 'Currency',
);That is, you can do any of the following, depending on the type of output you want:
$eaPayment
$eaPayment.Nice
$eaPayment.Int -
Re: Operations on variables

2 November 2010 at 7:39pm
Ah, you guys are great! Thanks! That worked with a couple of tweaks. The eaPayment function needed to go on my Bill object and be called through <% control Bills %>.
As a learning exercise would someone be able tell me what this part
actually does. My understanding is that it creates a variable for the function from the 'Total' variable on the current object (being Flatmate in this instance). What i'm not really sure is what the '->' is actually doing.$total = $this->Total;
Also the automagic casting doesn't seem to be going that well. $eaPayment.Nice just returns a blank. It would be awesome to get this going as i'm getting a few decimal places...
b
-
Re: Operations on variables

2 November 2010 at 7:45pm
Your understanding is correct, you might want to read up on OO basics:
| 1132 Views | ||
| Go to Top | Next > |



