網站程式設計-PHP
http://campus-xoops.tn.edu.tw/modules/tad_book3/index.php?op=list_docs&tbsn=17
一、kind.php
- 複製 nav_m.php 成 kind.php
 - 用 ctrl + h => nav_m.php 取代成 kind.php
 - $TBL['kind']="nav_home";//分類 改成 $TBL['kind']="kind_prod";//分類
 - 將 templates/admin/theme.html 其中的 nav_m 的部份複制,貼至新檔
 - 將 nav_m.php 取代成 kind.php 、 選單 取代成 類別
 - 然後再貼回 templates/admin/theme.html
 - 後台選單 nav.php 增加一項
	
        <li><a href='kind.php'>商品類別管理</a></li>
	
 - 處理取消「網址」、「外連」
	
- op_list()程式
 - op_list 樣板
 - op_form()、op_form樣板
 - op_insert()
 - op_show()、op_show樣板
 - op_update()
 
 
二、增加商品資料表
- 商品展示資料表 結構
	
      CREATE TABLE `show_prod` (
        `sn` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'prod_sn',
        `kind` smallint(5) unsigned NOT NULL default 0 COMMENT '分類',
        `title` varchar(255) NOT NULL default '' COMMENT '名稱',
        `summary` text NOT NULL default '' COMMENT '摘要',
        `content` text NOT NULL default '' COMMENT '內容',
        `price` int(10) unsigned NOT NULL default 0 COMMENT '價格',
        `enable` enum('1','0') NOT NULL DEFAULT '1' COMMENT '狀態',
        `date` int(10) unsigned NOT NULL default 0 COMMENT '建立日期',
        `sort` smallint(5) unsigned NOT NULL default 0 COMMENT '排序',
        `counter` int(10) unsigned NOT NULL default 0 COMMENT '人氣',
        PRIMARY KEY (`sn`)
      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
	
 - 自動更新,update.php
	
<?php
require_once 'head.php';
#整理傳入變數
$op = isset($_REQUEST['op'])?$_REQUEST['op']:"";
$sn = isset($_REQUEST['sn'])?intval($_REQUEST['sn']):"";
#程式流程
switch($op){
  #顯示單筆
  case "op_show":
    die($op);
  break;
  #更新
  default:
    $op="op_list";
    $msg=op_list();
    if($msg)
    {
      redirect_header("index.php",3000,"更新系統成功!!");
    } else
    {
      redirect_header("index.php",3000,"更新系統失敗!!");
    }
  break;
}
#將變數送至樣板引擎
#op
$smarty->assign("op", $op);
/*
$WEB['theme_name'] = "admin";
WEB['title'] = "網站名稱";
$WEB['file_name'] = basename ($_SERVER['PHP_SELF']);
*/
#變數在head.php
$smarty->assign("WEB", $WEB);
#程式結尾
$smarty->display('theme.html');
#函數
########################################
# 更新主程式
########################################
function op_list()
{
  global $mysqli;
  #檢查資料夾
  mk_dir(WEB_PATH."/uploads");
  mk_dir(WEB_PATH."/uploads/slider");
  //-------- 資料表 ------
  #檢查資料表(show_kind)
  if(!chk_isTable("show_kind")) go_update1();
  #檢查資料表(show_files)
  if(!chk_isTable("show_files")) go_update2();
  #檢查資料表(show_prod)
  if(!chk_isTable("show_prod")) go_update3();
  //-------- 資料表 end------
  //-------- 欄位 ------
  //if(!chk_isColumn("sn1","show_kind")) go_update3();
  //-------- 欄位 end------
  return true;
}
########################################
# 建立資料表 show_kind
########################################
function go_update1()
{
    global $mysqli;
    $sql="
      CREATE TABLE `show_kind` (
        `sn` smallint(5) unsigned not null auto_increment comment 'sn',
        `ofsn` smallint(5) unsigned not null default 0 comment '父類別',
        `kind` varchar(255) not null default 'nav_home' comment '分類',
        `title` varchar(255) not null  default '' comment '標題',
        `sort` smallint(5) unsigned not null default 0 comment '排序',
        `enable` enum('1','0') not null default '1' comment '狀態',
        `url` varchar(255) not null default '' comment '網址',
        `target` enum('0','1') not null default '0' comment '外連',
        `col_sn` int(10) unsigned not null default 0 comment 'col_sn',
        `content` text not null default '' comment '內容',
        PRIMARY KEY (`sn`)
      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    ";
    $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    return true;
}
########################################
# 建立資料表show_files
########################################
function go_update2()
{
    global $mysqli;
    $sql="
      CREATE TABLE `show_files` (
        `sn` smallint(5) unsigned not null auto_increment comment 'sn',
        `col_name` varchar(255) not null default '' comment '欄位名稱',
        `col_sn` smallint(5) unsigned not null default 0 comment '欄位編號',
        `sort` smallint(5) unsigned not null default 0 comment '排序',
        `kind` enum('img','file') not null default 'img' comment '檔案種類',
        `file_name` varchar(255) not null default '' comment '檔案名稱',
        `file_type` varchar(255) not null default '' comment '檔案類型',
        `file_size` int(10) unsigned not null default 0 comment '檔案大小',
        `description` text not null default '' comment '檔案說明',
        `counter` mediumint(8) unsigned not null default 0 comment '下載人次',
        `sub_dir` varchar(255) not null default '' comment '檔案子路徑',
        PRIMARY KEY (`sn`)
      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    ";//die($sql);
    $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    return true;
}
########################################
# 建立資料表show_prod
########################################
function go_update3()
{
    global $mysqli;
    $sql="
      CREATE TABLE `show_prod` (
        `sn` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'prod_sn',
        `kind` smallint(5) unsigned NOT NULL default 0 COMMENT '分類',
        `title` varchar(255) NOT NULL default '' COMMENT '名稱',
        `summary` text NOT NULL default '' COMMENT '摘要',
        `content` text NOT NULL default '' COMMENT '內容',
        `price` int(10) unsigned NOT NULL default 0 COMMENT '價格',
        `enable` enum('1','0') NOT NULL DEFAULT '1' COMMENT '狀態',
        `date` int(10) unsigned NOT NULL default 0 COMMENT '建立日期',
        `sort` smallint(5) unsigned NOT NULL default 0 COMMENT '排序',
        `counter` int(10) unsigned NOT NULL default 0 COMMENT '人氣',
        PRIMARY KEY (`sn`)
      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    ";//die($sql);
    $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    return true;
}
########################################
# 在資料表show_kind 增加一個 sn1 欄位
########################################
// function go_update3()
// {
//     global $mysqli;
//     $sql="ALTER TABLE `show_kind` ADD `sn1` smallint(5) unsigned NOT NULL default 0 comment 'sn1'";//die($sql);
//     $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
//     return true;
// }
########################################
# 檢查某欄位是否存在(欄名,資料表)
########################################
function chk_isColumn($col_name="",$tbl_name="")
{
    global $mysqli;
    if(!$col_name and $tbl_name)return;
    //SHOW COLUMNS FROM `show_kind` LIKE 'sn1'
    $sql    = "SHOW COLUMNS FROM `{$tbl_name}` LIKE '{$col_name}'";
    $result = $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    if ($result->num_rows)return true; //欄位存在
    return false;//欄位不存在
}
########################################
# 檢查資料表是否存在(資料表)
########################################
function chk_isTable($tbl_name="")
{
    global $mysqli;
    if(!$tbl_name)return;
    $sql    = "SHOW TABLES LIKE '{$tbl_name}'";//die($sql);
    $result = $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    if ($result->num_rows)return true; //欄位存在
    return false;//欄位不存在
}