2017-02-11 5 views
-2

私はフォームを作成し、それ自身に情報を投稿するPHPページを作成しました。私はそれから私が返す/私はコードをここに文字列をエコーし​​たいメソッドを実行するオブジェクトを作成します。メソッドphpの値をエコーし​​ようとしています

<?php 
include_once 'checkLogin.php'; 
$action='';//this will store what the user wants to do 
if(isset($_GET['savings'])){$action='save';} 
if(isset($_GET['depreciation'])){$action='deprec';} 
$time=$_POST['time'];//get the values from the form below 
$interest=$_POST['interest']; 
$InitialSum=$_POST['InitialSum']; 
    //depreciation and savings can be done on the same page 
class Information{ 
    function __construct($time,$interest,$InitialSum){ 
    $this->time=$time; 
    $this->interest=$interest; 
    $this->InitialSum=$InitialSum; 
    } 
    function CalcSave(){ 
    $totalAmt=$InitialSum*($interest*100);//the total amount, this will count as one year 
    for ($x=0;$x<$time;$x++){//therefore this should only do 1 less than the total 
     $totalAmt=($interest*100)*$totalAmmt;//after another year 
    } 
    return'After '.$time.' years your savings will be worth '.$totalAmt.'.'; 
    } 
    function CalcDeprec(){ 
    $totalAmt=$InitialSum*$interest; 
    for ($x=0;$x<$time;$x++){//therefore this should only do 1 less than the total 
     $totalAmt=$interest*$totalAmmt;//after another year 
    } 
    return 'After'.$time.'years your asset will be worth '.$totalAmt.'.'; 
    } 
} 
?> 
<!DOCTYPE HTML> 
<html> 
<body> 
    <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" > 
Initial Amount: <input type="number" name="InitialSum"><br> 
Interest: <input type="number" name="interest"><br> 
How long: <input type='number' name='time'><br> 
<input type="submit"> 
</form> 
<p> 
    <?php 
    $user=new Information($time,$interest,$InitialSum);//this creates the user object 
    if ($action=='save'){ 
    $user->CalcSave();//this will run the method since user is the object 
    echo $result; 
    } 
    if ($action=='deprec'){ 
    $user->CalcDeprec(); 
    echo $result; 
    } 
    ?> 
</p> 
</body> 
</html> 

、それは別のページに行く場合、私は気にしませんが、理想的に私はそれがページ上にエコーされたいです。私はそれから何かエラーを取得していないと私は非常にOOPのPHPで経験はありません。 編集私は$result = $user->CalcSave(); echo $result;

echo $user->CalcSave(); 

を試みたが、それはページの最後に

?action=depreciation 

せずに同じページをリロードし、何もエコーされません。

+0

'するecho $ user-> CalcSave();'と 'するecho $ user-> CalcDeprecを();' – RiggsFolly

答えて

0

このお試しください:

$result = $user->CalcSave(); 
echo $result; 

と:

$result = $user->CalcDeprec(); 
echo $result; 
+3

はなぜOPべき"このコードを使用する"? **良い答え**は、何が行われたのか、そしてなぜそれがそのように行われたのかについての説明をいつも持っています。この質問を見つけて回答を読んでいるかもしれないので、OPのためだけでなく将来の訪問者のために 。 – RiggsFolly

+0

または単に 'echo $ user-> CalcSave();'と不要なスカラ変数の作成について忘れてください – RiggsFolly

+0

これはうまくいきませんでした。 – Awtthem

関連する問題