search
HomeBackend DevelopmentPHP TutorialThe html template completes page static and template paging processing

It only allows you to modify a certain part of the page. Of course, this "certain part" is determined by you. The artist first makes a page, and then we use this page as a template (it should be noted that this template does not need to use code like EditRegion3, this code is a logo made by Dreamwerver to facilitate its own design), and use this template The place we need to change is replaced with a character that can be distinguished from HTML, such as "{title}", "[title]". When generating a static page, you only need to replace the data with these strings. This is what templates are about.

Create a php page and an html page [template page]; Note: If the data is called from the database, save the data in the form of an array, and then generate it in a loop;
In the php page, open the html Page->Read the content of the html page->Replace parameters->Create (open) a new html page->Write the replaced content into a new file->Close the new file->Generate successfully ;


$open = fopen("template.htm","r"); //打开模板文件
$content = fread($open,filesize("template.htm")); //读取模板文件内容
//print_r($content);
$content = str_replace("{title}","测试标题",$content);//替换
$content = str_replace("{contents}","测试内容",$content);

$newtemp = fopen("1.htm","w");//生成,用写入方式打开一个不存在(新)的页面
fwrite($newtemp,$content);//将刚刚替换的内容写入新文件中
fclose($newtemp);
echo "生成

php batch generation html test:


//假设从数据库中调的数据存放在二维数组$arr中
$arr = array(array("新闻标题一","新闻内容一"),array("新闻标题二","新闻内容二")); 

foreach($arr as $key=>$value){
 $title = $value[0];
 $contents = $value[1];
 //echo $title.''.$contents.'';
 $path = $key.'.html';
 $open = fopen("template.htm","r"); //打开模板文件
 $handle = fread($open,filesize("template.htm")); //读取模板文件内容

 $content = str_replace("{title}",$title,$handle);//替换
 $content = str_replace("{contents}",$contents,$handle);

 $newtemp = fopen($path,"w");//用写入方式打开一个不存在(新)的页面
 fwrite($newtemp,$content);//将刚刚替换的内容写入新文件中
 fclose($newtemp);
 echo "生成";
}

Paging problem:

Like us When paging is specified, there are 20 articles per page. There are 45 articles in a certain sub-channel list after database query. First, we obtain the following parameters through query: 1, total number of pages; 2, number of articles per page. The second step, for ($i = 0; $i


$fp= fopen ("temp.html","r");
$content= fread ($fp,filesize ("temp.html"));
$onepage= ’20’;
$sql    = "select id from article where channel=’$channelid’";
$query  = mysql_query ($sql);
$num    = mysql_num_rows ($query);
$allpages= ceil ($num / $onepage);
for ($i = 0;$i<$allpages; $i++){
    if ($i == 0){
        $indexpath = "index.html";
    } else {
        $indexpath = "index_".$i."html";
    }
    $start = $i * $onepage;
    $list  = ’’;
    $sql_for_page = "select name,filename,title from article where channel=’$channelid’ limit $start,$onepage";
    $query_for_page = mysql_query ($sql_for_page);
    while ($result = $query_for_page){
        $list .= ’<a href=’.$root.$result[’filename’].’ target=_blank>’.$title.’</a><br>’;
    }
    $content = str_replace ("{articletable}",$list,$content);
    if(is_file ($indexpath)){
        @unlink ($indexpath); //若文件已存在,则删除
    }
    $handle = fopen ($indexpath,"w"); //打开文件指针,创建文件
    if (!is_writable ($indexpath)){
        echo "文件:".$indexpath."不可写,请检查其属性后重试!"; //修改为echo
    }
    if (!fwrite ($handle,$content)){   //将信息写入文件
        echo "生成文件".$indexpath."失败!"; //修改为echo
    }
    fclose ($handle); //关闭指针
}
fclose ($fp);
die ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!");

The above is the detailed content of The html template completes page static and template paging processing. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

How do you redirect a page in PHP?How do you redirect a page in PHP?Apr 28, 2025 pm 04:54 PM

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

Explain type hinting in PHPExplain type hinting in PHPApr 28, 2025 pm 04:52 PM

Article discusses type hinting in PHP, a feature for specifying expected data types in functions. Main issue is improving code quality and readability through type enforcement.

What is PDO in PHP?What is PDO in PHP?Apr 28, 2025 pm 04:51 PM

The article discusses PHP Data Objects (PDO), an extension for database access in PHP. It highlights PDO's role in enhancing security through prepared statements and its benefits over MySQLi, including database abstraction and better error handling.

How to create API in PHP?How to create API in PHP?Apr 28, 2025 pm 04:50 PM

Article discusses creating and securing PHP APIs, detailing steps from endpoint definition to performance optimization using frameworks like Laravel and best security practices.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!