2011-10-02 81 views
0

我不確定這是否可能,但如果是這樣的話,我想它會通過PHP(我總共是初學者)。從PHP文件輸入HTML文件中的DIV內容

基本上我有一個.html文件,裏面有一個空的DIV標籤,我希望得到那個存儲在.php文件中的DIV標籤的內容,並將它放到DIV中。

嘗試做這更清楚一點......

HTML FILE 


<div id="content"> 

</div> 

和...

CSS 

#content { 
position:relative; 
width:700px; 
max-height:550px; 
min-height:10px; 
overflow:auto; 
} 

#content h1 { 
font-family:"Kristen ITC"; 
font-size:20px; 
font-weight:bold; 
margin-top:20px; 
margin-bottom:0px; 
margin-left:70px; 
text-align:left; 
color:#000000; 
text-decoration:underline; 
} 

#content p { 
font-family:"Bradley Hand ITC"; 
font-size:22px; 
font-weight:bold; 
margin-top:0px; 
margin-bottom:0px; 
margin-left:90px; 
margin-right:130px; 
text-align:left; 
color:#000000; 
text-decoration:none; 
} 

然後,我希望能有這樣的...

<h1>- Chapter 1</h1> 
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus molestie orci at orci blandit consectetur dapibus elit sollicitudin. Donec diam libero, feugiat vitae egestas eget, pellentesque eget tellus.</p> 
    <h1>- Chapter 2</h1> 
    <p>Phasellus in libero at ligula tincidunt convallis et a libero. Ut elementum molestie enim, vitae consequat mi imperdiet in. Ut metus justo, pharetra vel convallis et, eleifend vitae est. Aenean fringilla tincidunt nunc ac gravida.</p> 

在.php文件中,然後我將鏈接到.html文件中,像這樣...

HTML FILE 

<div id="content"> 

(Somehow link the .php. file here to get the <h1> and <p> stuff) 

</div> 

我希望這是有道理的。

目前我已經試過(記住我是一個初學者,所以不要笑,如果我只是愚蠢)...

HTML FILE 
<div id="content"> 

$contents = file_get_contents('current.php'); 

</div> 

與PHP文件是...

PHP FILE 

<?php 
// print output 
<h1>- Chapter 1</h1> 
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus molestie orci at orci blandit consectetur dapibus elit sollicitudin. Donec diam libero, feugiat vitae egestas eget, pellentesque eget tellus.</p> 
    <h1>- Chapter 2</h1> 
    <p>Phasellus in libero at ligula tincidunt convallis et a libero. Ut elementum molestie enim, vitae consequat mi imperdiet in. Ut metus justo, pharetra vel convallis et, eleifend vitae est. Aenean fringilla tincidunt nunc ac gravida.</p> 
?> 

我不知道我在做什麼,我是否比較接近完成這項工作,距離實現它還有多遠,或者甚至可能。

在一個完美的世界裏,我可以只使用.html文件和.css文件,它看起來像這樣example I made on jsfiddle

誰能幫助我?

回答

2

你是包括一個文件(不是1:1複製文件的內容)。嘗試:

<div id="content"> 
<?php include('current.php'); ?> 
</div> 

這將剝開<?php ... ?>標籤,希望它有幫助。文檔is available

+0

謝謝你這個工作!我不得不刪除我的PHP文件中的標籤,就像Rob W建議的那樣,但它現在全部正常工作! – Flickdraw

1

使用此:

HTML FILE 
<div id="content"> 
<?php 
echo file_get_contents('current.php'); 
?> 
</div> 

得到你的 「PHP」 文件,該文件只包含普通的HTML擺脫<?php?>的。

如果你想添加動態內容,在服務器端,你必須包含<?php?>內的PHP代碼。在定義純HTML時,請勿使用<?php,因爲<?php會通知解釋器將<?php?>之間的內容解析爲PHP代碼。

+0

感謝您的回覆,因爲您可能會告訴我這是一個極端的新手。我試過這種方法,但它不想爲我工作。不過,您已經給了我更多關於標籤的瞭解。我設法使用hakre的方法以及刪除該標籤的建議,現在它工作得很好。謝謝! – Flickdraw

+0

我建議使用'file_get_contents'而不是'include',因爲你使'current.php'看起來像一個HTML文件。 'include'實際上解析了包含文件中的PHP標籤,而'echo file_get_contents($ file);'顯示了文件而不解析它。 –

0

你應該使用file_get_contents('sourcefile.php')函數來檢索源文件的內容。

如果源文件包含代碼,則可以使用require()或include()。 欲瞭解更多信息,請閱讀php.net手冊。