Example of PHP using OB cache to implement static function
- 2021-12-04 18:21:01
- OfStack
In this paper, an example is given to show that PHP uses OB cache to realize static function. Share it for your reference, as follows:
Implementation steps
1. Create test data table and write data
2. Realize the update operation in the background. Use the OB cache to generate the corresponding HTML file for every 1 content
3. Display the data information of the foreground
Concrete realization
① Create a test data table and write the data (test. sql file):
# Create a data table
create table news(
id int auto_increment,
title varchar(100) not null default '',
body text,
primary key(id)
)engine =myisam default charset=utf8;
# Data writing
insert into news values(null,' Static ',' Static can reduce server pressure '),(null,' Pseudo static ',' Pseudo-static state can satisfy SEO Optimization ');
② Realize the update operation in the background (admin. php file)
<?php
// Specific background updates
// Get all the data information
mysql_connect('127.0.0.1','root','123456');
mysql_select_db('test');
$sql='select * from news';
$res = mysql_query($sql);
while ($row=mysql_fetch_assoc($res)) {
// For each 1 Bar data generation html Documents
ob_start();// Open OB Cache
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> Static introduction </title>
</head>
<body>
<h1><?php echo $row['title']; ?></h1>
<div><?php echo $row['body']; ?></div>
</body>
</html>
<?php
// Get OB Content in the cache
$str = ob_get_contents();
// Shut down OB Cache and empty the contents. Because if you don't empty the browser, you will see all the data results
ob_end_clean();
// Write information to a file The specific file directory and file name need to be customized
// For the real project about html Storage of files 1 Generally, it exists in the format of year, month and day
file_put_contents($row['id'].'.html',$str);
}
?>
③ Realize foreground data display (list. php file):
<?php
// Show list
// Get all the data information
mysql_connect('127.0.0.1','root','123456');
mysql_select_db('test');
$sql='select * from news';
$res = mysql_query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> Static introduction </title>
</head>
<body>
<h1> Show list </h1>
<table>
<tr>
<td> Serial number </td>
<td> Title </td>
<td> View </td>
</tr>
<?php while ($row =mysql_fetch_assoc($res)) {?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><a href="<?php echo $row['id'];?>.html" rel="external nofollow" > View </a></td>
</tr>
<?php } ?>
</table>
</body>
</html>
For more readers interested in PHP related content, please check the topics on this site: "Summary of php Cache Technology", "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"
I hope this article is helpful to everyone's PHP programming.