最新xoops模組開發
一、流程
- switch
	
  #---- 商品排序
  case "opSort":
    opSort();
    break;
	
 
二、函數
- 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'] = "<img src='{$row['prod']}' style='width:50px;' class='img-responsive center-block'>";
    }
    $rows[] = $row;
  }
  $xoopsTpl->assign("rows", $rows);
  #-----拖曳排序 -------------------------
  get_jquery(true);
  #---------------------------------------
}
	
 - 樣板
	
<{if $op=="opSort"}>
	
	<{* 排序 *}> 
	<link rel="stylesheet" href="<{xoAppUrl modules/tadtools/sweet-alert/sweet-alert.css}>" type="text/css" />
	<script src="<{xoAppUrl modules/tadtools/sweet-alert/sweet-alert.js}>" type="text/javascript"></script> 
	<script type='text/javascript'>
	  $(document).ready(function(){
	    $('#sort').sortable({ opacity: 0.6, cursor: 'move', update: function() {
	      var order = $(this).sortable('serialize') + '&op=opUpdateSort';
	      $.post('main.php', order, function(msg){
	        if(msg ==1){
	          swal("<{$smarty.const._BP_SORT_SUCCESS}>", "", "success");
	          location.reload();
	        }else{
	          swal("<{$smarty.const._BP_SORT_ERROR}>", "", "error");
	          location.reload();
	        }              
	      });
	    }
	    });
	  });
	</script>
  <div class='container-fluid'>
  	<h2>商品排序</h2>
    <div class='row'>
      <div id="save_msg"></div>
      <table id="form_table" class="table table-bordered table-condensed table-hover">
        <thead>
          <tr>
            <th class="col-sm-1 text-center"><{$smarty.const._MD_UGMMODULE_THUMB}></th>
            <th class="col-sm-1 text-center"><{$smarty.const._MD_UGMMODULE_DATE}></th>
            <th class="col-sm-4 text-center"><{$smarty.const._MD_UGMMODULE_TITLE}></th>
            <th class="col-sm-1 text-center"><{$smarty.const._MD_UGMMODULE_SORT}></th>
            <th class="col-sm-1 text-center"><{$smarty.const._MD_UGMMODULE_CHOICE}></th>
            <th class="col-sm-1 text-center"><{$smarty.const._MD_UGMMODULE_ENABLE}></th>
            <th class="col-sm-2 text-center">
            	<button type="button" class="btn btn-warning btn-xs" onclick="location.href='<{$smarty.session.return_url}>'"><{$smarty.const._BACK}></button>
            </th>
          </tr>
        </thead>
        <{foreach from=$rows item=row key=id name=t}>
          <{if $smarty.foreach.t.first}>
            <tbody id='sort'>
          <{/if}>          
          <tr id='tr_<{$row.sn}>'>
            <td class="text-center"><{$row.prod}></td>
            <td class="text-center"><{$row.date}></td>
            <td class="text-center"><{$row.title}></td>
            <td class="text-center"><{$row.sort}></td>
            <td class="text-center">              
              <{if $row.choice}>
                <img src='<{$xoops_url}>/modules/ugm_tools2/images/on.png' />
              <{else}>
                <img src='<{$xoops_url}>/modules/ugm_tools2/images/off.png' />
              <{/if}>                
            </td>
            <td class="text-center">
              <{if $row.enable}>
                <img src='<{$xoops_url}>/modules/ugm_tools2/images/on.png' />
              <{else}>
                <img src='<{$xoops_url}>/modules/ugm_tools2/images/off.png' />
              <{/if}>                
            </td>
            <td class="text-center">
              <img src="<{$xoops_url}>/modules/tadtools/treeTable/images/updown_s.png" style="cursor: s-resize;" alt="<{$smarty.const._TAD_SORTABLE}>" title="<{$smarty.const._TAD_SORTABLE}>"> 
            </td>
          </tr>
          <{if $smarty.foreach.t.last}>            
            </tbody>
          <{/if}>
        <{/foreach}>
      </table>
      <{$bar}>
    </div>
  </div>
<{/if}>
	
 - 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']; }