码迷,mamicode.com
首页 > Web开发 > 详细

PHP Smarty增删改查

时间:2016-07-14 13:10:55      阅读:709      评论:0      收藏:0      [点我收藏+]

标签:

配置文件 init.inc.php

<?php
define("ROOT",str_replace("\\","/",dirname(__FILE__)).‘/‘); //常量ROOT中指定项目根目录(常量的名称,常量的值)
//当前文件的目录,不包含文件名 dirname(__FILE__))

require ROOT.‘libs/Smarty.class.php‘; //加载Smarty类文件

$smarty = new Smarty(); //实例化Smarty对象<br>

//设置初始路径
//$smarty -> auto_literal = false; //就可以让定界符号使用空格
$smarty->setTemplateDir(ROOT.‘templates/‘); //设置所有模板文件存放位置
//$smarty->addTemplateDir(ROOT.‘templates2/‘); //添加一个模板文件夹
$smarty->setCompileDir(ROOT.‘templates_c/‘); //设置编译过的模板存放的目录
$smarty->addPluginsDir(ROOT.‘plugins/‘); //设置为模板扩充插件存放目录
$smarty->setCacheDir(ROOT.‘cache/‘); //设置缓存文件存放目录
$smarty->setConfigDir(ROOT.‘configs/‘); //设置模板配置文件存放目录

$smarty->caching = false; //设置Smarty缓存开关功能
$smarty->cache_lifetime = 60*60*24; //设置缓存模板有效时间一天
$smarty->left_delimiter = ‘<{‘; //设置模板语言中的左结束符
$smarty->right_delimiter = ‘}>‘; //设置模板语言中的右结束符
?>

技术分享

main.php

<?php
include("../init.inc.php");
include("../../dbda.php");
$db=new DBDA();

$stimu="select * from timu";
$timu=$db->Query($stimu);

foreach($timu as $k=>$v)
{
    //处理正确答案名称
    $sxuanxiang="select name from xuanxiang where timu={$v[0]} and xuanxiang=‘{$v[2]}‘";
    $timu[$k][2]=$db->StrQuery($sxuanxiang);
                
    //处理科目名称
    $skemu="select name from kemu where code=‘{$v[3]}‘";
    $timu[$k][3]=$db->StrQuery($skemu);
    
    //处理难度名称
    $snandu="select name from nandu where code=‘{$v[4]}‘";
    $timu[$k][4]=$db->StrQuery($snandu);
    
    //处理类型名称
    $sleixing="select name from leixing where code=‘{$v[5]}‘";
    $timu[$k][5]=$db->StrQuery($sleixing);
}
$smarty->assign("timu",$timu);

$smarty->display("main.html");

main.html

<style type="text/css">
.a
{
    padding-left:150px;
    padding-top:50px;
}
.but
{
    padding-left:1100px;
}
</style>
</head>

<body>
<div class="a">
    <table width="1000px" border="1" cellpadding="0" cellspacing="0">
        <tr height="30px" align="center" style="font-weight:bold" >
            <td>题目名称</td>
            <td>答案</td>
            <td>科目</td>
            <td>难度</td>
            <td>类型</td>
            <td>操作</td>
        </tr>
        <{foreach $timu as $v}>
        <tr height="25px" align="center" > 
            <td><{$v[1]|truncate:5}></td>
            <td><{$v[2]}></td>
            <td><{$v[3]}></td>
            <td><{$v[4]}></td>
            <td><{$v[5]}></td>
            
             <td><a href="update.php?code=<{$v[0]}>">修改</a>&nbsp;&nbsp;<a href="delete.php?code=<{$v[0]}>">删除</a>&nbsp;&nbsp;<a href="">查看详情</a></td>
        </tr>
        <{/foreach}>
    </table>
</div><br />
<div class="but"><a href="add.php">添加数据</a></div>

                                       技术分享

update.php

<?php
include("../init.inc.php");
include("../../dbda.php");
$db=new DBDA();

$code=$_GET["code"];

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

$sxuanxiang="select * from xuanxiang where timu=‘{$code}‘  order by code asc";
$xuanxiang=$db->Query($sxuanxiang);

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

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

$sleixing="select * from leixing";
$leixing=$db->Query($sleixing);

$smarty->assign("kemu",$kemu);
$smarty->assign("timu",$timu);
$smarty->assign("nandu",$nandu);
$smarty->assign("leixing",$leixing);
$smarty->assign("xuanxiang",$xuanxiang);

$smarty->display("update.html");

updatechuli.php

<?php
$code=$_GET["code"];
include("../../dbda.php");
$db=new DBDA();

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

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

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

$a=$_POST["a"];
$b=$_POST["b"];
$c=$_POST["c"];
$d=$_POST["d"];

//添加题目
$stimu="insert into timu values(‘‘,‘{$name}‘,‘{$daan}‘,‘{$kemu}‘,‘{$nandu}‘,‘{$type}‘)";
$timu=$db->Query($stimu,0);

if($timu)
{
    //取上面添加的题目的代号
    $id=$db->conn->insert_id;
        
    //添加选项
    $sxxa="insert into xuanxiang values(‘‘,‘{$a}‘,‘A‘,‘{$id}‘)";
    $db->Query($sxxa,0);
    $sxxb="insert into xuanxiang values(‘‘,‘{$b}‘,‘B‘,‘{$id}‘)";
    $db->Query($sxxb,0);
    $sxxc="insert into xuanxiang values(‘‘,‘{$c}‘,‘C‘,‘{$id}‘)";
    $db->Query($sxxc,0);
    $sxxd="insert into xuanxiang values(‘‘,‘{$c}‘,‘D‘,‘{$id}‘)";
    $db->Query($sxxd,0);
    
    
    header("location:add.php");
}
else
{
    echo"添加失败";
}

 

PHP Smarty增删改查

标签:

原文地址:http://www.cnblogs.com/yy01/p/5669983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!