smarty可以实现HTML与php代码的分离,之前我们用php代码做过数据的增删改查,现在我们运用smarty来实现这些功能,并用分页显示
查询;
主页面,以表格的形式显示数据及需要的操作:one.html
<body> <h1>主页面</h1> <form action="one.php" method="get"> <div>题目名称: <input type="text" name="name" /> 所属科目: <select name="km"> <{foreach $km as $v}> <option value="<{$v[0]}>"><{$v[1]}></option> <{/foreach}> </select> <input type="submit" value="查询" /> </div> </form> <table border="1" width="100%" cellpadding="0" cellspacing="0"> <tr> <td>题目名称</td> <td>答案</td> <td>所属科目</td> <td>难度</td> <td>类型</td> <td>操作</td> </tr> <{foreach $shuju as $v}> <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> <a href="update.php?code=<{$v[0]}>">修改</a></td> </tr> <{/foreach}> </table> <a href="add.php">添加数据</a> <div><{$fpage}></div> </body>
one.php:对one.html页面进行控制,查找数据,传送数据,并通过运行该页面显示one.html页面内容;在该页面实现分页查询
<?php include("../init.inc.php"); include("../../DBDA.class.php"); include("page.class.php"); $db = new DBDA(); //查科目 $skm = "select * from kemu"; $akm = $db->Query($skm); //查询条件 $tj = " 1=1 ";//题目 $tj2 = " 1=1 ";//科目 if(!empty($_GET["name"])) { $tj = " name like ‘%{$_GET[‘name‘]}%‘ "; } if(!empty($_GET["km"])) { $tj2 = " kemu = ‘{$_GET[‘km‘]}‘ "; } $ftj = " where".$tj." and".$tj2;//注意空格 //总数 $sqlz = "select count(*) from timu".$ftj; $total = $db->StrQuery($sqlz); $page = new Page($total,2); //查询数据实现分页 $sql = "select * from timu".$ftj.$page->limit; $attr = $db->Query($sql); $fpage = $page->fpage(); $smarty->assign("km",$akm); $smarty->assign("fpage",$fpage); $smarty->assign("shuju",$attr); $smarty->display("one.html");
增加:
add.html:增加数据页面
<body> <h1>添加数据</h1> <form action="addchuli.php" method="post"> <div>请输入题目名称:<input type="text" name="name" /></div> <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> <div>请输入题目答案:<input type="text" name="daan" /></div> <div>请输入科目: <select name="kemu"> <{foreach $kemu as $v}> <option value="<{$v[0]}>"><{$v[1]}></option> <{/foreach}> </select> </div> <div>请输入题目难度: <select name="nandu"> <{foreach $nandu as $v}> <option value="<{$v[0]}>"><{$v[1]}></option> <{/foreach}> </select> </div> <div>请输入题目类型: <select name="type"> <{foreach $type as $v}> <option value="<{$v[0]}>"><{$v[1]}></option> <{/foreach}> </select> </div> <input type="submit" value="添加" /> </form> </body>
add.php;将数据传入add.html页面
<?php include("../init.inc.php"); include("../../DBDA.class.php"); $db = new DBDA(); $sql = "select * from kemu"; $attr = $db->Query($sql); $sql = "select * from nandu"; $attrnd = $db->Query($sql); $sql = "select * from type"; $attrt = $db->Query($sql); $smarty->assign("type",$attrt); $smarty->assign("nandu",$attrnd); $smarty->assign("kemu",$attr); $smarty->display("add.html");
addchuli.php:将添加的数据加入数据库
<?php include("../../DBDA.class.php"); $db = new DBDA(); $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"]; //添加题目 $sql = "insert into timu values(‘‘,‘{$name}‘,‘{$daan}‘,‘{$kemu}‘,‘{$nandu}‘,‘{$type}‘)"; $r = $db->Query($sql,0); if($r) { //添加选项 $id = $db->conn->insert_id; $sqla = "insert into xuanxiang values(‘‘,‘{$a}‘,‘A‘,‘{$id}‘)"; $db->Query($sqla,0); $sqlb = "insert into xuanxiang values(‘‘,‘{$b}‘,‘B‘,‘{$id}‘)"; $db->Query($sqlb,0); $sqlc = "insert into xuanxiang values(‘‘,‘{$c}‘,‘C‘,‘{$id}‘)"; $db->Query($sqlc,0); $sqld = "insert into xuanxiang values(‘‘,‘{$d}‘,‘D‘,‘{$id}‘)"; $db->Query($sqld,0); header("location:add.php"); } else { echo "添加失败"; }
删除:
delete.php:主页面点击删除按钮时,根据传来的主键值删除数据库相关数据
<?php $code = $_GET["code"]; include("../../DBDA.class.php"); $db = new DBDA(); $sql = "delete from xuanxiang where timu=‘{$code}‘"; $db->Query($sql,0); $sql = "delete from timu where code=‘{$code}‘"; $db->Query($sql,0); header("location:one.php");
修改;
update.html;主页面点击修改按钮时,到达该页面,并显示出已经存在的相关数据,点击该页面修改按钮后,将修改后数据传入数据库,原数据消失
<body> <h1>修改数据</h1> <form action="updatechuli.php?code=<{$timu[0][0]}>" method="post"> <div>请输入题目名称:<input type="text" name="name" value="<{$timu[0][1]}>" /></div> <{foreach $xuanxiang as $v}> <div>请输入选项<{$v[2]}>:<input type="text" name="<{$v[2]}>" value="<{$v[1]}>" /></div> <{/foreach}> <div>请输入题目答案:<input type="text" name="daan" value="<{$timu[0][2]}>" /></div> <div>请输入科目: <select name="kemu"> <{foreach $kemu as $v}> <{if $timu[0][3]==$v[0]}> <option selected="selected" value="<{$v[0]}>"><{$v[1]}></option> <{else}> <option value="<{$v[0]}>"><{$v[1]}></option> <{/if}> <{/foreach}> </select> </div> <div>请输入题目难度: <select name="nandu"> <{foreach $nandu as $v}> <{if $timu[0][4]==$v[0]}> <option selected="selected" value="<{$v[0]}>"><{$v[1]}></option> <{else}> <option value="<{$v[0]}>"><{$v[1]}></option> <{/if}> <{/foreach}> </select> </div> <div>请输入题目类型: <select name="type"> <{foreach $type as $v}> <{if $timu[0][5]==$v[0]}> <option selected="selected" value="<{$v[0]}>"><{$v[1]}></option> <{else}> <option value="<{$v[0]}>"><{$v[1]}></option> <{/if}> <{/foreach}> </select> </div> <input type="submit" value="修改" /> </form> </body>
update.php:查找数据库,将原有数据传入update.html页面
<?php include("../init.inc.php"); include("../../DBDA.class.php"); $db = new DBDA(); $code = $_GET["code"]; $sql = "select * from timu where code=‘{$code}‘"; $attr = $db->Query($sql); $sql1 = "select * from xuanxiang where timu=‘{$code}‘"; $attrxx = $db->Query($sql1); $sql2 = "select * from kemu"; $attrkm = $db->Query($sql2); $sql3 = "select * from nandu"; $attrnd = $db->Query($sql3); $sql4 = "select * from type"; $attrt = $db->Query($sql4); $smarty->assign("type",$attrt); $smarty->assign("nandu",$attrnd); $smarty->assign("kemu",$attrkm); $smarty->assign("xuanxiang",$attrxx); $smarty->assign("timu",$attr); $smarty->display("update.html");
updatechuli.php:将修改后的数据提交到数据库,原有数据删除
<?php include("../../DBDA.class.php"); $db = new DBDA(); $name = $_POST["name"]; $daan = $_POST["daan"]; $kemu = $_POST["kemu"]; $nandu = $_POST["nandu"]; $type = $_POST["type"]; $code = $_GET["code"]; $a = $_POST["A"]; $b = $_POST["B"]; $c = $_POST["C"]; $d = $_POST["D"]; $sqls = "delete from timu where code=‘{$code}‘"; if($db->Query($sqls,0)) { $sql = "insert into timu values(‘‘,‘{$name}‘,‘{$daan}‘,‘{$kemu}‘,‘{$nandu}‘,‘{type}‘)"; if($db->Query($sql,0)) { //添加选项 $id = $db->conn->insert_id; $sqla = "insert into xuanxiang values(‘‘,‘{$a}‘,‘A‘,‘{$id}‘)"; $db->Query($sqla,0); $sqlb = "insert into xuanxiang values(‘‘,‘{$b}‘,‘B‘,‘{$id}‘)"; $db->Query($sqlb,0); $sqlc = "insert into xuanxiang values(‘‘,‘{$c}‘,‘C‘,‘{$id}‘)"; $db->Query($sqlc,0); $sqld = "insert into xuanxiang values(‘‘,‘{$d}‘,‘D‘,‘{$id}‘)"; $db->Query($sqld,0); header("location:one.php"); } else { echo "修改失败"; } } else { echo "删除失败"; }
时间: 2024-11-09 02:13:25