#---- 商品排序
case "opSort":
opSort();
break;
```
2.
二、函數
1. opSort ```
########################################
# 列表
########################################
function opSort() {
global $xoopsDB, $xoopsTpl, $module_name, $kind, $ugmKind;
#---- 過濾讀出的變數值 ----
$myts = MyTextSanitizer::getInstance();
$sql = "select a.sn,a.title,a.enable,a.choice,a.date,a.sort
from " . $xoopsDB->prefix("cnu_show_prod") . " as a
order by a.sort desc,a.date desc"; //die($sql);
$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'], 3, web_error());
$rows = array();
#----單檔圖片上傳
$subdir = "prod"; //子目錄(前後不要有 / )
$ugmUpFiles = new ugmUpFiles($module_name, $subdir); //實體化
$col_name = "prod"; //資料表關鍵字
$thumb = true ; //顯示縮圖
while ($row = $xoopsDB->fetchArray($result)) {
$row['sn'] = intval($row['sn']);
$row['title'] = $myts->htmlSpecialChars($row['title']);
$row['sort'] = intval($row['sort']);
$row['enable'] = intval($row['enable']);
$row['choice'] = intval($row['choice']);
#日期
$row['date'] = intval($row['date']);
$row['date'] = date("Y-m-d", xoops_getUserTimestamp($row['date'])); //從資料庫撈出
$col_sn = $row['sn']; //關鍵字流水號
$row['prod'] = $ugmUpFiles->get_rowPicSingleUrl($col_name,$col_sn,$thumb);
#-----------------------------------
if($row['prod']){
$row['prod'] = "";
}
$rows[] = $row;
}
$xoopsTpl->assign("rows", $rows);
#-----拖曳排序 -------------------------
get_jquery(true);
#---------------------------------------
}
```
2. 樣板 ```
<{if $op=="opSort"}>
<{* 排序 *}>
<{/if}>
```
3. ajax
main.php -> switch ```
case "opUpdateSort": //更新排序
#強制關除錯
ugm_module_debug_mode(0);
echo opUpdateSort();
exit;
```
```
########################################
# 自動更新排序ajax
########################################
function opUpdateSort() {
global $xoopsDB;
$sort = getTblRow("cnu_show_prod");
$msg="";
foreach ($_POST['tr'] as $sn) {
if (!$sn) {
continue;
}
$sql = "update " . $xoopsDB->prefix("cnu_show_prod") . " set `sort`='{$sort}' where `sn`='{$sn}'";
if(!$xoopsDB->queryF($sql)){
$msg[]=$sn;
}
$sort--;
}
if(!$msg){
return true;
}
return false;
}
#
function getTblRow($tbl){
global $xoopsDB;
$sql="select count(*) as count from ".$xoopsDB->prefix($tbl);//die($sql);
$result=$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'], 3, web_error());
$row = $xoopsDB->fetchArray($result);
return $row['count'];
}
```
4.