Title1

Title2

Title3

17-1 kind.php

一、kind.php

<?php
require_once 'head.php';
#定義常用變數
$TBL['name']="show_kind";//資料表名稱
$TBL['kind']="kind_prod";//分類

#整理傳入變數
$op = isset($_REQUEST['op'])?$_REQUEST['op']:"";
$sn = isset($_REQUEST['sn'])?intval($_REQUEST['sn']):"";

#程式流程
switch($op){
  #新增記錄
  case "op_insert":
    $sn=op_insert();
    redirect_header("kind.php?op=op_show&sn={$sn}",3000,"新增資料成功!!");
    exit;
  break;
  #更新記錄
  case "op_update":
    $sn=op_update($sn);
    redirect_header("kind.php?op=op_show&sn={$sn}",3000,"更新資料成功!!");
    exit;
  break;
  #刪除記錄
  case "op_delete":
    op_delete($sn);
    redirect_header($_SESSION['CurrentUrl'],3000,"刪除記錄成功!!");
    exit;
  break;
  #表單
  case "op_form":
    op_form($sn);
  break;
  #顯示單筆
  case "op_show":
    op_show($sn);
  break;
  #列表
  default:
    # ---- 目前網址 ----
    $_SESSION['CurrentUrl']=getCurrentUrl();
    $op="op_list";
    op_list();
  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_insert()
{
  global $mysqli,$TBL;
  #資料過濾
  #http://php.net/manual/en/mysqli.real-escape-string.php
  $_POST['title']  = $mysqli->real_escape_string($_POST['title']);
  $_POST['enable'] = intval($_POST['enable']);
  //$_POST['sort']   = intval($_POST['sort']);

  $_POST['sort']   = get_max_sort_show_kind("sort",$TBL);//直接寫入最大值

  # nav_home => 首頁的選單
  $_POST['kind'] = $TBL['kind'];

  $sql = "insert into `{$TBL['name']}`
          (`title`,  `enable`, `sort`,`kind`)
          VALUES
          ('{$_POST['title']}', '{$_POST['enable']}', '{$_POST['sort']}', '{$_POST['kind']}')";
  $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));

  $sn=$mysqli->insert_id;//傳回insert 指令所產生之流水號

  return $sn;
}

########################################
# 更新記錄
########################################
function op_update($sn="")
{
  global $mysqli,$TBL;
  if(!$sn)redirect_header("index.php",3000,"更新記錄錯誤!!");

  #資料過濾
  $_POST['sn']   = intval($_POST['sn']);
  $_POST['title']  = $mysqli->real_escape_string($_POST['title']);
  $_POST['enable'] = intval($_POST['enable']);
  $_POST['sort']   = intval($_POST['sort']);
  $sql = "update `{$TBL['name']}` set
          `title`  = '{$_POST['title']}' ,
          `enable` = '{$_POST['enable']}',
          `sort` = '{$_POST['sort']}'
          where sn='{$_POST['sn']}'";
  $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));

  return $sn;
}

###############################################################################
#  刪除資料
###############################################################################
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;
}



########################################
#取得單筆記錄
########################################
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) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
  $DBV = $result->fetch_assoc();

  #過濾撈出資料
  $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'] = intval($DBV['enable']);
  $DBV['target'] = intval($DBV['target']);
  return $DBV;
}

########################################
# 表單
########################################
function op_form($sn="")
{
  global $mysqli,$smarty;

  #抓取預設值
  if($sn)
  {
    #編輯
    $DBV=get_show_kind($sn);//取得單筆記錄
    $DBV['op'] = "op_update";
    $DBV['form_title'] = "編輯類別";
    //print_r($DBV);die();
  }else
  {
    #新增
    $DBV=array();

    $DBV['op'] = "op_insert";
    $DBV['form_title'] = "新增類別";
  }

  //預設值設定
  //標題 title (text)、網址 url (text)、是否外連 target (radio)、是否啟用 enable (radio)、排序 sort(text)

  $DBV['sn']     = (isset($DBV['sn']))     ? $DBV['sn']     : "";
  $DBV['title']  = (isset($DBV['title']))  ? $DBV['title']  : "";
  $DBV['enable'] = (isset($DBV['enable'])) ? $DBV['enable'] : "1";
  $DBV['target'] = (isset($DBV['target'])) ? $DBV['target'] : "0";
  $DBV['url']    = (isset($DBV['url']))    ? $DBV['url']    : "";
  $DBV['sort']   = (isset($DBV['sort']))   ? $DBV['sort']   : "";


  $smarty->assign("DBV", $DBV);
  return;
}

########################################
# op_show
########################################
function op_show($sn="")
{
  global $mysqli,$smarty,$TBL;
  if(!$sn)redirect_header("index.php",3000,"查詢選單資料錯誤!!");

  #取得單筆記錄,可以寫成函數
  #sn ofsn  kind  title sort  enable  url target  col_sn  content
  $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) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));
  $DBV = $result->fetch_assoc();

  #過濾撈出資料
  $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;
}

########################################
# 列表
########################################
function op_list()
{
  global $mysqli,$smarty,$TBL;

  #取得所有記錄
  $sql = "select *
          from `{$TBL['name']}`
          where `kind`='{$TBL['kind']}'
          order by `sort` ";//die($sql);
  $result = $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate));


  $rows=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['sort'] = intval($row['sort']);
    $row['enable'] = $row['enable'] ? "是":"<span style='color:red;'>否</span>";

    $rows[]= $row;
  }

  $smarty->assign("DBV", $rows);
  return;
}

二、樣板

{* 類別管理 表單 *}
    {if $WEB.file_name == "kind.php" and $op == "op_form"}
      <div class="container" style="margin-top:20px;">
        <div style="margin:10px 0;">
          <button onclick="window.location.href='{$smarty.session.CurrentUrl}'" type="button" class="btn btn-warning">返回</button>
        </div>
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">{$DBV.form_title}</h3>
          </div>
          <div class="panel-body">
            <form action="kind.php" method="post">

              <div class="row">

                <div class="col-md-6">
                  <div class="form-group">
                    <label>標題</label>
                    <input type="text" class="form-control" id="title " name="title" value="{$DBV.title}" placeholder="標題">
                  </div>
                </div>

                <div class="col-md-2">
                  <div class="form-group">
                    <label style="display:block;">啟用狀態</label>

                    <input type='radio' name='enable' id='enable_1' value='1' {if $DBV.enable==1}checked{/if} >
                    <label for='enable_1'>是</label>&nbsp;&nbsp;

                    <input type='radio' name='enable' id='enable_0' value='0' {if $DBV.enable==0}checked{/if}>
                    <label for='enable_0'>否</label>

                  </div>
                </div>

                <div class="col-md-2">
                  <div class="form-group">
                    <label>排序</label>
                    <input type="text" class="form-control" id="sort"  name="sort"  value="{$DBV.sort}" placeholder="排序">
                  </div>
                </div>

              </div>


              <!--用來控制程式流程-->
              <input type="hidden" name="op" value="{$DBV.op}">
              <input type="hidden" name="sn" value="{$DBV.sn}">
              <button type="submit" class="btn btn-default">送出</button>
            </form>



          </div>
        </div>
      </div>

    {/if}

    {* 類別管理 顯示單筆 *}
    {if $WEB.file_name == "kind.php" and $op == "op_show"}
      <div class="container" style="margin-top:20px;">
        <div style="margin:10px 0;">
          <button onclick="window.location.href='{$smarty.session.CurrentUrl}'" type="button" class="btn btn-warning">返回</button>
          <button onclick="window.location.href='?op=op_form&sn={$DBV.sn}'" type="button" class="btn btn-success">編輯</button>
        </div>
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">顯示單筆</h3>
          </div>
          <div class="panel-body">

              <div class="row">
                <div class="col-md-6">
                  <div class="form-group">
                    <label>標題</label>
                    <div class="form-control">{$DBV.title}</div>
                  </div>
                </div>
                <div class="col-md-2">
                  <div class="form-group">
                    <label style="display:block;">啟用狀態</label>
                    <div class="form-control">{$DBV.enable}</div>
                  </div>
                </div>
                <div class="col-md-2">
                  <div class="form-group">
                    <label>排序</label>
                    <div class="form-control">{$DBV.sort}</div>
                  </div>
                </div>
              </div>

          </div>
        </div>
      </div>
    {/if}

   {* 類別管理 列表 *}
   {if $WEB.file_name == "kind.php" and $op == "op_list"}
     {literal}
        <link rel="stylesheet" href="../class/sweet-alert/sweet-alert.css" type="text/css" />
        <script src="../class/sweet-alert/sweet-alert.js" type="text/javascript"></script>
        <script type="text/javascript">
          function op_delete_js(sn){
            swal({
              title: '確定要刪除此資料?',
              text: '相關資料通通都將會被移除!',
              type: 'warning',
              showCancelButton: 1,
              confirmButtonColor: '#DD6B55',
              confirmButtonText: '確定刪除!',
              closeOnConfirm: false ,
              allowOutsideClick: true
            },
            function(){
              location.href='kind.php?op=op_delete&sn=' + sn;
            });
          }
        </script>
      {/literal}
      <div class="container" style="margin-top:20px;">
        <h2 class="text-center">類別管理</h2>
        <table class="table table-bordered table-hover list-table">
          <thead>
            <tr class="active">
              <th class="text-center col-md-8">標題</th>
              <th class="text-center col-md-1">排序</th>
              <th class="text-center col-md-1">啟用</th>
              <th class="text-center col-md-2">
                <button onclick="window.location.href='?op=op_form'" type="button" class="btn btn-primary btn-xs">新增</button>
              </th>
            </tr>
          </thead>
          <tbody>
            {foreach from=$DBV item=row}
              <tr>
                <td>{$row.title}</td>
                <td class="text-center">{$row.sort}</td>
                <td class="text-center">{$row.enable}</td>
                <td class="text-center">
                  <button onclick="window.location.href='?op=op_show&sn={$row.sn}'" type="button" class="btn btn-warning btn-xs">瀏覽</button>
                  <button onclick="window.location.href='?op=op_form&sn={$row.sn}'" type="button" class="btn btn-success btn-xs">編輯</button>
                  <button onclick="javascript:op_delete_js({$row.sn});" type="button" class="btn btn-danger btn-xs">刪除</button>
                </td>
              </tr>
            {/foreach}
          </tbody>
        </table>
      </div>

   {/if}