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的更多相关文章
随机推荐
- php实验5数组
1.自定义两个数组,分别为索引数组和关联数组,每个数组必须至少有4个元素,使用print_r( )函数输出数组元素. 2.编写一个随机抽奖程序,示例运行结果如下: 3.定义一个三维数组$categor ...
- 简单的ADO.NET连接数据小样例
ADO.NET连接数据库的样例如下: using System; using System.Collections.Generic; using System.ComponentModel; usin ...
- SQL SERVER中如何在声明游标的语句中,用变量做表名
-- 因为定义游标所用的表名是变量,所以采用EXEC(定义语句) 的方式来声明游标set @StrSql='DECLARE Ba_Cursor CURSOR FOR (SELECT a.PhoneId ...
- C#之反射
Assembly assembly = Assembly.Load("PeopleDal"); //获取程序集名称 Module[] modules = assembly.GetM ...
- 【转】代码中特殊的注释技术——TODO、FIXME和XXX的用处
(转自:http://blog.csdn.net/reille/article/details/7161942) 作者:reille 本博客网址:http://blog.csdn.net/reille ...
- python之模块
模块即一推代码的集合来实现某个功能,使用时直接调用,甚是方便. 模块又分为三种 自定义模块 内置模块 第三方模块 下面就来介绍介绍什么是内置模块及如何去使用它和内置模块的好处. 使用模块模块前首先导入 ...
- kettle系列-6.kettle实现多字段字典快速翻译
在数据清洗转换中,常见的字典翻译,如性别在原表中是1(男).2(女)等,类似还有很多较大的字典需要翻译,若同一个表中有很多个字典需要翻译,采用[数据库查询]方式翻译的话效率就会相当低下. 这里采用ja ...
- [转]IIS6.0迁移至IIS7.0
原文地址:http://www.splaybow.com/post/iis-6.0-7.0.html 公司的项目需要迁移到IIS7的目标机器中 在此做记录 原来server 2003系统 迁到2008 ...
- git学习(四):撤销修改和撤销删除
修改有两种情况 在工作区修改但没有add到暂存区 git checkout -- <file> 在工作区修改了也add到暂存区 git reset HEAD <file> 先撤 ...
- [leetcode] 小心成环
156. Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes wit ...