[英]Write in html file with PHP
我想用PHP用html文件編寫,但只寫了最后一個:請幫助我:
<?php
$link = mysql_connect('my_server', 'user', 'password')
or die("Can't");
$db = mysql_select_db('database')
or die ("Can't select");
$sql = 'SELECT * FROM News LIMIT 3';
$retval = mysql_query($sql,$link);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
//echo "<pre>"; print_r($row); echo "</pre>";
$content = '<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<div class="block">';
$content .='<a href="="">'.$row['Title'].'</a>';
$content .='<div class="date">';
$content .=$row['Date'];
$content .='</div>';
$content .= '</div>';
$filename ="footer.html";
@unlink($filename);
$handle = fopen("footer.html", 'w+');
echo $row['Title'].'<br>';
if ($handle)
{
if (!fwrite($handle, $content))
die("cant' write");
}
}
?>
只寫最后一個查詢..我試過但沒收到...
您需要將初始$ content變量和文件寫入部件放到for循環之外。
$content = '';
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
//echo "<pre>"; print_r($row); echo "</pre>";
$content .= '<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<div class="block">';
$content .='<a href="="">'.$row['Title'].'</a>';
$content .='<div class="date">';
$content .=$row['Date'];
$content .='</div>';
$content .= '</div>';
}
$filename ="footer.html";
@unlink($filename);
$handle = fopen("footer.html", 'w+');
echo $row['Title'].'<br>';
if ($handle)
{
if (!fwrite($handle, $content))
die("cant' write");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.