2011-05-26 56 views
0

作为一个新手,我的问题是,是这样写的PHP代码是很好的做法,混合HTML和PHP还是有这样做的更好的办法结合PHP和HTML

<?php 
    if (isset($_POST['submit'])) 
    { 
     $principal_balance = $_POST['principal_amount']; 
     $interest_rate = $_POST['interest_rate']; 
     $repayment_amount = $_POST['repayment_amount']; 

     echo "<html>"; 
     echo "<head>"; 
     echo "<title> Loans </title>"; 
     echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />"; 
     echo" <link rel=\"stylesheet\" type=\"text/css\" href=\"styles/document.css\" />"; 
     echo "<body>"; 


     echo "<table>"; 
     echo "<th> Principal Balance </th> <th> Interest Amount </th> <th> Principal Balance Amount Recovered </th> <th> Principal Balance </th> <th> Outstanding Balance </th>"; 
     while ($principal_balance > 0) 
     { 

      if ($principal_balance < $repayment_amount) 
      { 
       exit; 
      } 
      else 
      { 
       $interest_amount = $interest_rate * $principal_balance * 1/12; 

       $principal_amount_recovered = $repayment_amount - $interest_amount; 

       $outstanding_balance = $principal_balance - $principal_amount_recovered; 

       round ($interest_amount, 2); 
       round ($principal_amount_recovered, 2); 
       round ($outstanding_balance, 2); 
       //echo $principal_balance . "," . $interest_amount . "," . $principal_amount_recovered . "," . $outstanding_balance . "<br />"; 
       echo "<tr> <td>" . round ($principal_balance, 2) . "</td> <td>" . round ($interest_amount, 2) . "</td> <td>" . round ($principal_amount_recovered, 2). "</td> <td>" . round ($outstanding_balance, 2) . "</td> </td>"; 

       $principal_balance = $outstanding_balance;   
      } 
     } 
     echo "</table>"; 
     echo "</body>"; 
     echo "</html>"; 
    } 
?> 
+0

对于像这样的独立脚本,没有真正的问题。当您开始制作更大更复杂的脚本时,您需要考虑某种组织,例如[MVC](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%% 93控制器) – Ross 2011-05-26 11:49:14

回答

1

好问题。

我总是推荐那些刚刚开始学习PHP的人,他们关心将标记和PHP脚本混合在一起并不是非常重要,因为在开始时,您需要学习的是熟悉语法并查看PHP代码的工作原理。

但是,当您进一步改进时,将标记(HTML)与业务层(PHP脚本)分开是一种很好的做法,以便脚本看起来更干净,更好,更易于维护。

关于你上面的代码,我建议你看一下这个话题,我的回答是:How to connect controller to view in PHP OOP?

0

它要么是,或者打开和关闭php标签,这在我看来很丑陋,难以阅读。我个人更喜欢在PHP中回应HTML,就像你一样。

另一个选项在某些情况下更清晰,它会将输出保存到变量中,并继续添加新字符串,然后在代码结尾处回显该字符串。 Ej:

$output = ""; 
$world = "world"; 
$output.= "Hello"; 
if ($world) { 
$output.= ' '.$world; 
} 
echo $output; //would print "hello world 

总之,使用什么更清洁,更容易阅读在每个场合。如果你做错了,你的代码会看起来很丑并且很难维护,这就是很多人讨厌php的原因。

0

这是一个合理的开始。随着项目的发展,您可能需要考虑从内容中分离标记(例如,首先生成内容,然后将内容传递给以HTML标记它的显示例程)。

0

不,这样开发并不是很好的做法。最终,如果项目变得更大,代码会变得更加混乱和难以维护。

您最好使用模板引擎。

我跟Smarty有过很好的经历。请看看:

http://www.smarty.net/

首先,你必须创建一些文件夹,但在那之后,它很容易。模板语言很容易理解。我甚至用于非常小的项目。

+0

请不要使用聪明,它是缓慢和笨拙的 – 2011-05-26 11:57:57

+0

这是你的意见还是你有实际的数字? – edwin 2011-05-26 12:55:31

+0

这是我的观点根据我的经验。我忘了我另一件夹克上的数字 – 2011-05-26 13:04:40

0

尽量保持与html分离的逻辑,最好将逻辑保持在文档的开头。例如,更好地使用像Smarty这样的模板系统。

0
<?php 
    if (isset($_POST['submit'])) 
    { 
     $principal_balance = $_POST['principal_amount']; 
     $interest_rate = $_POST['interest_rate']; 
     $repayment_amount = $_POST['repayment_amount']; 
?> 

<html> 
<head> 
<title> Loans </title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<link rel="stylesheet" type="text/css" href="styles/document.css" /> 
<body> 


<table> 
     <th> Principal Balance </th> <th> Interest Amount </th> <th> Principal Balance Amount Recovered </th> <th> Principal Balance </th> <th> Outstanding Balance </th> 
<?php 
     while ($principal_balance > 0) 
     { 

      if ($principal_balance < $repayment_amount) 
      { 
       exit; 
      } 
      else 
      { 
       $interest_amount = $interest_rate * $principal_balance * 1/12; 

       $principal_amount_recovered = $repayment_amount - $interest_amount; 

       $outstanding_balance = $principal_balance - $principal_amount_recovered; 

       round ($interest_amount, 2); 
       round ($principal_amount_recovered, 2); 
       round ($outstanding_balance, 2); 
       //echo $principal_balance . "," . $interest_amount . "," . $principal_amount_recovered . "," . $outstanding_balance . "<br />"; 
       echo "<tr> <td>" . round ($principal_balance, 2) . "</td> <td>" . round ($interest_amount, 2) . "</td> <td>" . round ($principal_amount_recovered, 2). "</td> <td>" . round ($outstanding_balance, 2) . "</td> </td>"; 

       $principal_balance = $outstanding_balance;   
      } 
     } 
?> 

</table> 
</body> 
</html> 
<?php 
    } 
?> 
0

两个建议:

1 - 避免嵌套代码。尝试写

if("bad condition") exit; 

而不是

if("good condition") 
    { 
    <do the big job for many line of code> 
    .... 
    } 

这是一个良好的编码习惯

2 - 大多数时候,你可以写主文档结构,避免纯HTML。而且你只对包含动态内容的内部标签使用echo。

<?php 
    if (! isset($_POST['submit'])) 
    exit; 
?>   
<html> 
<head> 
<title> Loans </title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
    <link rel="stylesheet" type="text/css" href="styles/document.css" />" 
<body> 
<table> 
    <th> Principal Balance </th> 
    <th> Interest Amount </th> <th> Principal Balance Amount Recovered </th> 
    <th> Principal Balance </th> <th> Outstanding Balance </th> 

<?php 
$principal_balance = $_POST['principal_amount']; 
$interest_rate = $_POST['interest_rate']; 
$repayment_amount = $_POST['repayment_amount']; 

while ($principal_balance > 0) 
{ 

    if ($principal_balance < $repayment_amount) 
    { 
    exit; 
    } 
    else 
    { 
    $interest_amount = $interest_rate * $principal_balance * 1/12; 
     $principal_amount_recovered = $repayment_amount - $interest_amount; 

    $outstanding_balance = $principal_balance - $principal_amount_recovered; 

    round ($interest_amount, 2); 
    round ($principal_amount_recovered, 2); 
    round ($outstanding_balance, 2); 
    //echo $principal_balance . "," . $interest_amount . "," . $principal_amount_recovered . "," . $outstanding_balance . "<br />"; 
    echo "<tr> <td>" . round ($principal_balance, 2) . "</td> <td>" . round ($interest_amount, 2) . "</td> <td>" . round ($principal_amount_recovered, 2). "</td> <td>" . round ($outstanding_balance, 2) . "</td> </td>"; 

    $principal_balance = $outstanding_balance;   
} 
    } 
} 
?> 
</table> 
</body> 
</html> 

下一步可能是从页面中提取php代码并编写一个函数。