The technical principle of HTML generated by PHP

Source: Internet
Author: User
Tags html sample php file

1 Create a new database in MySQL, name it (customizable), create a new table, name News (because it's a press release, take a good name, you can customize it), and then create the names of these fields:








ID (auto increment, which is key, type: INT)


title (as the name suggests, news title, type of TEXT)


content (News contents, type is preferable TEXT)


path (HTML file path, type preferred TEXT)





2) Establish conn.php








This is the connection database PHP file, you can put the statement of the connection data alone in this file, in the future several need to connect the database file directly refer to the file.








3) Design Add news form Add.form simple source code as follows:








<form method= "POST" action= "add.php" >//Submit to add.php


News title: <input type= "text" name= "title" size= "><br>
"

news content: <textarea name= "Content" cols= "rows=" ></textarea><br>


<input type= "Submit" Name= "submitted" >


</form>











4 to create an HTML template, save as model.htm, and add.php can be in the same directory.








Sample source code:








<html>


<body>


The title of this news: {title}


content of this news: {content}


</body>


</html>







The content within the
{} curly braces is the content to be replaced. The entire static template design can be based on their own ideas, but {} replaced content must be included, such as the above {title},{content}; Click to simply say, design a very good news template, Put the mark to be replaced such as {title},{content} and so on where you need it to sprinkle.





5) Detailed add.php source








<?php


require_once ("conn.php");//reference conn.php, connect database


$title =$_post["title"];


$content =$_post["content"]; Get form variable





//below establish a document, the value of the automatic counting


$countfile = "Count.txt";


if (!file_exists ($countfile))


  {


fopen ($countfile, "w"); If this file does not exist, a
is automatically created

  }


$fp =fopen ($countfile, "R");


$num =fgets ($fp, 20);


$num = $num +1; Each time its value automatically adds a


fclose ($FP);


$fp =fopen ($countfile, "w");


fwrite ($fp, $num); Update its value


fclose ($FP);


//using the above automatic counting value to get the HTML path $path


$houzui = ". html";


$path = $num. $houzui;


//This form of the path is automatic growth, such as 1.html,2.html,3.html ... add a piece of news automatically adds 1





//Below add data to table news
with SQL statements




$sql = "INSERT INTO News (Title,content,path) VALUES ('". $title. "', '". $content. "', '". $path. ")";


$query =mysql_query ($sql);


//Below is the key to replacing the {title},{content} tags in the template with the data obtained from the form





$fp =fopen ("model.htm", "R")//read-only open template


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


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


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


fclose ($FP);





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


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


fclose ($handle);


//Finishing work:





echo "<a href= $path target=_blank> View the news just added </a>";


OK, the whole generation of HTML sample source code is here, the key is to use the replacement method.


$str =str_replace ("{Replaced content}", $ replaced content, $STR);





    Therefore, summary of the above practice: first design a news template, the need to be replaced by {} to the corresponding location in the template, and then design the form, and then the final form handler, the form of the variables obtained from the replacement template in the corresponding content can be, This will generate different HTML each time; If you need to modify the content of the HTML is the same, get the modified form content, first update the database with the UPDATE statement, and then replace the contents of the template can be deleted, first delete the table to delete the content, and then use unlink ($ Path) to delete the physical files of HTML.

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.