PHP Batch generation Html,txt file implementation code

Source: Internet
Author: User
Tags php file

First, create a conn.php file to link the database

Copy Code code as follows:


<?php


$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 Generation HTML

Copy Code code as follows:


<?php


require_once ("conn.php");


$query = "Select Id,title,introduce from my_table";


$result = mysql_query ($query) or Die ("Query failed:". Mysql_error ());


/* Generate HTML result/*


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"); Read-only open template


$str =fread ($fp, FileSize ("template.html"));//Read the contents of the template


$str =str_replace ("{title}", $title, $STR);


$str =str_replace ("{Introduce}", $introduce, $STR);/replace content


fclose ($FP);


$handle =fopen ($path, "w"); Write mode open News path


fwrite ($handle, Strip_tags ($introduce)); Write the content you just replaced into the generated HTML file


fclose ($handle);


//echo "<a href=html/$id .html> Build Success </a>". <br> ";


}


/* Release of resources */


mysql_free_result ($result);


mysql_close ($link);


?>


template.html File Contents:

Copy Code code as follows:


<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


<html xmlns= "http://www.w3.org/1999/xhtml" >


<head>


<meta http-equiv= "Content-type" content= "text/html"; Charset=utf-8 "/>


<title>{title}</title>


</head>


<body>


{Introduce}


</body>


</html>


PHP Batch Generation txt

Copy Code code as follows:


<?php
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 [' Introduce '];
$path = "html/$id. txt";
$handle =fopen ($path, "w");//write way to open News path
Fwrite ($handle, Strip_tags ($introduce)); Fclose ($handle);
}
* Release resources */
Mysql_free_result ($result);
Mysql_close ($link);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.