2014-12-03 38 views
0

我想知道哪一个是创建网站的最佳方式。 我在我的网站上有'导航栏',内容是'和'页脚'。因为我的网站是完全动态的,因此我想在大多数页面中加载“导航栏”和“页脚”。所以请建议我以最好,最快的方式来完成这项任务。管理和链接php文件的最佳方法,

例子:

|----------------------| 
|  Header   | 
|      | 
|======================- 
|      |\ 
|      | \ 
|      | --->> Want to change this area, in different pages, Login, Signup, 
|  Content  |/ About Us, Contact etc.. but the header and footer remains 
|      |/  same. 
|      | 
|======================| 
|  Footer   | 
|----------------------| 

问:我的头HTML代码太长,包含像滑块,导航栏和与之相关的很多jQuery代码很多东西。所以如果我将它复制到所有页面中,它会使页面滞后?或者我应该保存标题分开的文件和页脚分开,并包括他们与PHP?

+0

您有哪些服务器技术可以访问? PHP/JSP?如果只有客户端,肯定使用jQuery/Ajax – mplungjan 2014-12-03 11:57:54

+0

。使用[require_once()](http://php.net/manual/en/function.require-once.php)明确包含页眉和页脚文件 – Ranjith 2014-12-03 11:58:23

+0

我使用php,jquery ..我不太了解关于ajax。 – 2014-12-03 11:59:30

回答

0

你的结构看起来就像这样,

的header.php

|----------------------| 
|  Header   | 
|======================| 

footer.php

|======================| 
|  Footer   | 
|----------------------| 

现在,您需要使用require_once方法包括。

page1.php中

require_once('header.php'); 

    // Body Content 

    require_once('footer.php'); 

使page2.php

require_once('header.php'); 

    // Body Content 

    require_once('footer.php'); 

还为您jQueryjs代码创建script.js文件和粘贴。现在能能如果你想使用jQuery有这样一个外部文件中使用此命令<script src="script.js" />

+0

谢谢,还有其他可能的方法吗?但不能在每个页面中复制和粘贴页眉和页脚。 – 2014-12-03 12:10:10

+0

我不认为,有没有其他更好的方法来使用PHP。 – Ranjith 2014-12-03 12:14:03

+0

好的,是更好还是与Ajax?或者请告诉我可以把头部也放在头文件中? – 2014-12-03 12:15:56

0

到包括文件:

$(function() { 
    $("#header").load("header.html"); 
    $("#footer").load("footer.html"); 
}); 

,并有

<body> 
    <div id="header"></div> 
    <div id="content">....</div> 
    <div id="footer"></div> 
</body> 

话,甚至可以交换content

$(function() { 
    $("#header").load("header.html",function() { 
    $("#header").on("click",".navigation",function(e) { 
     e.preventDefault(); 
     $("#content").load(this.href); 
    }); 
    $("#login").click(); // load first content 
    }); 
    $("#footer").load("footer.html"); 
}); 

其中报头中的链接的hrefs点t o login.html,about.html,

+0

嗯,看起来更好。想要交换内容.. – 2014-12-03 12:26:34

0

如果页眉和页脚是静态的,则可以有一个包含页眉和页脚的布局,并且可以在其中包含动态内容。这是很多PHP框架的做法。

<?php 

$page = (isset($_GET['page']) && !empty ($_GET['page'])) ? $_GET['page'] . '.php' : 'index.php'; 

?> 

<html> 
    <head> 
    </head> 
    <body> 
     <header></header> 

     <?php include_once $page; ?> 

     <footer></footer> 
    </body> 
</html> 
+0

闻起来像iframe,但如果我在yourfile.php中的链接,我想去另一个页面,那么我怎么可以改变这包括一次。 – 2014-12-03 12:23:55

+0

使用一些变量include_once $ yourFile;并在$ yourFile你将有字符串,你将通过你的URL生成 – 2014-12-03 12:27:05

+0

在我的代码中,你不能找到像iframe之类的东西,这是太不同了 – 2014-12-03 12:27:47