2016-04-04 21 views
0

Firts私はPHPを使用してMySQLデータベースから値を取り出しました。これは私のPHPコードです:phpからhtmlへのパス値

<?php 
$dsn = 'mysql:host=localhost;dbname=iot'; 
$username = 'root'; 
$password = ''; 

$dbh = new PDO($dsn, $username, $password); 
//build the query 
$query="SELECT temperaturevalue, humidityvalue FROM sensors"; 

//execute the query 
$data = $dbh->query($query); 
//convert result resource to array 
$result = $data->fetchAll(PDO::FETCH_ASSOC); 

//view the entire array (for testing) 
//print_r($result); 

//display array elements 
foreach($result as $output) { 
echo "temperature Value"; 
echo $output['temperaturevalue']; 
echo "<br/>"; 
echo "<br/>"; 
echo "humidity "; 
echo $output['humidityvalue'] ; 

} 

?> 

ここでは、この値をHTMLページに表示します。この私のhtmlコード:

<!DOCTYPE html> 
<html> 
<head> 
<title>Smart house</title> 
<meta http-equiv="refresh" content="30"> 
<meta charset="UTF-8"> 
<style> 

h1 {color:orange; 
    font-style: italic; 
    font-size:300%} 

img { 
    width:100%; 
} 
</style> 
</head> 
<body> 

<h1><strong>Connect and control</strong></h2> 
<P style="text-align:center;"><img src="myhouse.png" alt="smart house" style="width:50px;height:40px;"></p> 
<br> 

</body> 
</html> 

私はexempleのための私のhtmlページ上で表示するために何を追加する必要があります:これは非常に簡単です

Temperature value : 25.56 
Humidity value : 300 
+0

Htmlファイルは静的です。つまり、動的データを渡すことはできません。 PHPのページ自体にhtmlを印刷してください。 – NoImaginationGuy

答えて

0

。あなたは値をエコーする必要があるときにbalisを開く必要があります。 htmlファイルにはphpコードを含めることができないので、あなたのhtmlファイルは.php拡張でなければならないことを確かめてください。

<h1><strong>Connect and control</strong></h2> 
Temperature value : <?php echo $output['temperaturevalue']; ?> 
Humidity value : <?php echo $output['humidityvalue']; ?> 
<br> 

編集:私は1つのPHPページについてのみ話しています。

+0

停止してください。 $出力は最初のページに含まれる変数です。どのようにその変数を渡すのですか? – NoImaginationGuy

+0

HTMLはPHPコードを処理しないので、彼は彼の新しいPHPファイルにPHPコードを入れますと思いますか? – Kangoo13

+0

ええ、$出力はそのページのローカル変数です – NoImaginationGuy

1

PHPページを表示することはできません。

  1. 「.HTML」から変更し、ファイルの種類「を.PHP」へ
  2. は、データを表示したい 『foreachの』一部ご上記のphpを追加します。また、デザイン(HTML)構造ごとに "エコー"部分を調整してください。
0

この

が.PHPファイルを作成し、.phpのファイル内の.htmlファイルには、あなたはhtmlファイルに値を印刷することができます含ま好きです

例:

test.phpを

<?php 
$dsn = 'mysql:host=localhost;dbname=iot'; 
$username = 'root'; 
$password = ''; 

$dbh = new PDO($dsn, $username, $password); 
//build the query 
$query="SELECT temperaturevalue, humidityvalue FROM sensors"; 

//execute the query 
$data = $dbh->query($query); 
//convert result resource to array 
$result = $data->fetchAll(PDO::FETCH_ASSOC); 

//view the entire array (for testing) 
//print_r($result); 

// foreach code removed and added to html file. 
include("index.html"); 
?> 

index.html

<!DOCTYPE html> 
<html> 
<head> 
<title>Smart house</title> 
<meta http-equiv="refresh" content="30"> 
<meta charset="UTF-8"> 
<style> 

h1 {color:orange; 
    font-style: italic; 
    font-size:300%} 

img { 
    width:100%; 
} 
</style> 
</head> 
<body> 

<?php 
    //display array elements 
    foreach($result as $output) { 
     echo "temperature Value"; 
     echo $output['temperaturevalue']; 
     echo "<br/>"; 
     echo "<br/>"; 
     echo "humidity "; 
     echo $output['humidityvalue'] ; 

    } 

?> 

<h1><strong>Connect and control</strong></h2> 
<P style="text-align:center;"><img src="myhouse.png" alt="smart house" style="width:50px;height:40px;"></p> 
<br> 

</body> 
</html> 

希望すると、これが役立ちます。

0

あなたは1つのPHPファイル内のファイルをマージする必要があります。

<?php 
$dsn = 'mysql:host=localhost;dbname=iot'; 
$username = 'root'; 
$password = ''; 

$dbh = new PDO($dsn, $username, $password); 
//build the query 
$query="SELECT temperaturevalue, humidityvalue FROM sensors"; 

//execute the query 
$data = $dbh->query($query); 
//convert result resource to array 
$result = $data->fetchAll(PDO::FETCH_ASSOC); 

?> 
<!DOCTYPE html> 
<html> 
<head> 
<title>Smart house</title> 
<meta http-equiv="refresh" content="30"> 
<meta charset="UTF-8"> 
<style> 

h1 {color:orange; 
    font-style: italic; 
    font-size:300%} 

img { 
    width:100%; 
} 
</style> 
</head> 
<body> 

<h1><strong>Connect and control</strong></h2> 
<P style="text-align:center;"><img src="myhouse.png" alt="smart house" style="width:50px;height:40px;"></p> 

<? 
//display array elements 
foreach($result as $output) { 
echo "temperature Value"; 
echo $output['temperaturevalue']; 
echo "<br/>"; 
echo "<br/>"; 
echo "humidity "; 
echo $output['humidityvalue'] ; 

} 
?> 
</body> 
</html> 

あなたは問題なくHTMLやPHPのコードを組み合わせることができます。

0

HTMLページに表示している場合は、それを行うことはできません。あなたは、それが働いていたであろうこれらのコードを試してみてください この

<h1><strong>Connect and control</strong></h2> 
<?php foreach($result as $output) { ?> 
Temperature value : <?php echo $output['temperaturevalue']; ?> 
Humidity value : <?php echo $output['humidityvalue']; ?> 

<?php } ?> 

のようなあなたのforeachループを入れて..... PHPへ 変更ファイル拡張子のHTMLに持っています。