php脚本的输出需要保存在$ content中

时间:2014-08-14 08:49:41

标签: php mysql

如果php脚本的输出需要保存在$ content中,我如何调整下面的php?

<?php

//db verbindung
mysql_connect("Hostname", "Username", "Password");
mysql_select_db("Database Name");
//db abfrage
$query = "SELECT 
    YEAR(datetime) AS dy, 
    MONTH(datetime) -1 AS dm, 
    DAY(datetime) AS dd, 
    HOUR(datetime) AS th, 
    MINUTE(datetime) AS tm, 
    temp, 
    hum, 
    pressure 
    FROM wettertabelle 
    WHERE DATE(datetime) = '2013-11-25' 
    ORDER BY datetime
";
// NEU: Variable definieren
$zeilenzaehler = 1;
//ausgabe der zeilen
$result = mysql_query($query)
        OR die("Error: $query <br>" . mysql_error());
while ($row = mysql_fetch_array($result)) {
// echo 
    if ($zeilenzaehler != 1) {
        echo ",";
    }
    echo "{date: new Date(" . $row['dy'] . "," . $row['dm'] . "," . $row['dd'] . "," . $row['th'] . "," . $row ['tm'] . "),t:" . $row['temp'] . ",h:" . $row['hum'] . ",p:" . $row['pressure'] . "}";
//Variable jetzt auf 2
    $zeilenzaehler = 2;
};
?>

什么是有效的,例如:

<?php
$content = This is the output!!
?>

<?php
ob_start();
echo 'This is the output!!';   
$content=ob_get_contents();
ob_end_clean();
?>

我如何调整上面的脚本以使其进入以下输出结构:

<?php
class user_klasse
{
function ausgabe()
{
return 'This test is generated by php.';
}
}
?>

1 个答案:

答案 0 :(得分:1)

您只有两个echo语句,更改它们以保存变量中的值。对于那么小的东西,不需要输出缓冲。

if ($zeilenzaehler != 1)
{
  $content.= ",";    // instead of echo, and same for the one below
}
$content.= "{date: new Date(".$row['dy'].",".$row['dm'].",".$row['dd'].",".$row['th'].",".$row ['tm']."),t:".$row['temp'].",h:".$row['hum'].",p:".$row['pressure']."}";

最后你可以做到

echo $content;