Home  >  Article  >  Backend Development  >  Use related functions to implement PHP processing paging_PHP tutorial

Use related functions to implement PHP processing paging_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:34:52694browse

By pairing

as we specify paging, 20 articles per page. There are 45 articles in a certain sub-channel list according to the database query. First, we obtain the following parameters through query: 1, the total number of pages; 2, the number of articles per page.

The second step of PHP processing of paging, for ($i = 0; $i < allpages; $i++), page element acquisition, analysis, and article generation are all executed in this loop. The difference is that the sentence die ("Create file".$filename."Success!"; is removed and placed in the display after the loop, because this statement will terminate program execution. Example:

  1. < ?php  
  2. $fp = fopen ("temp.html","r");  
  3. $content = fread ($fp,filesize ("temp.html"));  
  4. $onepage = '20';  
  5. $sql = "select id from article where 
    channel='$channelid'"
    ;  
  6. $query = mysql_query ($sql);  
  7. $num = mysql_num_rows ($query);  
  8. $allpages = ceil ($num / $onepage);  
  9. for ($i = 0;$i<$allpages; $i++){  
  10. if ($i == 0){  
  11. $indexpath = "index.html";  
  12. } else {  
  13. $indexpath = "index_".$i."html";  
  14. }  
  15. $start = $i * $onepage;  
  16. $list = '';  
  17. $sql_for_page = "select name,filename,title 
    from article where channel='$channelid'
     limit $start,$onepage"
    ;  
  18. $query_for_page = mysql_query ($sql_for_page);  
  19. while ($result = $query_for_page){  
  20. $list .'.$root.$result['filename']
    .'
    target=_blank>'.$title.'a> <br>';

  21. }
  22. $content = str_replace ( "{articletable}
    ",$list,$content);
  23. if (is_file ($indexpath)){
  24. @ unlink ($indexpath); //If the file already exists, delete it
  25. }
  26. $ handle = fopen ($indexpath,"w");
    //Open the file pointer and create the file
  27. /*
  28. Check if the file is created and writable
  29. */
  30. if (!is_writable ($indexpath)){
  31. echo "File: ".$indexpath." is not writable,
    Please check Try again after its attribute! "; //Change to echo
  32. }
  33. if (!fwrite ($handle,$content)) { //Write information to file
  34. echo "Generate file".$indexpath."Failed!"; //Change to echo
  35. }
  36. fclose ($handle); //Close pointer
  37. }
  38. fclose ($fp);
  39. die ("Generating the paging file is completed. If the generation is incomplete,
    please check the file permission system and regenerate! ");
  40. ?>
  41. Approximately PHP The idea of ​​handling paging is as follows. Other data generation, data input and output checking, paging content pointing, etc. can be added to the page as appropriate.


    .

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445950.htmlTechArticleWhen pagination is specified by us, there are 20 articles per page. The database query of a certain sub-channel list contains 45 articles. First, we obtain the following parameters through query: 1. Total number of pages; 2. Each page...
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