So here's what I want to do, I have a class and within SomeClass, there's a function consists of 2 variables, each has a numerical value. I want to return an array of the two variables which have dependencies in post data outside of SomeClass from a form that posts to itself.
<?php
class SomeClass{
private $someVar1;
private $someVar2;
public function setVars($var1,$var2) {
$this->someVar1 = $var1;
$this->someVar2 = $var2;
}
function __construct() {
}
function setFraction() {
$sum['num'] = 140*pow($this->someVar1,0.75);
$sum['den'] = $sum['num']/$this->someVar2;
return $sum;
}
}
if(isset($_POST['num'])){$num = preg_replace('/\D/', '', $_POST['num']);}
elseif(isset($_POST['den'])){$den = preg_replace('/\D/', '', $_POST['den']);}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Numerator:<input type="text" value="<?php if(isset($num)){echo $num;}?>" name="num" /><br />
Denominator:<input type="text" value="<?php if(isset($den)){echo $den;}?>" name="den" /><br />
<input type="submit" value="Go" name="Go">
</form>
<?php if(isset($num,$den)):
$obj = new SomeClass();
$obj->setVars($num,$den);
$sum=$obj->setFraction();?>
<pre>
<?php print_r($obj->setFraction());?>
</pre>
<ul>
<?php foreach($sum as $val):?>
<li><?php echo $val['num']?></li>
<li><?php echo $val['den']?></li>
<?php endforeach;?>
</ul>
<?php endif;?>
When I run the script, post data for $num and $den is being sent and returned with setFraction(), however when I try to iterate through the array in a foreach loop, I don't get any return data. I'm sure its something simple, I've tried it with a foreach loop and without with same results, any help would be excellent, thx.
EDIT:
<?php if(isset($num,$den)):
$obj = new SomeClass();
$obj->setVars($num,$den);
$array = $obj->setFraction();?>
<ul>
<li><?php echo $array['num']?></li>
<li><?php echo $array['den']?></li>
</ul>
<?php endif;?>
$sum=$obj->setFraction();to$sum