編輯
```
2. 程式 ```
###############################
# 商品表單
###############################
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);
}
```
3. 樣板 ```
<{if $op == "opForm"}>
<{/if}>
```
4. 取得商品某筆資料 ```
###############################
# 商品單筆資訊
###############################
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;
}
```
5. 用 $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;
}
```
6.