php生成静态页面的简单实例

来源:互联网 发布:苹果手机数据备份软件 编辑:程序博客网 时间:2024/04/30 04:27

一个简单的实例:
新闻模版文件news_tmp.html:
<html>
<head>
<title>{title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<TABLE border=0 width=767 cellspacing="0" cellpadding="5">
  <TR>
 <TD>
      <div align="center">{news_title}</div>
    </TD>
</TR>
<TR>
 <TD>
      <div align="center">{news_image}</div>
    </TD>

</TR>
<TR>
 <TD>{news_content}</TD>
</TR>
</TABLE>
</body>
</html>
______________________________________________________________________________________________
答7:
新闻生成文件aaa.php:
<?
//假设下面信息都来自表单
$title="娱乐新闻---郑秀文准备宣布退出娱乐圈";
$news_title="郑秀文准备宣布退出娱乐圈";
$image_path=array("image/xxx.jpg","image/xxx2.jpg","image/xxx3.jpg");
$news_content="事业如日方中的郑秀文(Sammi),除是乐坛天后外,亦以五百五十万片酬登上全港片酬最高女星之位......";
$fp=@fopen("news_tmp.html","r") or die("没有模版文件或者没有相关权限!");

$str=fread($fp,filesize("news_tmp.html"));
fclose($fp);


______________________________________________________________________________________________
答8:
$news_filename=time().".html"; //生成的新闻文件名
$str=str_replace("{title}",$title,$str);
$str=str_replace("{news_title}",$news_title,$str);
$str=str_replace("{news_content}",$news_content,$str);
if(count($image_path)){
 for($n=0;$n<count($image_path);$n++)
  $news_image.="<img src=".$image_path[$n]."><BR>";
 $str=str_replace("{news_image}",$news_image,$str);
}else
 $str=str_replace("{news_image}","",$str);
$fw=fopen($news_filename,"w");
fwrite($fw,$str);
?>

 

 

 

简单演示PHP如何使用模板生成静态页面。

模板文件templets.htm:

<html>
  <head>
    <title>{title}</title>
  </head>
  <body>
    <p>Hello {hello}</p>
  </body>
</html>

 PHP文件代码:

<?php

  $title = 'webjx';
  $hello = 'webjxcom!';
  $file = file_get_contents('templets.htm');
  $file = str_replace(array('{title}','{hello}'),array($title,$hello), $file);
  echo $file;

?>

  • www.happyjiaoyour.com域名出售

    2007-11-20 11:21:18

    www.happyjiaoyour.com域名出售,如有意向,请加qq:371606962
  • 第一个smarty例子--分页显示数据

    2007-11-18 14:18:21

    查看PHP-smarty安装 
     
    模板页index.tpl:
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>查看留言</title>
    <style type="text/css">
    <!--
    .STYLE1 {color: #FF0000}
    -->
    </style>
    </head>
    <body>
    <{*assign var="login" value=0*}> 
    <div align="center">
      <p><a href="index.php">主页</a>  <a href="addmeg.php">留言</a> 
      <{if $login==0}> <a href="gli.php">管理</a><{/if}>  <{*login=0未登陆,显示管理链接*}>
      </p>
    </div>
     <{section name=lp loop=$ly}> 
    <table width="590" border="1" align="center" cellpadding="0" cellspacing="1">
      <tr>
        <td width="85" rowspan="2"><p>留言人:<br />
    <{$ly[lp].fbr}></p>
        </td>
        <td width="427" height="23"><p>  标题:<{$ly[lp].tm}></p>
        </td>
      </tr>
      <tr>
        <td height="58"> 内容:<{$ly[lp].nr}></td>
      </tr>
    </table><br />
    <{/section}>
    <p align="center">总<span class="STYLE1"><{$pcunt}></span>页 当前为第<span class="STYLE1"><{$page}></span>页 <a href="index.php">首页</a> <{$qian}><{$next}><a href="index.php?page=<{$pcunt}>">最后一页</a></p>
    </body>
    </html>
     
    //////////////////////////////////////////////////////////////////////////////
     
    index.php:
     
    <?php 
       /********************************************* 
       * 
       * 文件名:index.php
       * 作 用: 显示留言分页
       * 作 者: 龙的心
       *  Q Q:282129207 
       * 
       *********************************************/ 
      
      require("./class/Smarty.class.php"); //包含smarty类文件
      $smarty = new Smarty();  //建立smarty实例对象$smarty
      $smarty->template_dir = './templates/';
      $smarty->compile_dir  = './templates_c/';
      $smarty->config_dir   = './configs/';
      $smarty->cache_dir    = './cache/';
      $smarty->caching    = false;  //这里是调试时设为false,发布时请使用true 
     $smarty->left_delimiter = "<{"; 
      $smarty->right_delimiter = "}>";
      
      
    mysql_connect('localhost','root','root');
    mysql_select_db('nihao');
    mysql_query("set names 'gb2312'");
    $page=$_GET['page'];
    if($page==null)
    $page=1;
    $psize=4; //每页记录数
    $str="select *from ly";
    $query=mysql_query($str);
     $num=@mysql_num_rows($query);//总记录数
     $pcunt=ceil($num/$psize);//总页数
     $nextpage = $page+1;
     $qianpage= $page-1;
     $start=($page-1)*$psize;
     
     $str="select *from ly limit $start,$psize";
    $query=mysql_query($str);
    while($arr=mysql_fetch_array($query))
    {//print_r($arr);
    $array[]=$arr; 
    }
    if($page>1) $str1="<a href=index.php?page=$qianpage>上一页</a> ";
    if($page<$pcunt) $str2="<a href=index.php?page=$nextpage>下一页</a> ";
    $smarty->assign("login", "0"); //login=0未登陆,显示管理链接
    $smarty->assign("ly", $array); 
    $smarty->assign("page", $page);
    $smarty->assign("qian", $str1); 
    $smarty->assign("next", $str2); 
    $smarty->assign("pcunt", $pcunt); 
    $smarty->display("index.tpl"); 
    ?> 
     
  • 原创粉丝点击