網站程式設計-PHP
- function.php 加上
defined('WEB_PATH') || die("WEB_PATH root path not defined");
- 程式的流程,都是以列表為開始,故有需要將列表的網址存至 $_SESSION ,讓程式可以順利回到列表。
function.php
###############################################################################
# 取得目前網址
###############################################################################
if(!function_exists("getCurrentUrl")){
function getCurrentUrl(){
global $_SERVER;
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')=== FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING']?'?' . $_SERVER['QUERY_STRING']:"";
$currentUrl = $protocol . '://' . $host . $script . $params;
return $currentUrl;;
}
}
列表流程
//預設動作
default:
# ---- 目前網址 ----
$_SESSION['CurrentUrl']=getCurrentUrl();
$op="op_list";
op_list();
break;
當程式有需要回到列表時,只要轉向至 $_SESSION['CurrentUrl'] 即可
在顯示單筆的樣板
<div style="margin:10px 0;">
<button onclick="window.location.href='{$smarty.session.CurrentUrl}'" type="button" class="btn btn-warning">返回</button>
</div>
- 在單筆顯示增加一個編輯按鈕
<button onclick="window.location.href='?op=op_form&sn={$DBV.sn}'" type="button" class="btn btn-success">編輯</button>
- 刪除完轉向列表的$_SESSION['CurrentUrl']
- 列表樣板修改
{* 選單管理 列表 *}
{if $WEB.file_name == "nav_m.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='nav_m.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-3">標題</th>
<th class="text-center col-md-4">網址</th>
<th class="text-center col-md-1">排序</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>{$row.url}</td>
<td class="text-center">{$row.sort}</td>
<td class="text-center">{$row.target}</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}
-