2017-05-28 58 views
-1

我正在开发一个网站,150个文件,大量的代码;至少对我来说,是一个先行者。在PHP网站中包含代码的正确方法

有许多地方我会重复输出HTML的PHP​​回声,每个PHP都有很多PHP变量。我正在考虑不重复这部分内容的最佳方式。

鉴于这种HTML:

<main> 
     <section> 
      <?php 
      $query = "SELECT a, b, c, d, e FROM table1"; 
      $result = mysqli_query($connection,$query); 
      while ($row = mysqli_fetch_assoc($result)) { 
       $table1_a = $row['a']; 
       $table1_b = $row['b']; 
       $table1_c = $row['c']; 
       $table1_d = $row['d']; 
       $table1_e = $row['e']; 
       echo 'This code is thirty lines long and appears 
       identical in many places in the website. 
       It uses many variables, like '.$table1_a.','.$table1_b.', 
       '.$table1_c.','.$table1_d.','.$table1_e.', and others'; 
      } 
      ?> 
     </section> 
     <section> 
      <?php 
      $query = "SELECT a, b, c, d, e FROM table2"; 
      $result = mysqli_query($connection,$query); 
      while ($row = mysqli_fetch_assoc($result)) { 
       $table2_a = $row['a']; 
       $table2_b = $row['b']; 
       $table2_c = $row['c']; 
       $table2_d = $row['d']; 
       $table2_e = $row['e']; 
       echo 'This second code is different to the other, 
       but is thirty lines long as well and is repeated 
       in many other places of the website. 
       It uses many variables, like'.$table2_a.', 
       '.$table2_b.','.$table2_c.','.$table2_d.', 
       '.$table2_e.' and others'; 
      } 
      ?> 
     </section> 
    </main> 

我想象的方式有两种:与包括与功能

随着包括我会写-without的querys回响,因为它们是分开所有不同 - 文件夹/ includes中的文件,然后我在页面中调用它们。

在一个文件中包括/ echo_1.php为包括将像:

<?php 
    echo 'This second code is different to the other, 
    but is thirty lines long as well and appears identical in 
    many other places of the website. 
    It uses many variables, like '.$table2_a.','.$table2_b.','.$table2_c.', 
    '.$table2_d.','.$table2_e.' and others'; 
?> 

而在功能的功能/ echo_1.php将是:

<?php 
    function echo_1($table1_a,$table1_b,$table1_c,$table1_d,$table1_e){ 
     echo 'This second code is different to the other, but is thirty lines 
     long as well and appears identical in many other places of the website.      
     It uses many variables, like 
     '.$table1_a.','.$table1_b.','.$table1_c.','.$table1_d.', 
     '.$table1_e.' and others'; 
    } 
?> 

而调用:

<main> 
     <section> 
      <?php 
      $query = "SELECT a, b, c, d, e FROM table1"; 
      $result = mysqli_query($connection,$query); 
      while ($row = mysqli_fetch_assoc($result)) { 
       $table1_a = $row['a']; 
       $table1_b = $row['b']; 
       $table1_c = $row['c']; 
       $table1_d = $row['d']; 
       $table1_e = $row['e']; 
       //Calling the include from the file echo_1.php 
       include 'includes/echo_1.php'; 
      } 
      ?> 
     </section> 
     <section> 
      <?php 
      $query = "SELECT a, b, c, d, e FROM table2"; 
      $result = mysqli_query($connection,$query); 
      while ($row = mysqli_fetch_assoc($result)) { 
       $table2_a = $row['a']; 
       $table2_b = $row['b']; 
       $table2_c = $row['c']; 
       $table2_d = $row['d']; 
       $table2_e = $row['e']; 
       //Using the function echo_1 to create the echo 
       echo_1($table1_a,$table1_b,$table1_c,$table1_d,$table1_e); 

      } 
      ?> 
     </section> 
    </main> 

此外,还有可能是回声有部分代码 - 带查询 - 重复在其他不同的回声之间,我认为不重复它们是很棒的,也许在另一个包含或其他函数内部。

With includes我打算拨打很多电话给另一个文件,这可能会减慢网站。有了函数,我只打一个电话,但我不知道这是否会更好。哪个选项是最有效的方法,包含或包含函数?任何关于编写可维护代码的建议都会受到欢迎!

N.

+3

我建议你看看进入mvc框架 –

+1

是的,罗伯特是对的。从Laravel这样的事情开始,它已经在这方面采取了一些最佳实践。 – ceejayoz

+0

最好使用一个框架,否则你必须遵循mvc架构,你也可以使用一个像twig的模板引擎 –

回答

1

有更好的方法来做到这一点,但为防止有些重复自己,你能做出这样的改变:

<main> 
    <section> 
     <?php 
     section('table1'); 
     ?> 
    </section> 
    <section> 
     <?php 
     section('table2'); 
     ?> 
    </section> 
</main> 

而且你的函数:

function section($table) { 
      $query = "SELECT a, b, c, d, e FROM $table"; 
      $result = mysqli_query($connection, $query); 
      while ($row = mysqli_fetch_assoc($result)) { 
       $table_a = $row['a']; 
       $table_b = $row['b']; 
       $table_c = $row['c']; 
       $table_d = $row['d']; 
       $table_e = $row['e']; 
       echo 'This code is thirty lines long and appears 
       identical in many places in the website. 
       It uses many variables, like '.$table_a.','.$table_b.', 
       '.$table_c.','.$table_d.','.$table_e.', and others'; 
      } 
} 
+0

因此,使用包含代码的函数是很常见的...而且里面有Querys!这很好... 问题在于Querys是完全不同的,所以将它们放在函数内部是没有用的,因为那样我将需要与重复代码块一样多的函数,而且我不会减少它。 – Nik

+0

从示例代码中不太明显。它仍然可以工作,您只需发送比表名更多的信息。在这种情况下使用数组可能会有所帮助。 – RST