PHP batch generated HTML, TXT file

Source: Internet
Author: User

First create a conn.php file to link the database

<?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 Bulk HTML generation

<?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 Results */
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 to 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 Resources */
Mysql_free_result ($result);
Mysql_close ($link);
?>

template.html File Contents:

<! 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

<?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 to open News path
Fwrite ($handle, Strip_tags ($introduce)); Write the content you just replaced into the generated TXT file
Fclose ($handle);
}

/* Release Resources */
Mysql_free_result ($result);
Mysql_close ($link);
?>

PHP batch generated HTML, TXT file

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.