HTML页面试图从php / mysql

时间:2017-05-13 22:16:41

标签: php html mysql

这个相当简单的问题令我烦恼。我有一个用CSS布局的html页面。我有一个简单的PHP页面,从mysql站点返回最新的记录。我需要在" leftContent"的html页面中显示这些信息。格。

working' latest.php'页面是:

<?php
/* 
Return the latest date and record for the left pane.
*/

include 'ESP8266_dbLogin.php';

$result = mysqli_query($link, "SELECT * FROM `thLog` ORDER BY logID DESC LIMIT 1") or die ("Connection error");

while($row = mysqli_fetch_array($result)) {
echo "Date: " . $row['logDate'] . "<br>";
echo "lightVal: " . $row['lightVal'];
}
mysqli_close($link);
?>

&#39;的index.html&#39;代码如下,对于php变量,目标DIV为contentsLeft:

<!DOCTYPE html>
<!-- Basic Layout -->
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"  http-equiv="content-type">
    <title>Sparks - Monitor</title>
    <link rel="stylesheet" type="text/css"  href="css/default.css">
    <link href='//fonts.googleapis.com/css?family=Baloo' rel='stylesheet'>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>
    <script type="text/javascript" src="data24.js" ></script>
  </head>
  <body>
  <div id="wrapper">
    <div id="main">
      <div id="banner">
        <div id="bannerLeft">
          <img class="bannerImg" alt="ESP8266 Logo" src="images\imgESP8266.png">
        </div>
        <div id="bannerRight">
          <img class="bannerImg" alt="Settings Icon" src="images\imgGear.png">
        </div>
        <div id="bannerMain">
          <h1>SPARKS Energy Monitor - Home</h1>
        </div>
      </div>
      <div class="content" id="contentLeft">
        <h2>Current Usage:</h2>
        <p> The current lightVal and date should be here<p>
      </div>
      <div class="content" id="contentRight">
        Generating chart, please wait...
      </div>
    </div>
  </div>  
  </body>
<html>

感谢任何帮助以最有效的方式将php变量(lightVal)放入html页面。我知道,愚蠢的问题!!

1 个答案:

答案 0 :(得分:0)

1)将您的index.html重命名为index.php
2)用PHP代码替换目标div

  <div class="content" id="contentLeft">
    <h2>Current Usage:</h2>
    <?php
        /* 
        Return the latest date and record for the left pane.
        */
        include 'ESP8266_dbLogin.php';

        $result = mysqli_query($link, "SELECT * FROM `thLog` ORDER BY logID DESC LIMIT 1") or die ("Connection error");
        while($row = mysqli_fetch_array($result)) {
    ?>
      <p class="dateClass"><?=$row['logDate']?></p>
      <p class="lightValClass"><?=$row['lightVal']?></p>
    <?
        }
        mysqli_close($link);
    ?>
  </div>

应该就够了,只需从浏览器调用index.php即可。