smarty应用
smarty可以實(shí)現(xiàn)HTML與php代碼的分離,之前我們用php代碼做過數(shù)據(jù)的增刪改查,現(xiàn)在我們運(yùn)用smarty來實(shí)現(xiàn)這些功能,并用分頁顯示
?
查詢;
主頁面,以表格的形式顯示數(shù)據(jù)及需要的操作: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">添加數(shù)據(jù)</a> <div><{$fpage}></div> </body>?
one.php:對one.html頁面進(jìn)行控制,查找數(shù)據(jù),傳送數(shù)據(jù),并通過運(yùn)行該頁面顯示one.html頁面內(nèi)容;在該頁面實(shí)現(xiàn)分頁查詢
<?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;//注意空格//總數(shù) $sqlz = "select count(*) from timu".$ftj; $total = $db->StrQuery($sqlz); $page = new Page($total,2);//查詢數(shù)據(jù)實(shí)現(xiàn)分頁 $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:增加數(shù)據(jù)頁面
<body> <h1>添加數(shù)據(jù)</h1> <form action="addchuli.php" method="post"> <div>請輸入題目名稱:<input type="text" name="name" /></div> <div>請輸入選項(xiàng)A:<input type="text" name="a" /></div> <div>請輸入選項(xiàng)B:<input type="text" name="b" /></div> <div>請輸入選項(xiàng)C:<input type="text" name="c" /></div> <div>請輸入選項(xiàng)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;將數(shù)據(jù)傳入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:將添加的數(shù)據(jù)加入數(shù)據(jù)庫
<?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) {//添加選項(xiàng)$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:主頁面點(diǎn)擊刪除按鈕時(shí),根據(jù)傳來的主鍵值刪除數(shù)據(jù)庫相關(guān)數(shù)據(jù)
<?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;主頁面點(diǎn)擊修改按鈕時(shí),到達(dá)該頁面,并顯示出已經(jīng)存在的相關(guān)數(shù)據(jù),點(diǎn)擊該頁面修改按鈕后,將修改后數(shù)據(jù)傳入數(shù)據(jù)庫,原數(shù)據(jù)消失
<body> <h1>修改數(shù)據(jù)</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>請輸入選項(xiàng)<{$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:查找數(shù)據(jù)庫,將原有數(shù)據(jù)傳入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:將修改后的數(shù)據(jù)提交到數(shù)據(jù)庫,原有數(shù)據(jù)刪除
<?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)) {//添加選項(xiàng)$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 "刪除失敗"; }轉(zhuǎn)載于:https://www.cnblogs.com/dianfu123/p/5566987.html
總結(jié)
- 上一篇: ML_R kNN
- 下一篇: 闽江学院2015-2016学年下学期《软