PHP guru. I just learned PHP and encountered some problems in generating static pages, that is, anti-page problems. I hope I can provide a beautiful and complete page turning code. thank you.

Source: Internet
Author: User
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;-W3CDTDXHTML1.0TransitionalEN & quot; www. w3.orgTRxhtml1DTDx big brother PHP experts. I just learned PHP and encountered some problems in generating static pages. this is the anti-page problem. I hope I can 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 ");

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.