Title1

Title2

Title3

13-2 整理後台nav_m.php 過程

  1. 定義常用變數
    
    #定義常用變數
    $TBL['name']="show_kind";//資料表名稱
    $TBL['kind']="nav_home";//分類

     

  2. 新增記錄
    
    ########################################
    # 新增記錄
    ########################################
    function op_insert()
    {
      global $mysqli,$TBL;
      #資料過濾
      #http://php.net/manual/en/mysqli.real-escape-string.php
      $_POST['title']  = $mysqli->real_escape_string($_POST['title']);//字串
      $_POST['target'] = intval($_POST['target']);//數字
      $_POST['enable'] = intval($_POST['enable']);
      $_POST['sort']   = intval($_POST['sort']);
      $_POST['url']    = $mysqli->real_escape_string($_POST['url']);
    
      # nav_home => 首頁的選單
      $_POST['kind'] = $TBL['kind'];
    
      $sql = "insert into `{$TBL['name']}`
              (`title`, `target`, `enable`, `sort`,`url`,`kind`)
              VALUES
              ('{$_POST['title']}', '{$_POST['target']}', '{$_POST['enable']}', '{$_POST['sort']}', '{$_POST['url']}', '{$_POST['kind']}')";
      $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    
      $sn=$mysqli->insert_id;//傳回insert 指令所產生之流水號
    
      return $sn;//傳回 $sn
    }

     

  3. 更新記錄
    
    ########################################
    # 更新記錄
    ########################################
    function op_update($sn="")
    {
      global $mysqli,$TBL;
      if(!$sn)redirect_header("index.php",3000,"更新記錄錯誤!!");
    
      #資料過濾
      $_POST['title']  = $mysqli->real_escape_string($_POST['title']);
      $_POST['target'] = intval($_POST['target']);
      $_POST['enable'] = intval($_POST['enable']);
      $_POST['sort']   = intval($_POST['sort']);
      $_POST['url']    = $mysqli->real_escape_string($_POST['url']);
    
      $sql = "update `{$TBL['name']}` set
              `title`  = '{$_POST['title']}' ,
              `target` = '{$_POST['target']}',
              `enable` = '{$_POST['enable']}',
              `url` = '{$_POST['url']}',
              `sort` = '{$_POST['sort']}'
              where sn='{$_POST['sn']}'";
      $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    
      return;
    }

     

  4. 刪除資料
    
    ###############################################################################
    #  刪除資料
    ###############################################################################
    function op_delete($sn="")
    {
      global $mysqli,$TBL;
      if(!$sn)redirect_header("index.php",3000,"刪除記錄錯誤!!");
    
      #檢查
      $sql = "delete
              from `{$TBL['name']}`
              where `sn`='{$sn}'";//die($sql);
      $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    
      return;
    }

     

  5. 取得單筆記錄
    
    ########################################
    # 取得單筆記錄
    ########################################
    function get_show_kind($sn="")
    {
      global $mysqli,$TBL;
      if(!$sn)redirect_header("index.php",3000,"查詢選單資料錯誤!!");
    
      #取得單筆記錄,可以寫成函數
      $sql = "select *
              from `{$TBL['name']}`
              where `sn`='{$sn}' and `kind`='{$TBL['kind']}'";
      $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
      $result = $mysqli->query($sql);
      $DBV = $result->fetch_assoc();
      return $DBV;
    }
    

     

  6. 顯示單筆
    
    ########################################
    # 顯示單筆
    ########################################
    function op_show($sn="")
    {
      global $mysqli,$smarty;
      if(!$sn)redirect_header("index.php",3000,"查詢選單資料錯誤!!");
    
    
      $DBV=get_show_kind($sn);//取得單筆記錄
    
      #過濾撈出資料
      $DBV['sn'] = intval($DBV['sn']);
      //http://www.w3school.com.cn/php/func_string_htmlspecialchars.asp
      $DBV['title'] = htmlspecialchars($DBV['title'], ENT_QUOTES); // 轉換雙引號和單引號
      $DBV['url'] = htmlspecialchars($DBV['url'], ENT_QUOTES); // 轉換雙引號和單引號
      $DBV['sort'] = intval($DBV['sort']);
      $DBV['enable'] = $DBV['enable'] ? "是":"<span style='color:red;'>否</span>";
      $DBV['target'] = $DBV['target'] ? "是":"<span style='color:red;'>否</span>";
    
      $smarty->assign("DBV", $DBV);
      return;
    }

     

  7. 列表
    
    ########################################
    # 列表
    ########################################
    function op_list()
    {
      global $mysqli,$smarty,$TBL;
    
      #取得所有記錄
      $sql = "select *
              from `{$TBL['name']}`
              where `kind`='{$TBL['kind']}'
              order by `sort` ";
      $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
      $result = $mysqli->query($sql);
    
    
      $DBV=array();
      while($row = $result->fetch_assoc())
      {
        #過濾撈出資料
        $row['sn'] = intval($row['sn']);
        //http://www.w3school.com.cn/php/func_string_htmlspecialchars.asp
        $row['title'] = htmlspecialchars($row['title'], ENT_QUOTES); // 轉換雙引號和單引號
        $row['url'] = htmlspecialchars($row['url'], ENT_QUOTES); // 轉換雙引號和單引號
        $row['sort'] = intval($row['sort']);
        $row['enable'] = $row['enable'] ? "是":"<span style='color:red;'>否</span>";
        $row['target'] = $row['target'] ? "是":"<span style='color:red;'>否</span>";
    
        $DBV[]= $row;
      }
    
      $smarty->assign("DBV", $DBV);
      return;
    }

     

  8. 取得選單最大值 function.php
    
    #####################################################################################
    #  自動取得(排序欄位,資料表)的最新排序
    #  get_max_sort_show_kind($col,$TBL)
    #  (排序欄位,資料表)#
    #####################################################################################
    if(!function_exists("get_max_sort_show_kind")){
    function get_max_sort_show_kind($col="sort",$TBL=""){
      global $mysqli;
      if(empty($col) or empty($TBL))return;
      $sql = "select max({$col})
              from `{$TBL['name']}`
              where kind='{$TBL['kind']}'";
       $result = $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
      
      list($sort)=$result->fetch_row();
      return ++$sort;
    }
    }

     

  9. 修改 op_insert()
    
    ########################################
    # 新增記錄
    ########################################
    function op_insert()
    {
      global $mysqli,$TBL;
      #資料過濾
      #http://php.net/manual/en/mysqli.real-escape-string.php
      $_POST['title']  = $mysqli->real_escape_string($_POST['title']);//字串
      $_POST['target'] = intval($_POST['target']);//數字
      $_POST['enable'] = intval($_POST['enable']);
      //$_POST['sort']   = intval($_POST['sort']);
      $_POST['url']    = $mysqli->real_escape_string($_POST['url']);
    
      $_POST['sort']   = get_max_sort_show_kind("sort",$TBL);//直接寫入最大值
    
      # nav_home => 首頁的選單
      $_POST['kind'] = $TBL['kind'];
    
      $sql = "insert into `{$TBL['name']}`
              (`title`, `target`, `enable`, `sort`,`url`,`kind`)
              VALUES
              ('{$_POST['title']}', '{$_POST['target']}', '{$_POST['enable']}', '{$_POST['sort']}', '{$_POST['url']}', '{$_POST['kind']}')";
      $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
    
      $sn=$mysqli->insert_id;//傳回insert 指令所產生之流水號
    
      return $sn;//傳回 $sn
    }