關於smarty最直觀的感覺就是分離了頁面中html和php的代碼,頁面再也不混亂,很清晰了……php
smarty->assign();//註冊html
smarty->display();//加載模板sql
除了老師給的表,kemu,nandu,type都建了表格,便於使用post
main.phpui
<?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.htmlspa
<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> <a href="update.php?code=<{$v[0]}>">更改</a></td> </tr> <{/foreach}> </table> <a href="add.php">添加數據</a> </body> </html>
add.phpcode
<?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.htmlorm
<!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.phpxml
<?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.phphtm
<?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"更改失敗"; }