smarty学习-留言本实例

 

最近学习了一下smarty,打算写一个简单的smarty应用来练练手!

就拿留言本来联系吧!

下面是我在本机的环境:

Window XP    Apache2.2/mysql5/php5.2.1

D:/php/www 为我的网站根目录。

在该目录下我新建了2个目录

一个是smarty用来存放smarty相关的库文件,一个是guestbook存放留言本的文件。

首先把下载下来的smarty库中的libs中的文件复制进smarty目录,

进入guestbook目录,新建5个目录依次为:cache,configs,templates,templates_c,inc 这几个目录的作用我就不说了,网上有很多介绍。

inc目录是我用来存放用到的相关配置文件。

好了,大概的环境就介绍完了。

由于每个用到smarty的文件都需要调用smarty的类,所以我在这里写了一个文件smarty.inc.php,专门用来处理smarty的初始化操作。我把它保存在inc目录下,相关代代码如下

<?php
 define('SMARTY_DIR','D:/php/www/smarty/');
 define('GUESTBOOK','D:/php/www/guestbook/');
 require_once(SMARTY_DIR."Smarty.class.php");

 $tpl = new Smarty();
 
 $tpl->template_dir  = GUESTBOOK.'templates/';
 $tpl->compile_dir  = GUESTBOOK.'templates_c/';
 $tpl->config_dir   = GUESTBOOK.'configs/';
 $tpl->cache_dir   = GUESTBOOK.'cache/';
 $tpl->left_delimiter = '<{';
 $tpl->right_delimiter = '}>';

?>

新建conn.inc.php保存在inc目录,这里定义了一些常量。

<?php

 /*
  * 定义数据库链接
 */
 define("DB_HOST","localhost");
 define("DB_USER","root");
 define("DB_PASS","liuqun");
 define("DB_DATABASE","guestbook");
 
 /*
  * 定义页面
 */
 
 $title = "AJAX/smarty 留言板实例";
?>

新建db.inc.php,数据库操作类,暂时只用到这些,以后再加其他功能!

<?php
 class db{
   var $conn;
      
   public function __construct(){
    $this->conn = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die('error'); 
    
    mysql_select_db(DB_DATABASE);
    
    return $this->conn;
    
   }
   
   public function query($sql){
     $result = mysql_query($sql);
     return $result;
   }
   

下面是首页的代码:显示了所有的留言信息

<?php
 require_once('./inc/smarty.inc.php');
 require_once('./inc/conn.inc.php');
 require_once('./inc/db.inc.php');
 
 $db = new db();
 
 $sql = "select * from guestbook order by gb_id desc";
 $result = $db->query($sql);
 while($rs = $db->fetch($result)){
  $guestData[] = array("uname"=>$rs['gb_name'],
             "email"=>$rs['gb_email'],
             "time"=>$rs['gb_time'],
             "content"=>$rs['gb_content']); 
 }
 
 $tpl->assign('title',$title);
 $tpl->assign('content',$guestData);
 $tpl->display('index.htm');
?>

这里所做的很简单,只是把所有的留言取出来并放入一个数组中

下面是index.htm页面代码:

<html>
<head>
<title><{$title}></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<scrīpt type="text/javascrīpt" src="./JS/check.js"></scrīpt>
<style type="text/css">
<!--
body{
 font-family: Verdana, Arial, Helvetica, sans-serif;
 color: #000;
 font-size: 12px;
 line-height: 18px;
 margin-left: 0px;
 margin-top: 0px;
 margin-right: 0px;
 margin-bottom: 0px;
}
a:link{
 
 line-height: 18px;
 color: #000000;
 text-decoration: underline;
}
a:visited{
 color: #000000;
}

a:hover{
 color: #FF0000;
 text-decoration: none;
}
.table{
 margin-top:15px;
}
.borderb{
 border: 1px dashed #999999;
}
.da{
 font-size:14px;color:#000000;
}
.red{
 font-size:14px;color:#FF0000;
}

.hui{
 font-size:12px;
 color:#999999;
 line-height: 20px;
}
.hui a:link{
 
 line-height: 18px;
 color: #999;
 text-decoration: underline;
}
.hui a:visited{
 color: #999;
}

.hui a:hover{
 color: #FF0000;
 text-decoration: none;
}
h3 {
 font-size:16px;
 font-weight:bold;
}


-->
</style>
</head>
<body>
<table width="760" border="0" align="center" bgcolor="#DDDDDD" style="margin-top:15px;">
  <tr>
    <td height="27" align="center" class="borderb">留言本</td>
  </tr>
</table>
<table width="760" border="0" align="center" bgcolor="#DDDDDD" style="margin-top:15px;">
  <tr>
    <td height="27" align="right" class="borderb"><a href="#sub" class="hui">新增留言</a>    </td>
  </tr>
</table>

<{section name=guestbook loop=$content}>

<table width="760" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC"  style="margin-top:15px;">
  <tr align="center" bgcolor="#FFFFFF">
    <td width="10%" height="27" align="right" class="da">用户名:</td>
    <td width="20%" height="27" class="hui"><{$content[guestbook].uname}></td>
    <td width="5%" height="27" align="right" class="da">EMAIL:</td>
    <td width="20%" height="27" class="hui"><{$content[guestbook].email}></td>
    <td width="10%" height="27" align="right" class="da">留言时间:</td>
    <td width="20%" height="27" class="hui"><{$content[guestbook].time}></td>
  </tr>
  <tr bgcolor="#FFFFFF">
    <td width="10%" height="27" align="right" class="da">留言内容:</td>
    <td height="27" colspan="5" class="hui" style="word-break:break-all"><{$content[guestbook].content}></td>
  </tr>
</table>
<{/section}>
<table width="760" border="0" align="center" bgcolor="#DDDDDD" style="margin-top:15px;">
  <tr>
    <td height="27" align="right" class="borderb"><a href="#" class="hui">新增留言</a>    </td>
  </tr>
</table>
<div id="loading"></div>
<table width="760" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC" class="borderb"  style="margin-top:65px;">
  <form id="formData" action="add_message.php" name="formData" method="POST" ōnSubmit=" return checkdata()">
    <tr bgcolor="#FFFFFF">
      <td height="27" align="right" class="da">(<span class="hui">必填</span>)用户名:</td>
      <td height="27"> <input name="uname" type="text" id="uname" size="30" /></td>
      <td width="30%">
<div id="unameDiv"></div></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td height="27" align="right" class="da">(<span class="hui">必填</span>)EMAIL:</td>
      <td height="27"> <input name="email" type="text" id="email" size="30" /></td>
      <td><div id="emailDiv"></div></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td height="27" align="right" class="da">(<span class="hui">必填</span>)留言内容:</td>
      <td height="27"> <textarea name="content" cols="35" rows="8"></textarea></td>
      <td><div id="contentDiv"></div></td>
    </tr>
    <tr align="left" bgcolor="#FFFFFF">
      <td height="27" colspan="3" class="da">
                      
                
           
  <a name="sub"></a><input type="submit" name="Submit" value="提 交 留 言"></td>
    </tr>
  </form>
</table>
</body>
</html>

最主要的就是这里,使用smarty的section 循环显示出所有的数据。

<{section name=guestbook loop=$content}>

<table width="760" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC"  style="margin-top:15px;">
  <tr align="center" bgcolor="#FFFFFF">
    <td width="10%" height="27" align="right" class="da">用户名:</td>
    <td width="20%" height="27" class="hui"><{$content[guestbook].uname}></td>
    <td width="5%" height="27" align="right" class="da">EMAIL:</td>
    <td width="20%" height="27" class="hui"><{$content[guestbook].email}></td>
    <td width="10%" height="27" align="right" class="da">留言时间:</td>
    <td width="20%" height="27" class="hui"><{$content[guestbook].time}></td>
  </tr>
  <tr bgcolor="#FFFFFF">
    <td width="10%" height="27" align="right" class="da">留言内容:</td>
    <td height="27" colspan="5" class="hui" style="word-break:break-all"><{$content[guestbook].content}></td>
  </tr>
</table>
<{/section}>

add_message.php

<?php
 require_once('./inc/smarty.inc.php');
 require_once('./inc/conn.inc.php');
 require_once('./inc/function.inc.php');
 require_once('./inc/db.inc.php');
 
 $uname = trim($_POST['uname']);
 $email = trim($_POST['email']);
 $content = trim($_POST['content']);
 //$stime = date("Y-m-d H:i:s",time());
 
 if(!$uname){
  $uname = "游客";
 }
 $db = new db();
 
 $sql = "INSERT INTO `guestbook` (`gb_name` ,`gb_time` ,`gb_content` ,`gb_email` )VALUES ('$uname', NOW( ) , '$content', '$email')";
 $result = $db->query($sql);
 if(!$result){
  $add_value = "添加失败";
 }else{
  $add_value = "添加成功";
 }
 
 $tpl->assign('times',"3");
 $tpl->assign('reurl',"index.php");
 $tpl->assign('add_value',$add_value);
 $tpl->display('add_message.htm');
 ?>

add_message.htm

<!DOCTYPE HTMLPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<meta http-equiv="refresh" content="<{$times}>;URL=<{$reurl}>">
</head>

<body>
<table width="760" border="1" align="center" bordercolor="#CCCCCC">
  <tr>
    <td align="center"><{$add_value}>,3秒后自动返回</td>
  </tr>
</table>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值