Smarty模板:(前后端分离)
原理:核心是一个类,先修改配置文件,在使用的时候引入配置文件即可,(init.inc.php)
$smarty->assign("ceshi",$a);//注册变量到模板
$smarty->display("test.html");//前台显示
一、登录:login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<form action="loginchuli.php" method="post">
<div>用户名:<input type="text" name="uid" /></div>
<div>密码:<input type="password" name="pwd" /></div>
<div><input type="submit" value="登录"/></div>
</form>
</body>
</html>
登录:login.php
<?php include("../init.inc.php"); //把smarty模板引进 $smarty->display("login.html");//在login.html页面写出登录界面,在login.php里执行
登录处理:loginchuli.php
<?php //login登录处理界面 session_start(); $uid = $_POST["uid"]; $pwd = $_POST["pwd"]; include("../DBDA.php"); $db = new DBDA(); $sql = "select pass from login where username = '{$uid}'"; $mm = $db->StrQuery($sql); if($pwd != "" && $pwd==$mm) { $_SESSION["uid"] = $uid; header("location:main.php"); } else { header("location:login.php"); }
二、主页面:main.html
#top{ 100%; height:50px} #name{ float:right; height:50px; 100px; text-align:center; line-height:50px; vertical-align:middle} </style> </head> <body> <div id="top"> <div id="name"><{$name}></div> </div> <div>人员信息表</div> <div> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> <td>操作</td> </tr> <{foreach $info as $v}> <tr> <td><{$v[0]}></td> <td><{$v[1]}></td> <td><{$v[2]|sex}></td> <!--把之前的sex=0,1改为男,女,方法->modifier.sex.php(自写小插件)--> <td><{$v[3]|nation}></td> <!--把之前的nation=n001,n002改为汉族,回族,方法->modifier.nation.php(自写小插件)--> <td><{$v[4]}></td> <td><a href="shanchu.php?code=<{$v[0]}>">删除</a><a href="xiugai.php?id=<{$v[0]}>">修改</a></td><!--给删除,修改传键值,此键值为主键值--> </tr> <{/foreach}> </table> </div> </body>
所用组件:
modifier.sex.php
<?php function smarty_modifier_sex($sex) { return $sex?"男":"女"; //ture返回'男',false返回'女' }
modifier.nation.php
<?php function smarty_modifier_nation($nation) { $db = new MySQLi("localhost","root","2786802","text_zuoye"); //引入DBDA的类不可用,只能写入最原始方法来调用数据库 $sql = "select name from nation where code = '{$nation}'"; $result = $db->query($sql); $attr = $result->fetch_row(); return $attr[0]; }
主页面:main.php
<?php session_start(); include("../init.inc.php"); include("../DBDA.php"); $db = new DBDA(); if(empty($_SESSION["uid"])) //判断session是否为空,不为空取值,为空跳登录页面 { header("location:login.php"); exit; } $uid = $_SESSION["uid"]; $sql = "select name from login where username = '{$uid}'"; $name = $db->StrQuery($sql); $smarty->assign("name",$name); //smarty取到登录的uid,到main.html取出显示 $sqli = "select * from info "; $attr = $db->Query($sqli); $smarty->assign("info",$attr); //smarty取到info的数据放到数组,到main.html取出显示 $smarty->display("main.html"); //在本页面显示main.html的页面数据
三、修改页面:xiugai.html
<body> <form action="gai.php" method="post"> <div><input name="code" type="hidden" value="<{$info[0]}>" /></div> <div>姓名:<input name="name" type="text" value="<{$info[1]}>" /></div> <div>性别: <input type="radio" name="sex" value="1" <{$info[2]|cksex}> />男 <!--改为单选按钮,方法->modifier.ck1/ck.php(自写小插件)--> <input type="radio" name="sex" value="0" <{$info[2]|cksex1}> />女 </div> <div>民族: <!--民族改为下拉菜单显示,在xiugai.php页面查询出nation表的name,通过$nation接收,并且根据主键值通过循环给其所属的民族赋属性checked="checked",默认选中;--> <select name="nation"> <{foreach $nation as $v}> <{if $v[0]==$info[3]}> <option selected="selected" value="<{$v[0]}>"><{$v[1]}></option> <{else}> <option value="<{$v[0]}>"><{$v[1]}></option> <{/if}> <{/foreach}> </select> </div> <div>生日:<input name="birthday" type="text" value="<{$info[4]}>" /></div> <input type="submit" value="修改" /> </form> </body>
modifier_cksex.php
<?php function smarty_modifier_cksex($sex) { return $sex?"checked='checked'":""; //如果为ture,checked='checked' }
modifier_cksex1.php
<?php function smarty_modifier_cksex1($sex) { return $sex?"":"checked='checked'"; //如果为false,checked='checked' }
修改页面:xiugai.php
<?php $id = $_GET["id"]; include("../DBDA.php"); $db = new DBDA(); $sql = "select * from info where code='{$id}'"; $attr = $db->Query($sql); $sqlnation = "select * from nation"; $attrn = $db->Query($sqlnation); include("../init.inc.php"); //执行修改要显示页面,把smarty模板引入 $smarty->assign("info",$attr[0]); //$attr[0]赋给info,在html页面$info提取 $smarty->assign("nation",$attrn); //xiugai.html页面民族改下拉菜单时用 $smarty->display("xiugai.html"); //把xiugai.html里面的内容拿过来在本页面显示
删除页面:shanchu.php
<?php include("../DBDA.php"); $db = new DBDA(); $code = $_GET["code"]; $sql = "delete from info where code='{$code}'"; $db->Query($sql,0); header("location:main.php");