代码如下:
yao.php
<?php //(1)创建PDO对象 $dsn="mysql:host=localhost;port=3306;dbname=itcast;charset=utf8"; $username="root"; $password=""; $pdo=new PDO($dsn,$username,$password); //(2)执行查询的SQL语句,并返回结果集对象 $sql="SELECT * FROM student"; $PDOStatement=$pdo->query($sql); //(3)获取多行数据 $arrs=$PDOStatement->fetchAll(PDO::FETCH_ASSOC); ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>学生信息管理中心</title> </head> <body> <h2 align="center">学生信息管理者中心</h2> <table width="600" border="1" align="center" rules="all" cellpadding="5"> <tr bgcolor="#f0f0f0"> <th>编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> </tr> <?php foreach($arrs as $arr){?> <tr align="center"> <td><?php echo $arr['id']?></td> <td><?php echo $arr['name']?></td> <td><?php echo $arr['sex']?></td> <td><?php echo $arr['age']?></td> </tr> <?php }?> </table> </body> </html>:将学生管理文件的代码分成两个文件:
控制器文件:controller.php——纯PHP代码,没有前端代码;视图文件:view.html——前端代码,主要包含HTML、CSS、JS;先分离,再合并,运行时,只需要运行控制器文件。
代码如下: controller.php
<?php //(1)创建PDO对象 $dsn="mysql:host=localhost;port=3306;dbname=itcast;charset=utf8"; $username="root"; $password=""; $pdo=new PDO($dsn,$username,$password); //(2)执行查询的SQL语句,并返回结果集对象 $sql="SELECT * FROM student"; $PDOStatement=$pdo->query($sql); //(3)获取多行数据 $arrs=$PDOStatement->fetchAll(PDO::FETCH_ASSOC); //(4)包含视图文件:文件扩展名是.html结尾的 include "view.html";view.html
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>学生信息管理中心</title> </head> <body> <h2 align="center">学生信息管理者中心</h2> <table width="600" border="1" align="center" rules="all" cellpadding="5"> <tr bgcolor="#f0f0f0"> <th>编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> </tr> <?php foreach($arrs as $arr){?> <tr align="center"> <td><?php echo $arr['id']?></td> <td><?php echo $arr['name']?></td> <td><?php echo $arr['sex']?></td> <td><?php echo $arr['age']?></td> </tr> <?php }?> </table> </body> </html>html代码和PHP代码分离,其实就是前端人员和程序员分离。
前端人员喜欢HTML形式代码:{$name}php只能解释的代码:<?php echo $name?>查找替换->将{$name}转换为<?php echo $name?>Smarty就是一个类文件,是有PHP写的类文件。
下载网址:https://www.smarty.net/download
文件结构: view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> 姓名:{$name}<br> 年龄:{$age}<br> </body> </html>controller.php
<?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)向视图文件赋值 $smarty->assign("name","小明"); $smarty->assign("age","20"); //(4)显示视图文件 $smarty->display("./view.html");左定界符:$smarty->left_delimiter,默认="{" 右定界符:$smarty->right_delimiter,默认="}"
代码例子: view.html
<!DOCTYPE HTML> <html> <head> <title></title> <style type="text/css"> body{ background-color:#99ff00; } </style> </head> <body> 姓名:<{$name}><br> 年龄:<{$age}><br> </body> </html>controller.php
<?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)Smarty配置 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; //(4)向视图文件赋值 $smarty->assign("name","小明"); $smarty->assign("age","20"); //(5)显示视图文件 $smarty->display("./view.html");设置视图文件目录:$smarty->setTemplateDir(新目录路径) 读取视图文件的目录:$smaty->getTemplateDir()
例子: 目录结构: view.html 代码同上 controller.php
<?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)Smarty配置 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; $smarty->setTemplateDir("./App/Home/View/"); print_r($smarty->getTemplateDir()); //(4)向视图文件赋值 $smarty->assign("name","小明"); $smarty->assign("age","20"); //(5)显示视图文件 $smarty->display("./view.html");编译目录的设置:$smarty->setCompileDir("") 编译目录的读取:$smarty->getCompileDir() 配置目录的设置:$smarty->setConfigDir("") 配置目录的读取:$smarty->getConfigDir()
所有类型的PHP变量都可以传递到视图文件来使用、
但是,在视图中,对象和资源变量不常用。例子代码: view.html
<!DOCTYPE HTML> <html> <head> <title></title> <style type="text/css"> body{ background-color:#99ff00; } </style> </head> <body> 姓名:<{$name}><br> 年龄:<{$age}><br> 婚否:<{$isMarried}><br> 手机1:<{$tel[0]}><br> 手机2:<{$tel.1}><br> </body> </html>controller.php
<?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)Smarty配置 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; //(4)向视图文件赋值 $smarty->assign("name","小明"); $smarty->assign("age",20); $smarty->assign("isMarried",true); $smarty->assign("tel",array('152********','187********')); //(5)显示视图文件 $smarty->display("./view.html");所有的超全局数组变量,可以在视图文件中直接使用。
//Smarty 获取超全局数组变量 {$smarty.get.参数} {$smarty.post.参数} {$smarty.request.参数} {$smarty.server.参数} {$smarty.session.参数} {$smarty.cookie.参数} {$smarty.files.参数}例子代码: controller.php
<?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)Smarty配置 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; //(4)显示视图文件 $smarty->display("./view.html");view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> 用户名:<{$smarty.get.username}><br> 密码:<{$smarty.get.password}><br> 域名:<{$smarty.server.SERVER_NAME}><br> 客户端IP地址:<{$smarty.server.REMOTE_ADDR}> </body> </html>URL:http://localhost/test/Smarty/controller.php?username=liming&password=123 运行结果:
例子代码: controller.php
<?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)Smarty配置 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; //(4)自定义常量 const DB_HOST="localhost"; define("DB_USER","root"); //(5)显示视图文件 $smarty->display("./view.html");view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> PHP最大整数:<{$smarty.const.PHP_INT_MAX}><br> 主机名:<{$smarty.const.DB_HOST}><br> 用户名:<{$smarty.const.DB_USER}> </body> </html>例子代码: view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> 当前时间戳:<{time()}><br> 当前时间戳:<{$smarty.now}><br> 格式化时间戳:<{$smarty.now|datE_format:'%Y-%m-%d %H:%M:%S'}> </body> </html>例子代码:
例子代码: view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> <!--加载配置文件--> <{config_load file="myConfig.conf"}> <!--读取配置文件参数--> <{#a#}><br> <{$smarty.config.b}><br> <{$smarty.config.c}> </body> </html>controller.php
<?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)Smarty配置 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; $smarty->setConfigDir("./App/Conf"); //(4)显示视图文件 $smarty->display("./view.html");使用中括号,对配置文件进行分组。
代码例子: controller.php同上
view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> <!--加载配置文件--> <{config_load file="myConfig.conf section=$smarty.get.lan}> <!--读取配置文件参数--> <{#a#}><br> <{$smarty.config.b}><br> <{$smarty.config.c}> </body> </html>view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> <{foreach $arr as $key=>$value}> $arr[<{$key}>]=<{$value}><br> <{/foreach}> </body> </html> foreach的常用属性应用 @key:输出当前值得索引,可能是整型索引,也可能是字符索引;@index:当前数组索引,从0开始计算;@iteration:当前循环的次数,从1开始计算;@first:当首次循环时,值为true;@total:是整个循环的次数,可以在foreach内部或外部使用;例子代码: controller.php
<?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)Smarty配置 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; //(4)向数组赋一维数组 $arr=array( 'db_host'=>'localhost', 'db_user'=>'root', 'db_pass'=>'root' ); $smarty->assign("arr",$arr); //(5)显示视图文件 $smarty->display("./view.html");view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> <table border='1'> <tr> <th>元素的值</th> <th>数组下标</th> <th>从0开始计数</th> <th>从1开始计数</th> <th>是否第一次循环</th> <th>是否最后一次循环</th> <th>循环总次数</th> </tr> <{foreach $arr as $value}> <tr> <td><{$value}></td> <td><{$value@key}></td> <td><{$value@index}></td> <td><{$value@iteration}></td> <td><{$value@first}></td> <td><{$value@last}></td> <td><{$value@total}></td> </tr> <{/foreach}> </table> </body> </html>语法说明: (1)name:每次循环的索引,相对于$i (2)loop:指定循环的数组变量 (3)start:指定循环的初始值,默认为0,从第1个元素开始循环 (4)step:指定每次循环的步长值,默认为1 (5)max:指定最大循环次数
name和loop属性是必须的。 实例:输出一维数组 controller.php <?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)Smarty配置 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; //(4)向数组赋一维枚举数组 $arr=array('小明',24,'男'); $smarty->assign("arr",$arr); //(5)显示视图文件 $smarty->display("./view.html");view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> <!--section遍历一维枚举数组--> <{section name=i loop=$arr}> <{$arr[i]}><br> <{/section}> </body> </html> 实例:控制补偿、起始点、循环次数 controller.php <?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)Smarty配置 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; //(4)向数组赋一维枚举数组 $arr=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24); $smarty->assign("arr",$arr); //(5)显示视图文件 $smarty->display("./view.html");view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> <!--section遍历一维枚举数组--> <{section name=i loop=$arr start=4 step=3 max=5}> <{$arr[i]}><br> <{/section}> </body> </html>运行结果:
Smarty中的if与PHP中的if很像; PHP中的运算符在Smarty中都可以使用。
语法结构 //(1)只判断true情况 {if 条件} 代码 {/if} //(2)既判断true,也判断false {if 条件} 代码 {else} 代码 {/if} //(3)多条件判断 {if 条件1} 代码 {elseif 条件2} 代码 {else} 代码 {/if} if中的运算符 实例:if的简单应用 controller.php <?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)Smarty配置 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; //(4)显示视图文件 $smarty->display("./view.html");view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> <!--根据输入的年龄判断是否可以当兵--> <{if isset($smarty.get.age)}> <{if $smarty.get.age>=18 and $smarty.get.age<=25}> 你可以当兵! <{else}> 你不能当兵! <{/if}> <{else}> 请在地址给age参数赋值 <{/if}> </body> </html> 实例:表格隔行变色 controller.php <?php header("content-type:text/html;charset=utf-8"); //(1)包含Smarty类文件 require_once("./smarty-master/libs/Smarty.class.php"); //(2)创建Smarty类对象 $smarty=new Smarty(); //(3)Smarty配置 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; //(4)向视图赋一维数组 $arr=array(1,5,2,1,4,5,7,8,9,6,3,3,2,1,4,7,8); $smarty->assign("arr",$arr); //(5)显示视图文件 $smarty->display("./view.html");view.html
<!DOCTYPE HTML> <html> <head> <title></title> </head> <body> <table width='400' border='1'> <{foreach from=$arr key='key' item='value'}> <{if $value@iteration is div by 2}> <tr bgcolor="#99ff00"> <{else}> <tr> <{/if}> <td> </td> <td> </td> <td> </td> </tr> <{/foreach}> </table> </body> </html>变量调节器就是变量进行格式的函数,对变量进行格式化输出。
语法结构 //变量调节器的语法 {$var|调节器1:参数1:参数2:参数N|调节器2|调节器3|...}语法说明: (1)$var就是要修饰的变量 (2)“|”分割各个调节器函数 (3)“:”分割调节器函数的各个参数
Smarty中常用的变量调节器 upper:转成全大写,对应PHP的strtoupper() lower:转成全小写,对应PHP的strtolower() nl2br:将\n换行符转成<br>,对应PHP的nl2br() replace:查找替换,对应于PHP的str_replace() fata_format:时间戳格式化,对应于PHP的date() truncate:截取字符串,对应于PHP的substr()或mb_substr()