PHP SMARTY 樣板引擎
一、商品編輯
- 列表樣板 <a href="admin_prod.php?op=opForm&sn=<{$row.sn}>" class="btn btn-success btn-xs">編輯</a>
- 程式
###############################
# 商品表單
###############################
function opForm($sn=""){
global $db,$smarty;
if($sn){
#編輯
$row = getProd($sn);
$row['op'] = "opUpdate";
$row['img'] = getProdImgPath("prod",$sn);
}else{
#新增
$row =array();
$row['op'] = "opInsert";
$row['img']="";
}
$row['sn'] = isset($row['sn']) ? $row['sn']: "";
$row['kind'] = isset($row['kind']) ? $row['kind']: "";
$row['kind_options'] = get_kind_options($row['kind']);
$row['title'] = isset($row['title']) ? $row['title']: "";
$row['summary'] = isset($row['summary']) ? $row['summary']: "";
$row['content'] = isset($row['content']) ? $row['content']: "";
$row['price'] = isset($row['price']) ? $row['price']: 0;
$row['amount'] = isset($row['amount']) ? $row['amount']: 0;
$row['enable'] = isset($row['enable']) ? $row['enable']: "1";
$row['choice'] = isset($row['choice']) ? $row['choice']: "0";
#目前日期时间戳記 1970 00:00:00 GMT 的秒數
$now = strtotime("now");//得到目前網頁伺服器的「时间戳記」
$row['date'] = isset($row['date']) ? $row['date']: $now;
$row['date'] = date("Y-m-d",$row['date']);//格式化
$row['sort'] = isset($row['sort']) ? $row['sort']: getProdMaxSort();
$row['icon'] = isset($row['icon']) ? $row['icon']: "fa-facebook";
$DirName = "prod";
mk_dir(WEB_PATH . "/uploads/{$DirName}");
mk_dir(WEB_PATH . "/uploads/{$DirName}/image");
mk_dir(WEB_PATH . "/uploads/{$DirName}/flash");
include_once WEB_PATH . "/class/ck.php";
$fck = new CKEditor($DirName, "content", $row['content']);
$fck->setHeight(350);
$row['content'] = $fck->render();
$smarty->assign("row", $row);
#防止偽造表單
$token = getTokenHTML();
$smarty->assign("token", $token);
}
- 樣板
<{if $op == "opForm"}>
<script type="text/javascript" src="<{$xoAppUrl}>class/My97DatePicker/WdatePicker.js"></script>
<link rel="stylesheet" type="text/css" href="<{$xoAppUrl}>class/fontawesome-iconpicker/css/fontawesome-iconpicker.min.css">
<script type="text/javascript" src="<{$xoAppUrl}>class/fontawesome-iconpicker/js/fontawesome-iconpicker.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#icon').iconpicker();
});
</script>
<div class="panel panel-primary">
<div class="panel-heading">商品管理</div>
<div class="panel-body">
<form action="admin_prod.php" method="POST" role="form" id="opForm" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<label for="title">商品名稱</label>
<input type="text" class="form-control" id="title" name="title" value="<{$row.title}>">
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label for="kind">類別</label>
<select name="kind" class="form-control" size="1">
<{$row.kind_options}>
</select>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label for="price">商品價格</label>
<input type="text" class="form-control" id="price" name="price" value="<{$row.price}>">
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label for="amount">商品數量</label>
<input type="text" class="form-control" id="amount" name="amount" value="<{$row.amount}>">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<label style="display:block;">啟用</label>
<input type="radio" name="enable" id="enable_1" value="1" <{if $row.enable == '1'}>checked<{/if}>>
<label for="enable_1">啟用</label>
<input type="radio" name="enable" id="enable_0" value="0" <{if $row.enable == '0'}>checked<{/if}>>
<label for="enable_0">停用</label>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label style="display:block;">精選</label>
<input type="radio" name="choice" id="choice_1" value="1" <{if $row.choice == '1'}>checked<{/if}>>
<label for="choice_1">啟用</label>
<input type="radio" name="choice" id="choice_0" value="0" <{if $row.choice == '0'}>checked<{/if}>>
<label for="choice_0">停用</label>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label for="date">建立日期</label>
<input type="text" class="form-control" id="date" name="date" value="<{$row.date}>" onClick="WdatePicker()">
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label for="sort">排序</label>
<input type="text" class="form-control" id="sort" name="sort" value="<{$row.sort}>">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<label for="icon">圖示</label>
<div class="input-group iconpicker-container">
<input type="text" data-placement="bottomRight" class="form-control icp icp-auto iconpicker-element iconpicker-input" id="icon" name="icon" value="<{$row.icon}>">
<span class="input-group-addon"><i class="fa <{$row.icon}>"></i></span>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label for="pic">圖片<span>(800x449)</span></label>
<input type="file" name="pic" accept="image/*" class="form-control">
<br>
<img src="<{$row.img}>" class="img-responsive">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="summary">商品摘要</label>
<textarea class="form-control" rows="4" id="summary" name="summary"><{$row.summary}></textarea>
</div>
</div>
</div>
<div class="form-group">
<label for="content">商品內容</label>
<{$row.content}>
</div>
<{$token}>
<button type="submit" class="btn btn-primary btn-block">送出</button>
<input type="hidden" value="<{$row.op}>" name="op">
<input type="hidden" value="<{$row.sn}>" name="sn" id="sn">
</form>
</div>
</div>
<{/if}>
- 取得商品某筆資料
###############################
# 商品單筆資訊
###############################
function getProd($sn){
global $db;
#撈資料
$sql = "select *
from `ugm_p_prod`
where `sn`='{$sn}'
";
$result = $db->query($sql) or redirect_header("", 3000, $db->error."\n".$sql,true);
$row = $result->fetch_assoc();
return $row;
}
- 用 $col_name,$col_sn 取得圖片路徑
#用 $col_name,$col_sn 取得圖片路徑
function getProdImgPath($col_name,$col_sn){
global $db;
#撈資料
$sql = "select `file_name`,`sub_dir`
from `ugm_p_files_center`
where `col_name`='{$col_name}' and `col_sn`='{$col_sn}'
";
$result = $db->query($sql) or redirect_header("", 3000, $db->error."\n".$sql,true);
$row = $result->fetch_assoc();
if($row){
$row['file_name'] = htmlspecialchars($row['file_name'], ENT_QUOTES);
$row['sub_dir'] = htmlspecialchars($row['sub_dir'], ENT_QUOTES);
$ImgPath = WEB_URL."/uploads/".$row['sub_dir']."/".$row['file_name'];
}else{
$ImgPath="";
}
return $ImgPath;
}