2017-02-11 3 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> 

Я не возражаю, если он идет на другую страницу, но в идеале я хотел бы, чтобы это было отражение на странице. Я не получаю никаких ошибок от этого, и я не очень опытен с ООП в php. Edit Я попытался $result = $user->CalcSave(); echo $result; и

echo $user->CalcSave(); 

но перегружает ту же страницу без

?action=depreciation 

в конце страницы и ничего не перекликается.

+0

'эхо $ user-> CalcSave();' и 'эхо $ user-> CalcDeprec();' – RiggsFolly

ответ

0

Попробуйте это:

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

и:

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

зачем ОП «использовать этот код»? ** Хороший ответ ** всегда будет объяснять, что было сделано и почему это было сделано таким образом, не только для OP, но и для будущих посетителей SO, которые могут найти этот вопрос и прочитать ваш ответ. – RiggsFolly

+0

Или просто 'echo $ user-> CalcSave();' и забудьте о создании ненужной скалярной переменной – RiggsFolly

+0

Это не сработало. – Awtthem

Смежные вопросы