Php batch generate html and txt files
- $ Link = mysql_connect ("mysql_host", "mysql_user", "mysql_password") or die ("cocould not connect:". mysql_error ());
- Mysql_query ("set names utf8 ");
- Mysql_select_db ("my_database") or die ("cocould not select database ");
- ?>
Php batch generate html
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 ['inserted'];
- $ Path = "html/$id.html ";
- $ Fp = fopen ("template.html", "r"); // read-only open template
- $ Str = fread ($ fp, filesize ("template.html"); // read the template content
- $ Str = str_replace ("{title}", $ title, $ str );
- $ Str = str_replace ("{introduce}", $ introduce, $ str); // replace content
- Fclose ($ fp );
- $ Handle = fopen ($ path, "w"); // The path to open the news in writing mode.
- Fwrite ($ handle, strip_tags ($ introduce); // write the replaced content into the generated HTML file.
- Fclose ($ handle );
- // Echo "generated successfully "."
";
- }
/* Release resources */
- Mysql_free_result ($ result );
- Mysql_close ($ link );
- ?>
Template.html file content:
-
- {Title}
- {Introduce}
Php batch generate txt
Require_once ("conn. php ");
$ Query = "SELECT kid, title, introduce FROM pro_courses ";
- $ Result = mysql_query ($ query) or die ("Query failed:". mysql_error ());
/* Generate txt results */
- While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC )){
-
- $ Id = $ row ['id'];
- $ Title = $ row ['title'];
- $ Introduce = $ row ['inserted'];
- $ Path = "html/$id.txt ";
- $ Handle = fopen ($ path, "w"); // The path to open the news in writing mode.
- Fwrite ($ handle, strip_tags ($ introduce); // write the replaced content into the generated txt file.
- Fclose ($ handle );
- }
/* Release resources */
- Mysql_free_result ($ result );
- Mysql_close ($ link );
- ?>
|