php產生靜態檔案分頁問題

來源:互聯網
上載者:User

如我們指定分頁時,每頁20篇。某子頻道列表內文章經資料庫查詢為45條,則,首先我們通過查詢得到如下參數:1,總頁數;2,每頁篇數。第二步,for ($i = 0; $i < allpages; $i++),頁面元素擷取,分析,文章產生,都在此迴圈中執行。不同的是,die ("建立檔案".$filename."成功!";這句去掉,放到迴圈後的顯示,因為該語句將中止程式執行。例:

  Code:  
  $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 .= ''''.$title.''
'';
      }
    
     $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 ("產生分頁檔案完成,如產生不完全,請檢查檔案許可權系統後重建!");
?>  
  大致思路如此,其中如其它資料產生,資料輸入輸出檢查,分頁內容指向等可酌情在頁面中加入。
  在實際文章系統處理過程當中,還有許多問題有待考慮,與動態網頁面不同之處,需注意的地方還有很多。但大致思路即是如此,其它方面可舉一反三而得。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.