2014-07-02 18 views
-1

我是新來的此語言。我正在一個網站上工作。我使用這樣的HTML文件:將PHP文件整合到HTML文件中

<html lang="en"> 
... 
<div class="" id="temperatura" name="temp"> 
     <?php require 'php/staticsTemp.php'; ?> 
     <h3 class="centered">Temperature</h3> 
      <hr> 
     <br> 
      <table class="tg" border="5"> 
       <tr> 
        <th class="tg-031e">Temperature ºC</th> 
        <th class="tg-031e">Date & Time</th> 
       </tr> 
       <tr> 
        <td class="tg-031e">33</td> 
        <td class="tg-031e">44</td> 

       </tr> 
      </table> 
     </div> 
    ... 
</html> 

而且我想將表中的值33和44替換爲PHP文件中的值。 我的PHP看起來像這樣:

<?php 
include("ligacaobd.php"); 
$sql="SELECT * FROM Valores ORDER BY Momento DESC LIMIT 20"; 
$result = mysql_query($sql, $ligacaobd) or die(mysql_error()); 
$rowValor = mysql_fetch_assoc($result); 
do 
{ 
    $data[date('d/m/Y H:i:s', $rowValor['Momento'])]=$rowValor['Temperatura']; 
} 
while ($rowValor= mysql_fetch_assoc($result)); 
?> 

有什麼想法?我嘗試使用POST函數,但在HTML中不起作用。

回答

0

我不是很肯定你的意思,但我會盡力幫助你的。

首先不再使用mysql函數了。這些功能不再被維護。改爲使用** mysqliPDO

如果您想在HTML文檔中顯示變量,您可以執行以下操作。

<tr> 
    <td class="tg-031e"><?php echo $var1; ?></td> 
    <td class="tg-031e"><?php echo $var2; ?></td> 
</tr> 

此外,我建議你從PHP中分離HTML文件或至少將在文檔的頂部 PHP代碼。例如:

<?php 
    $var1 = 'Example Variable'; 
?> 
<html lang="en"> 
<head> 
    <title>Example</title> 
</head> 
<body> 
<?php echo $var1; ?> 
</body> 
<html> 

然而,最好的做法是從PHP中分離HTML。

由於您是PHP語言的新手,我爲您提供了一些很棒的教程。 看看http://www.w3schools.com。他們有一些基本的PHP教程供您開始。

好運。

+0

w3schools經常有過時和不正確的信息,學習PHP工作的更好的地方是他們自己的手冊 - http://www.php.net/manual/en/index.php – Eeji

+0

對於真正的基礎知識(if其他等)W3schools很好,但你是正確的官方文件更好 –

1

您必須按照原樣將.php轉換爲html,並直接在頁面中或通過另一個php頁面包含此php代碼。

然後,您將可以操縱你的變量,做一些事情,如:

<td class="tg-031e"><?php echo $myVariable1; ?></td> 
<td class="tg-031e"><?php echo $myVariable2; ?></td> 
+0

這是做的最好方法是什麼? – user3763489

+0

我不知道你最好是什麼意思。 可以肯定的是,爲了讓你的服​​務器知道他必須生成一個帶有替換變量的html頁面,它必須是.php 而且我建議你按照上面給出的關於sql查詢的提示。 – user1835565

0

Don't usemysql_*functions anymore!它們在PHP 5.5中被棄用,應該在5.6中被刪除。 Use PDO instead

尚未測試,但應該工作。你只需要改變你的<table />代碼如下:

<table class="tg" border="5"> 
    <tr> 
     <th class="tg-031e">Temperature ºC</th> 
     <th class="tg-031e">Date & Time</th> 
    </tr> 
<?php foreach ($data as $datetime => $temperature): ?> 
    <tr> 
     <td class="tg-031e"><?php echo $temperature; ?></td> 
     <td class="tg-031e"><?php echo $datetime; ?></td> 
    </tr> 
<?php endforeach; ?> 
</table> 

希望它能幫助:)