[英]PHP - write variable output to file
我想將變量$ rows的內容寫入文件out.txt。
變量$ rows放在while循環中,可以由從數據庫中檢索的幾個不同的行構成。
不幸的是,下面的代碼只寫了預期輸出的一行(精確的最后一行)。
$myFile = "/var/www/test/out.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$content = $rows['text'];
fwrite($fh, $content);
fclose($fh);
這是PHP代碼(它正在工作),它允許我在瀏覽器中查看$ rows變量的內容。
<?
if(isset($_POST['type'], $_POST['topic'], $_POST['tense'], $_POST['polarity']))
{
$tipo = $_POST['type'];
$argomento = $_POST['topic'];
$tempo = $_POST['tense'];
$segno = $_POST['polarity'];
foreach ($tipo as $key_tipo => $value_tipo)
{
foreach ($argomento as $key_argomento => $value_argomento)
{
foreach ($tempo as $key_tempo => $value_tempo)
{
foreach ($segno as $key_segno => $value_segno)
{
$q_tip_arg_tem_seg = "SELECT * FROM entries WHERE type = '".$value_tipo."' AND topic = '".$value_argomento."' AND tense = '".$value_tempo."' AND polarity = '".$value_segno."'";
$qu_tip_arg_tem_seg = mysql_query($q_tip_arg_tem_seg);
while ($rows = mysql_fetch_array($qu_tip_arg_tem_seg))
{
echo $rows['type']."<br />";
}
}
}
}
}
}
else
{
echo "You forgot to check something."."<br>"."Please select at least one type, topic, tense and polarity value."."<br>";
}
?>
你能幫我嗎? 謝謝。
您只能在文件中寫出一行。 你可能想要這樣的東西:
$myFile = "/var/www/test/out.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
if ($fh) {
while ($row = getrow()) {
$content = $rows['text'];
fwrite($fh, $content); // perhaps add a newline?
}
fclose($fh);
}
因此,打開文件,將所有內容寫入其中,然后關閉它。
你可以用兩種方式做到這一點
1.首先從out.txt文件中獲取詳細信息,然后用你的$ row連接它。如果你已經在out.txt中存在內容,還有$ row text.finaly用內容寫入文件。
2.通過將其轉換為varialble來收集單個變量中的所有行。並執行文件寫入操作。
如果您只想查看數組的外觀,請嘗試使用print_r()它以一種很好的讀取格式打印一個數組。
如果你想要一個更結構化的布局,這可能還不夠。
$myFile = "/var/www/test/out.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, print_r($rows,TRUE) );
fclose($fh);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.