Page 1 of 1
variable inside a variable php
Posted: Fri Nov 23, 2012 8:04 am
by jonnyfortis
I have hit a brick wall on some calculation i am making.
I have some tables that show figures that are used and payment figures. however some of the figures are generated from other variables.
i have my equations set up but am getting negative values. basically i have some variables that i have to included (e.g $option_one->balance_due_before) but im not sure if these are stored correctly in my new variables i have set up
this is what i have so far
Code: Select all
$row_Recordset1['rental_price']
$utilityHost = 21.0;
$col3 = $utilityHost * $col5;
$col4 = $option_one->balance_due_before - $option_one->four_weeks_security - $option_one->fee;
$col5 = $col4 / $row_Recordset1['rental_price'];
$col6 = $row_Recordset1['rental_price'] * $col5;
variable $option_one->balance_due_before is echoed on the page in another area £<?php echo $option_one->balance_before ?>
this is also the same for $option_one->four_weeks_security and $option_one->fee
the results are echoed for the following variables below
Code: Select all
echo "£" . number_format($row_Recordset1['rental_price'] - $utilityHost);
echo "£" . $utilityHost;
echo "£" . $col3;
echo "£" . $col4;
echo "£" . $col5;
echo "£" . $col6;
i think it must be $col4 must be causing the incorrect results
can you help?
Re: variable inside a variable php
Posted: Fri Nov 23, 2012 10:23 am
by Christopher
Can you show the initial values and what is results are echoed from those values. It is a difficult to see what is going on. Plus you have the variable $row_Recordset1['rental_price'] on a line 1 by itself with no assignment or even semicolon. And then you use $col5 on line 3 but it is not yet assigned a value.
Re: variable inside a variable php
Posted: Mon Nov 26, 2012 4:46 am
by jonnyfortis
Plus you have the variable $row_Recordset1['rental_price']
sorry that should read $row_Recordset1['rental_price'];
that was a typo
And then you use $col5 on line 3 but it is not yet assigned a value.
this is needs to get the values from $col4 / $row_Recordset1['rental_price'];
is that what you mean?
Re: variable inside a variable php
Posted: Mon Nov 26, 2012 4:49 am
by jonnyfortis
for example the $option_one
is as follows
$option_one = new PaymentOptionOne($rent, $weeks);
require_once('../lib/PaymentOptionOne.php');
Re: variable inside a variable php
Posted: Mon Nov 26, 2012 3:19 pm
by Christopher
jonnyfortis wrote:sorry that should read $row_Recordset1['rental_price'];
But the a like with just $row_Recordset1['rental_price']; on it will do nothing.
jonnyfortis wrote:this is needs to get the values from $col4 / $row_Recordset1['rental_price'];
is that what you mean?
So what is the value of $col5 when this like executes?
$col3 = $utilityHost * $col5;
Re: variable inside a variable php
Posted: Tue Nov 27, 2012 5:34 am
by jonnyfortis
ok this is what it looks like with values
example with actual values
$row_Recordset1['rental_price'];m = 104
$option_one->balance_due_before = 1910.8
$option_one->four_weeks_security = 416
$option_one->fee = 184.8;
the results i am getting are
echo "£" . number_format($row_Recordset1['rental_price'] - $utilityHost); -= £83
echo "£" . $utilityHost; = £21
echo "£" . $col3; = £-121.31538461538
echo "£" . $col4; = £-600.8
echo "£" . $col5; = £-5.7769230769231
echo "£" . $col6; = £-600.8
by the looks of it the 1st two are correct but then they are not
Re: variable inside a variable php
Posted: Tue Nov 27, 2012 4:00 pm
by Christopher
I get different numbers than you, but the code you are posting is still messed up (e.g., "$row_Recordset1['rental_price'];m = 104"). Here is some test code I assembled from things you posted above. I get different numbers.
Code: Select all
// mock class
class PaymentOptionOne
{
public $balance_due_before = 0.0;
public $four_weeks_security = 0.0;
public $fee = 0.0;
public function __construct($rent, $weeks) {}
}
// initialize vars
$option_one = new PaymentOptionOne(0.0, 0);
$row_Recordset1 = array();
$row_Recordset1['rental_price'] = 104;
$option_one->balance_due_before = 1910.8;
$option_one->four_weeks_security = 416;
$option_one->fee = 184.8;
$utilityHost = 21.0;
$col5 = 1; // set this var so the next line will work
$col3 = $utilityHost * $col5;
$col4 = $option_one->balance_due_before - $option_one->four_weeks_security - $option_one->fee;
$col5 = $col4 / $row_Recordset1['rental_price'];
$col6 = $row_Recordset1['rental_price'] * $col5;
echo "<br/>row_Recordset1['rental_price'] - utilityHost=" . number_format($row_Recordset1['rental_price'] - $utilityHost);
echo "<br/>utilityHost=" . $utilityHost;
echo "<br/>col3=" . $col3;
echo "<br/>col4=" . $col4;
echo "<br/>col5=" . $col5;
echo "<br/>col6=" . $col6;
Outputs:[text]row_Recordset1['rental_price'] - utilityHost=83
utilityHost=21
col3=21
col4=1310
col5=12.596153846154
col6=1310[/text]
Re: variable inside a variable php
Posted: Tue Nov 27, 2012 5:08 pm
by jonnyfortis
I get different numbers than you, but the code you are posting is still messed up (e.g., "$row_Recordset1['rental_price'];m = 104")
sorry that was a typo there should not be an m before = 104
the other values are pulled in from an include ( all the variales with $option_one )
Code: Select all
public $weekly_amount;
public $four_weeks_security;
public $fifty_two_weeks;
public $fee;
public $fifty_two_inc_fee;
public $deposit;
public $fifteen_weeks;
public $balance_before;
public $second_payment;
public $third_payment;
public $fourth_payment;
public $total;
function __construct($weekly_amount, $weeks) {
$this->weekly_amount = $weekly_amount;
$this->four_weeks_security = $this->weekly_amount * 4;
$this->fifty_two_weeks = $this->weekly_amount * $weeks;
$this->fee = 184.8;
$this->fifty_two_inc_fee = ($this->four_weeks_security + $this->fifty_two_weeks + $this->fee);
$this->deposit = 250;
$this->fifteen_weeks = (($this->weekly_amount * 15) + $this->fee + $this->four_weeks_security);
$this->balance_before = ($this->fifteen_weeks - $this->deposit);
$this->second_payment = ($this->weekly_amount * 14);
$this->third_payment = ($this->weekly_amount * 14);
$this->fourth_payment = 0;
if($weeks == 52)
$this->fourth_payment = ($this->weekly_amount * 9);
$this->total = ($this->fifteen_weeks + $this->second_payment + $this->third_payment + $this->fourth_payment);
}
}
but what you have produced is looking more like the correct results.. does the above code help?
Re: variable inside a variable php
Posted: Tue Nov 27, 2012 10:01 pm
by Christopher
The question is -- what part is not working? There are a lot of calculations there. Which ones are wrong? You need to step through with a debugger or put echo after every calculations to see where it is going wrong.
Re: variable inside a variable php
Posted: Wed Nov 28, 2012 7:26 am
by jonnyfortis
what part is not working
this is the code that is causing the problems. i am not getting errors but am getting incorrect results
$col4 = $option_one->balance_due_before - ($option_one->four_weeks_security - $option_one->fee);
i have added parentheses around part of the equation so this is processed first
$utilityHost = 21.0;
$col4 = $option_one->balance_due_before - ($option_one->four_weeks_security - $option_one->fee);
$col5 = $col4 / $row_Recordset1['rental_price'];
$col3 = $utilityHost * $col5;
$col6 = $row_Recordset1['rental_price'] * $col5;
$row_Recordset1['rental_price']; = 104
$option_one->balance_due_before = 1910.8
$option_one->four_weeks_security = 416
$option_one->fee = 184.8;
the results i am getting are
echo "£" . number_format($row_Recordset1['rental_price'] - $utilityHost); -= £83
echo "£" . $utilityHost; = £21
echo "£" . $col3; = £-46.684615384615
echo "£" . $col4; = £-231.2
echo "£" . $col5; = £-2.2230769230769
echo "£" . $col6; = £-231.2
not of the results should be negative