php循环生成html
这时候我写的生成html的代码,只能循环一次,不知道该怎么改?<?php$con=mysql_connect("localhost","root","");if(!$co...
这时候我写的生成html的代码,只能循环一次,不知道该怎么改?
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("getjson",$con);
$sql = "SELECT * from user";
$result = mysql_query($sql,$con);
$data = mysql_fetch_array($result);
$tiaoshu = count($data);
$tpl = 'index.tpl';
for ($i = 0; $i < $tiaoshu; $i++) {
$content = file_get_contents($tpl); //读取模板文件的内容
$content = str_replace('{title}',$data['username'],$content); //用具体数据来替换模板标签
file_put_contents($data['id'].'.html',$content); //将替换后的内容写进html文件中
}
?> 展开
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("getjson",$con);
$sql = "SELECT * from user";
$result = mysql_query($sql,$con);
$data = mysql_fetch_array($result);
$tiaoshu = count($data);
$tpl = 'index.tpl';
for ($i = 0; $i < $tiaoshu; $i++) {
$content = file_get_contents($tpl); //读取模板文件的内容
$content = str_replace('{title}',$data['username'],$content); //用具体数据来替换模板标签
file_put_contents($data['id'].'.html',$content); //将替换后的内容写进html文件中
}
?> 展开
2个回答
展开全部
你只是读了一条,循环怎么能出现多次呢
for 循环换一下
while($data = mysql_fetch_array($result))
{
$content = file_get_contents($tpl); //读取模板文件的内容
$content = str_replace('{title}',$data['username'],$content); //用具体数据来替换模板标签
file_put_contents($data['id'].'.html',$content); //将替换后的内容写进html文件中
}
for 循环换一下
while($data = mysql_fetch_array($result))
{
$content = file_get_contents($tpl); //读取模板文件的内容
$content = str_replace('{title}',$data['username'],$content); //用具体数据来替换模板标签
file_put_contents($data['id'].'.html',$content); //将替换后的内容写进html文件中
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询