-
- $link = mysql_connect("mysql_host" , "mysql_user" , "mysql_password" )or die("Could not connect : " . mysql_error());
- mysql_query("set names utf8");
- mysql_select_db("my_database") or die("Could not select database");
- ?>
复制代码
php 批量生成html
-
-
require_once(“conn.php”);
$query = "SELECT id,title,introduce FROM my_table";
- $result = mysql_query($query) or die("Query failed : " . mysql_error());
/* 生成 HTML 结果 */
- while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
-
- $id=$row['id'];
- $title=$row['title'];
- $introduce=$row['introduce'];
- $path="html/$id.html";
- $fp=fopen("template.html","r"); //只读打开模板
- $str=fread($fp,filesize("template.html"));//读取模板中内容
- $str=str_replace("{title}",$title,$str);
- $str=str_replace("{introduce}",$introduce,$str);//替换内容
- fclose($fp);
- $handle=fopen($path,"w"); //写入方式打开新闻路径
- fwrite($handle,strip_tags($introduce)); //把刚才替换的内容写进生成的HTML文件
- fclose($handle);
- //echo "生成成功"."
";
- }
/* 释放资源 */
- mysql_free_result($result);
- mysql_close($link);
- ?>
-
复制代码
template.html文件内容:
php 批量生成txt
-
-
require_once(“conn.php”);
$query = "SELECT kid,title,introduce FROM pro_courses";
- $result = mysql_query($query) or die("Query failed : " . mysql_error());
/* 生成 txt 结果 */
- while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
-
- $id=$row['id'];
- $title=$row['title'];
- $introduce=$row['introduce'];
- $path="html/$id.txt";
- $handle=fopen($path,"w"); //写入方式打开新闻路径
- fwrite($handle,strip_tags($introduce)); //把刚才替换的内容写进生成的txt文件
- fclose($handle);
- }
/* 释放资源 */
- mysql_free_result($result);
- mysql_close($link);
- ?>
-
复制代码
|
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31