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的更多相关文章

随机推荐

  1. Django缓存系统设置

    参考: http://lesliezhu.github.io/public/2016/04/19/django-cache.html http://www.opscoder.info/django_c ...

  2. tp5 中 model 的获取器

    在获取数据的字段值后自动进行处理 // 模型中写入如下代码,则查询结果会自动将status的结果进行转换 class User extends Model { public function getS ...

  3. Sublime Text 2 设置tab空格

    打开Sublime Text 2 英文版:选择Preference-defalut 中文版:选择Preference-键绑定-默认 找到"translate_tabs_to_spaces&q ...

  4. 关于PowerDesigner出现不允许有扩展属性,或对象不存在的解决办法(SQLSERVER2008下亲测可用)

    在Generate Database弹出框,选择options选项卡,右边的勾选去除所有Comment的勾选(目测是comment注释生成的语句有问题),应该也可以更改生成语句,暂时这样解决

  5. 【Java并发系列01】Thread及ThreadGroup杂谈

    img { border: solid black 1px } 一.前言 最近开始学习Java并发编程,把学习过程记录下.估计不是那么系统,主要应该是Java API的介绍(不涉及最基础的概念介绍), ...

  6. 阿里云服务器上开启linux远程桌面连接

    一.说明: 本文的目的是实现在windows机器上利用远程桌面连接来访问远程的linux桌面. 这里使用的是阿里云服务器,操作系统为Centos6.5. 二.基本步骤: 1.首先保证服务器已经安装完毕 ...

  7. Android 笔记 AutoCompleteTextView day8

    用于自动补全内容 适应器可用于显示多行内容 package com.supermario.autocompletedemo; import android.app.Activity; import a ...

  8. Flash: An Efficient and Portable Web Server

    Introduction This paper presents the design of a new Web server architecture called the asymmetric m ...

  9. java中的小数的取整的几种函数

    Math类中提供了5个与取整相关的函数,如下所示: static double ceil(double a):天花板函数,返回大于等于a的最小整数(但是以浮点数形式存储). static double ...

  10. 题目:求1+2+…+n,

    题目:求1+2+-+n, 要求不能使用乘除法.for.while.if.else.switch.case等关键字 以及条件判断语句(A?B:C). java 实现 public class sum { ...