Dear Big Brother PHP experts, I just learned PHP and encountered some problems in generating static pages. this is the problem of anti-page. I hope to provide a beautiful and complete page turning code. thank you! & Lt ;! DOCTYPEhtmlPUBLIC & quot;-// W3C // DTDXHTML1.0Transit big brother PHP experts. I just learned PHP and encountered some problems in generating static pages, that is, the anti-page problem, I hope to provide a beautiful and complete page turning code. thank you!
Php static html paging implementation method
$ Conn = mysql_connect ('localhost', 'root', '123 ')
Or die ('connection failed: '. mysql_error ());
// Select a data table
If (mysql_select_db ('bbs ', $ conn ))
{
Echo: The database is selected! '.'
';
}
Else
{
Echo 'database selection failed! '. Mysql_error ().'
';
}
@ Header ("Content-Type: text/html; charset = utf-8 ");
Mysql_query ("set names 'utf8 '");
$ Fp = fopen ("moban/temp.html", "r ");
$ Content = fread ($ fp, filesize ("moban/temp.html "));
$ Onepage = 2;
$ SQL = "select id from message ";
$ Query = mysql_query ($ SQL );
$ Num = mysql_num_rows ($ query );
$ Allpages = ceil ($ num/$ onepage );
$ Filedir = "news_1 ";
If (! Is_dir ($ filedir )){
// If it does not exist, create
Mkdir ($ filedir, 0777 );
}
For ($ I = 0; $ I <$ allpages; $ I ++ ){
If ($ I = 0 ){
$ Indexpath = "$ filedir/index.html ";
} Else {
$ Indexpath = "$ filedir/index _". $ I. ". html ";
}
$ Start = $ I * $ onepage;
$ List = '';
$ SQL _for_page = "select * from message limit $ start, $ onepage ";
$ Result = mysql_query ($ SQL _for_page );
While ($ row = mysql_fetch_array ($ result ))
{
$ List. = $ row ['id']. "". $ row ['title'].'
'. $ Row ['content'].'
';
}
$ Content1 = str_replace ("{articletable}", $ list. $ I, $ content );
// Pagination
$ List1 = '';
For ($ j = 0; $ j <$ allpages; $ j ++ ){
If ($ j = 0 ){
$ List1. = 'name'. $ j. 'page | ';
} Else {
$ List1. = "". $ j. "page | ";
}
}
$ Content2 = str_replace ("{mune}", $ list1, $ content1 );
If (is_file ($ indexpath )){
@ Unlink ($ indexpath); // delete a file if it already exists
}
$ Handle = fopen ($ indexpath, "w"); // open the file pointer and create a file
/*
Check whether the file is created and writable.
*/
If (! Is_writable ($ indexpath )){
Echo "File:". $ indexpath. "cannot be written. check its attributes and try again! "; // Modify it to echo
}
If (! Fwrite ($ handle, $ content2) {// write information to the file
Echo "generating file". $ indexpath. "failed! "; // Modify it to echo
}
Fclose ($ handle); // Close the pointer
}
Fclose ($ fp); die ("Generation of paging files is complete. if generation is incomplete, check the file permission system and generate a new one! ");
?>
------ Solution --------------------
/*
$ Fp = fopen ("tmp.htm", "r"); // read-only open template
$ Str = fread ($ fp, filesize ("tmp.htm"); // read the template content
// Replace the content to be replaced with the content to be generated in the next step.
$ Str = str_replace ("{title}", 'New title', $ str); // replace the title with the new title.
$ Str = str_replace ("{content}", 'New content', $ str); // replace content with new content
Fclose ($ fp); // close the file
Export handleappsfopen('news.htm', "w"); // write mode to open the news path
Fwrite ($ handle, $ str); // write the replaced content into the generated HTM file.
Fclose ($ handle );
Echo "generated successfully ";
*/
// Batch generate
$ Row = array ("news Title", "news content"), array ("news Title 2", "news content 2"); // Two-dimensional array
Foreach ($ row as $ id => $ val ){
$ Title = $ val [0];
$ Content = $ val [1];
Using pathpattern using id.'.htm ';
$ Fp = fopen ("tmp.htm", "r"); // read-only open template
$ Str = fread ($ fp, filesize ("tmp.htm "));
// $ Str = file_get_contents ("tmp.htm", "r ");