zoukankan      html  css  js  c++  java
  • php生成静态html分页实现方法

    <?php
    $conn=mysql_connect('localhost','root','wy')
    or die('连接失败:'.mysql_error());


    //选择数据表
    if (mysql_select_db('mynews',$conn))
    {
    echo'选择数据库成功!'.'<p>';

    }
    else
    {
    echo'数据库选择失败!'.mysql_error().'<p>';
    }
    @header("Content-Type: text/html; charset=utf-8");
    mysql_query("SET NAMES 'utf8'");

    $fp = fopen ("temp.html","r");
    $content = fread ($fp,filesize ("temp.html"));
    $onepage =2;
    $sql = "select id from news";
    $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 * from news limit $start,$onepage";
    $result=mysql_query($sql_for_page);
    while($row=mysql_fetch_array($result))
    {
            $list .= 'uid='.$row['id'].$row['title'].'<br>';
         }
    $content1 = str_replace ("{ articletable }",$list.$i,$content);
    //分页
    $list1 = '';
    for ($j = 0;$j<$allpages; $j++){
    if ($j == 0){
    $list1 .= '<a href="index.html" >第'.$j.'页 </a>|';
    } else {
    $list1 .= "<a href='index_".$j.".html' >第".$j."页 </a>|";
    }
    }
    $content2 = str_replace ("{ mune }",$list1,$content1);

    if (is_file ($indexpath)){
    @unlink ($indexpath); //若文件已存在,则删除
    }
    $handle = fopen ($indexpath,"w"); //打开文件指针,创建文件
    /*
      检查文件是否被创建且可写
    */
    if (!is_writable ($indexpath)){
    echo "文件:".$indexpath."不可写,请检查其属性后重试!"; //修改为echo
    }
    if (!fwrite ($handle,$content2)){ //将信息写入文件
    echo "生成文件".$indexpath."失败!"; //修改为echo
    }
    fclose ($handle); //关闭指针
    }
    fclose ($fp);
    die ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!");
    ?>

    temp.html

    <HTML>
    <TITLE>{ title }</TITLE>
    <BODY>
    this is a { file } file's templets
    { articletable }
    分页         { mune }
    </BODY>
    </HTML>

  • 相关阅读:
    The XOR Largest Pair
    似乎在梦中见过的样子 (KMP)
    Censoring(栈+KMP)
    KMP解决最小循环节问题
    收集雪花 (贪心+双指针+离散化)
    「POI2010」反对称 Antisymmetry (manacher算法)
    A Horrible Poem (字符串hash+数论)
    leetcode103——二叉树的锯齿形层次遍历
    leetcode102 ——二叉树的层序遍历
    二叉树——100 相同的树(easy)
  • 原文地址:https://www.cnblogs.com/kuyuecs/p/1279357.html
Copyright © 2011-2022 走看看