0

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;?>
1
  • you haven't assign $sum=$obj->setFraction(); to $sum Commented Oct 11, 2012 at 11:37

4 Answers 4

1

Store the returned data to some variable,and then iterate.

//

<?php if(isset($num,$den)):
  $obj = new SomeClass();
  $obj->setVars($num,$den);
  $array = $obj->setFraction();?>


  <ul>
    <?php foreach($array as $val):?>
      <li><?php echo $val['num']?></li>
      <li><?php echo $val['den']?></li>
    <?php endforeach;?>
  </ul>

<?php endif;?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This answer was the closest one. I used echo $array['num']; instead of with a foreach loop and it works!
1

you haven't assign $sum=$obj->setFraction(); to $sum

Comments

1

auYou're not assigning the return values of $obj->setFraction() to a variable. You need to do:

$sum=$obj->setFraction();

and all should be OK.

Just becuase you're returning a variable called $sum doesn't mean that that variable name is used once it's back in the main body of your script.

I have just noticed, as you rightly state in your acceptance, that you don't need the foreach block. Just use $sum['num'] and $sum['den']. Sorry about that - I should have noticed earlier

Comments

-1

You should use $obj->sum instead of $sum.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.