Home  >  Article  >  Backend Development  >  PHP implementation code for batch generating html and txt files_PHP tutorial

PHP implementation code for batch generating html and txt files_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:02:49937browse

First create a conn.php file to connect to the database

Copy the code The code is as follows:

$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 batch generate html
Copy code The code is as follows:

require_once(“conn.php”);
$query = "SELECT id,title,introduce FROM my_table";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
/* Generate HTML results*/
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$id=$row['id'];
$title=$row['title'];
$introduce= $row['introduce'];
                                                                                                                                                                                                                                                                                         > $ Str = Fread ($ FP, Filesize ("Template.html"); // Read the content in the template
$ Str = Str_replace ("{Title}", $ Title, $ Str);
$str=str_replace("{introduce}",$introduce,$str);//Replacement content
                                                                                           Open the news path
in the import mode ;a href=html/$id.html>Generated successfully"."
";
}
/* Release resources*/
mysql_free_result($result);
mysql_close($link);
?>



template.html file content:


Copy code
The code is as follows:
< meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
{title}


{introduce}





php batch generate txt


Copy code
The code is as follows:
require_once(“conn.php”); $query = "SELECT kid,title,introduce FROM pro_courses";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
/* Generate txt result*/
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"); //Open the news path in writing mode
           fwrite($handle,strip_tags($introduce)); //Write the content just replaced into the generated txt file
                                                                                                                                              who’ whom’ who who’ who’ who’ who’ who who who’ who who who who who who’s who who’ who who who’s’scoming’s,singling, > mysql_free_result($result);
mysql_close($link);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327898.htmlTechArticleFirst create a conn.php file to link to the database. Copy the code as follows: ?php $link = mysql_connect( "mysql_host" , "mysql_user" , "mysql_password" )or die("Could not con...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn