About_Smarty
Smarty是一个使用PHP写出来的模板PHP模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。
首先:要导入文件
<?php
include_once('smarty/Smarty.class.php'); $smarty = new Smarty; $smarty->template_dir = './templates/';
$smarty->compile_dir = './templates_c/';
$smarty->config_dir = './configs/';
$smarty->cache_dir = './cache/'; $smarty->left_delimiter = "{"; $smarty->right_delimiter = "}"; ?>
然后:链接数据库
<?php //数据库执行的魔术函数
function mysql_magic(){ //首先我们参数个数是未知的,但是可以通过PHP函数func_get_args
//得到所有的传入参数,返回值是一个数组
//这个函数还有一个相对的方法func_num_args,这个方法是获取传入参数的个数
$argsNum = func_get_args();
$args = func_get_args(); if($argsNum == 0){
echo "没有传入参数,直接返回false";
return false;
}
//array_shift数组方法,会把原数组的第一个位置的元素,移出数组,数组剩下后面的部分,并且将移出的元素返回
$sql = array_shift($args);
//为了不混淆$args,将args的值复制给另外的数组
$values = $args; //使用字符串替换函数str_replace
$sql = str_replace("?","'%s'",$sql); //使用vsprintf,将$sql字符串格式化
$sql = vsprintf($sql,$values);
//数据库执行语句已经拼好了,接下来就是要判断到底是增删改查的哪个语句
//根据判断执行不同的操作 //取得数据库语句第一个单词
// $s = explode(" ",$sql)[0];
$s = substr($sql,0,6); $conn = mysql_connect("localhost","root","") or die("数据连接异常".mysql_error());
mysql_select_db("bbs",$conn) or die(mysql_error());
mysql_query("set names 'utf8'"); $result = mysql_query($sql) or die(mysql_error()); if(strcasecmp($s,"insert") == 0){
return mysql_insert_id(); //>0->true表示插入成功,返回最新id,=0表示插入失败,没有获取最新插入的id
}
else if(strcasecmp($s,"update") == 0 || strcasecmp($s,"delete") == 0){
return mysql_affected_rows(); //返回几行受影响,>0表示删除或者更新成功,至少都有1行受影响,=0失败,没有行数受影响
}
else{
$arr = array();
while($row=mysql_fetch_array($result)){
$arr[] = $row;
}
return $arr;
} } ?>
最后:测试
<?php
include_once("smarty_inc.php");
include("mysql_func.php");
$sql = "select * from topic where parentid=?";
$topic = mysql_magic($sql,"0");
$arr = array(
array("我是标题1","我是内容1","我是作者1"),
array("我是标题2","我是内容2","我是作者2"),
array("我是标题3","我是内容3","我是作者3"),
array("我是标题4","我是内容4","我是作者4"),
);
$smarty->assign("name","macio jackson is a good black man");
$smarty->assign("age","19");
$smarty->assign("address","成都");
$smarty->assign("arr",$arr);
$smarty->assign("topic",$topic);
$smarty->display("index.html");
?>
About_Smarty的更多相关文章
随机推荐
- javaee 导航
tomcate端口设定和服务器虚拟目录设定 静态web 应用和动态web应用 tomcat相关问题 web应用 http 响应 url uri 动态页面 servlet 一个简单的servlet的de ...
- Linux posix线程库总结
由于历史原因,2.5.x以前的linux对pthreads没有提供内核级的支持,所以在linux上的pthreads实现只能采用n:1的方式,也称为库实现. 线程的实现,经历了如下发展阶段: Linu ...
- c#_图表之zeGraph
关于图表控件: http://blog.csdn.net/sgear/article/details/1449025 其中zedGraph的基本方法及属性介绍: http://wenku.baidu. ...
- html表单提交方式
xml 表单提交方式: * 使用submit提交 <form> 要提交的数据 <input type="submit" /'> </form> ...
- WinForm中重绘TabControl选项卡标题
最近开发WinForm频繁使用了TabControl控件,这个控件的选项卡没有BackgroundImage这个属性,那么如何为其各个选项卡添加背景图片呢?(这里说的是每个TabPage的头部,也就是 ...
- wifi的country code
转自:http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_3166.htmlCountry A 2 A 3 Number ------------- ...
- electrica writeup
关于 caesum.com 网上上的题目,分类有Sokoban,Ciphers,Maths,Executables,Programming,Steganography,Misc.题目有点难度,在努力奋 ...
- UVA 10692 Huge Mods(指数循环节)
指数循环节,由于a ^x = a ^(x % m + phi(m)) (mod m)仅在x >= phi(m)时成立,故应注意要判断 //by:Gavin http://www.cnblogs. ...
- abstract与interface的区别
abstract的用法: //通过abstract 关键字修饰的类叫抽象类. abstract class Animal { String name; String color; abstract p ...
- Linux学习笔记(12)-进程间通信|匿名管道
Linux的进程间通信有几种方式,包括,管道,信号,信号灯,共享内存,消息队列和套接字等-- 现在一个个的开始学习! ----------------------------------------- ...