用PHP写入html文件

时间:2014-03-25 11:02:33

标签: php html

我想用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");
        }
}
    ?>

只写最后一个查询.. 我试过但没有收到......

1 个答案:

答案 0 :(得分:3)

您需要输入初始$ content变量&amp;文件写入部分外部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");
}