设为首页 收藏本站
查看: 345|回复: 0

[经验分享] php生成静态页面的简单实例

[复制链接]

尚未签到

发表于 2017-3-31 06:40:08 | 显示全部楼层 |阅读模式
  一个简单的实例:
新闻模版文件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;
  ?>

查看(48) 评论(0) 收藏 推荐
www.happyjiaoyour.com域名出售
2007-11-20 11:21:18
www.happyjiaoyour.com域名出售,如有意向,请加qq:371606962
查看(12) 评论(1) 收藏 推荐
第一个smarty例子--分页显示数据
2007-11-18 14:18:21
查看PHP-smarty安装

模板页index.tpl:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
<title>查看留言</title>
<styletype="text/css">
<!--
.STYLE1{color:#FF0000}
-->
</style>
</head>
<body>
<{*assignvar="login"value=0*}>
<divalign="center">
<p><ahref="index.php">主页</a>  <ahref="addmeg.php">留言</a> 
<{if$login==0}> <ahref="gli.php">管理</a><{/if}>  <{*login=0未登陆,显示管理链接*}>
</p>
</div>
<{sectionname=lploop=$ly}>
<tablewidth="590"border="1"align="center"cellpadding="0"cellspacing="1">
<tr>
<tdwidth="85"rowspan="2"><p>留言人:<br/>
<{$ly[lp].fbr}></p>
</td>
<tdwidth="427"height="23"><p> 标题:<{$ly[lp].tm}></p>
</td>
</tr>
<tr>
<tdheight="58"> 内容:<{$ly[lp].nr}></td>
</tr>
</table><br/>
<{/section}>
<palign="center">总<spanclass="STYLE1"><{$pcunt}></span>页 当前为第<spanclass="STYLE1"><{$page}></span>页 <ahref="index.php">首页</a> <{$qian}><{$next}><ahref="index.php?page=<{$pcunt}>">最后一页</a></p>
</body>
</html>

//////////////////////////////////////////////////////////////////////////////

index.php:

<?php
/*********************************************
*
*文件名:index.php
*作用:显示留言分页
*作者:龙的心
*QQ:282129207
*
*********************************************/

require("./class/Smarty.class.php");//包含smarty类文件
$smarty=newSmarty();//建立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("setnames'gb2312'");
$page=$_GET['page'];
if($page==null)
$page=1;
$psize=4;//每页记录数
$str="select*fromly";
$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*fromlylimit$start,$psize";
$query=mysql_query($str);
while($arr=mysql_fetch_array($query))
{//print_r($arr);
$array[]=$arr;
}
if($page>1)$str1="<ahref=index.php?page=$qianpage>上一页</a> ";
if($page<$pcunt)$str2="<ahref=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");
?>



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-357812-1-1.html 上篇帖子: 如何使用phpdoc生成PHP文档 下篇帖子: {dede:php}代码失效原因
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表