smarty引擎之练习

Posted MrY的nn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了smarty引擎之练习相关的知识,希望对你有一定的参考价值。

关于smarty最直观的感受就是分离了页面中htmlphp的代码,页面不再混乱,很清晰了……

smarty->assign();//注册

smarty->display();//加载模板

除了老师给的表,kemu,nandu,type都建了表格,便于使用

main.php

<?php

include("./init.inc.php");//加载配置文件
//echo str_replace("\\","/",dirname(__FILE__))."/";
include("ChaXun.class.php");//加载类
$db=new ChaXun();
$sql="select * from timu";
$attr=$db->Query($sql);
//var_dump($attr);

$smarty->assign("shuju",$attr);//将要注册内容给smarty
$smarty->display("main.html");

main.html

<html>
<head>
<meta charset="utf-8" />
</head>

<body>
<h1>主页面</h1>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>题目名称</td>
<td>答案</td>
<td>所属科目</td>
<td>难度</td>
<td>题目类型</td>
<td>操作</td>
</tr>
<{foreach $shuju as $v}>//遍历smarty传过来的$shuju,这里使用foreach要注意结束foreach
<tr>
<td><{$v[1]}></td>
<td><{$v[2]}></td>
<td><{$v[3]}></td>
<td><{$v[4]}></td>
<td><{$v[5]}></td>
<td><a href="delete.php?code=<{$v[0]}>">删除</a>&nbsp;&nbsp;<a href="update.php?code=<{$v[0]}>">更改</a></td>
</tr>

<{/foreach}>
</table>
<a href="add.php">添加数据</a>

</body>
</html>

add.php

<?php
include("./init.inc.php");
//echo str_replace("\\","/",dirname(__FILE__))."/";
include("ChaXun.class.php");
$db=new ChaXun();
$sqlkm="select * from kemu";
$akm=$db->Query($sqlkm);

$sqlnd="select * from nandu";
$and=$db->Query($sqlnd);

$sqltp="select * from type";
$atp=$db->Query($sqltp);

$smarty->assign("kemu",$akm);
$smarty->assign("nandu",$and);
$smarty->assign("type",$atp);
$smarty->display("add.html");

add.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>
<h1>添加页面</h1>

<form action="addchuli.php" method="post">
<div>题目名称:<input type="text" name="name" /></div><br />

<div>请输入选项A:<input type="text" name="A" /></div>
<div>请输入选项B:<input type="text" name="B" /></div>
<div>请输入选项C:<input type="text" name="C" /></div>
<div>请输入选项D:<input type="text" name="D" /></div><br />

<div>题目答案:<input type="text" name="daan" /></div><br />
<div>所属科目:<select name="kemu">
                <{foreach $kemu as $v}>
                <option value="<{$v[0]}>"><{$v[1]}></option>
                <{/foreach}>
            </select></div><br />

<div>难度:<select name="nandu">
             <{foreach $nandu as $v}>
             <option value="<{$v[0]}>"><{$v[1]}></option>
             <{/foreach}>
          </select></div><br />

<div>题目类型:<select name="leixing">
               <{foreach $type as $v}>
               <option value="<{$v[0]}>"><{$v[1]}></option>
               <{/foreach}>
             </select></div><br />

<div><input type="submit" value="添加数据"/></div>

</form>
</body>
</html>

addchuli.php

<?php
include("ChaXun.class.php");
$db=new ChaXun();

$name=$_POST["name"];
$daan=$_POST["daan"];
$kemu=$_POST["kemu"];
$nandu=$_POST["nandu"];
$type=$_POST["leixing"];

$A=$_POST["A"];
$B=$_POST["B"];
$C=$_POST["C"];
$D=$_POST["D"];

$sqltm="insert into timu values(‘‘,‘{$name}‘,‘{$daan}‘,‘{$kemu}‘,‘{$nandu}‘,‘{$type}‘)";
if($db->Query($sqltm,1))
{    
    $id=$db->conn->insert_id;
        
    $sqlA="insert into xuanxiang values(‘‘,‘{$A}‘,‘A‘,‘{$id}‘)";
    $aA=$db->Query($sqlA,1);
    
    $sqlB="insert into xuanxiang values(‘‘,‘{$B}‘,‘B‘,‘{$id}‘)";
    $aB=$db->Query($sqlB,1);
    
    $sqlC="insert into xuanxiang values(‘‘,‘{$C}‘,‘C‘,‘{$id}‘)";
    $aC=$db->Query($sqlC,1);
    
    $sqlD="insert into xuanxiang values(‘‘,‘{$D}‘,‘D‘,‘{$id}‘)";
    $aD=$db->Query($sqlD,1);

    header("location:add.php");
}
else
{
    echo"添加失败";
}

delete.php

<?php
include("ChaXun.class.php");
$db=new ChaXun();

$code=$_GET["code"];

$sql="delete from timu where code=‘{$code}‘";
$db->Query($sql,1);

$sqlxx="delete from xuanxiang where timu=‘{$code}‘";
$db->Query($sqlxx,1);

header("location:main.php");

update.php

<?php
include("./init.inc.php");
//echo str_replace("\\","/",dirname(__FILE__))."/";
include("ChaXun.class.php");
$db=new ChaXun();
$code=$_GET["code"];

$sql="select * from timu where code=‘{$code}‘";
$atm=$db->Query($sql);

$sqlxx="select * from xuanxiang where timu=‘{$code}‘";
$axx=$db->Query($sqlxx);

$sqlkm="select * from kemu";
$akm=$db->Query($sqlkm);

$sqlnd="select * from nandu";
$and=$db->Query($sqlnd);

$sqltp="select * from type";
$atp=$db->Query($sqltp);


$smarty->assign("type",$atp);
$smarty->assign("nandu",$and);
$smarty->assign("kemu",$akm);
$smarty->assign("xx",$axx);
$smarty->assign("timu",$atm);
$smarty->display("update.html");

updatechuli.php

<?php

include("ChaXun.class.php");
$db=new ChaXun();

$code=$_GET["code"];
$name=$_POST["name"];
$daan=$_POST["daan"];
$kemu=$_POST["kemu"];
$nandu=$_POST["nandu"];
$type=$_POST["leixing"];
//这里也可以先删除清空所有,再insert into 也可以
$sql="update timu set name=‘{$name}‘,daan=‘{$daan}‘,kemu=‘{$kemu}‘,nandu=‘{$nandu}‘,type=‘{$type}‘ where code=‘{$code}‘";

//var_dump($db->Query($sql,1));
if($db->Query($sql,1))
{
    header("location:main.php");
}
else
{
    echo"更改失败";
}

 

以上是关于smarty引擎之练习的主要内容,如果未能解决你的问题,请参考以下文章

Smarty模板引擎

PHP - Smarty 模板 - 如何创建与上下文无关的模板

模板引擎(smarty)知识点总结五

Smarty模版引擎的原理

Smarty 模板引擎简介

PHP 随笔---Smarty模板引擎 已有模板和Smarty结合 11