PHP Batch generation Html,txt file implementation code _php skills

Source: Internet
Author: User
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 way to open a news path
Fwrite ($handle, Strip_tags ($introduce)); Write what 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 ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>{title}</title>
<body>
{Introduce}
</body>

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 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"); Write way to open a news path
Fwrite ($handle, Strip_tags ($introduce)); Write the content that you just replaced into the generated TXT file
Fclose ($handle);
}
/* Release of 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.