網站程式設計-PHP
一、流程圖
二、SQL 語法
- 新增
#################################
# 新增資料
#
#################################
function op_insert() {
global $mysqli;
#資料過濾
#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['kind'] = $mysqli->real_escape_string($_POST['kind']);
$sql = "insert into `creative_nav`
(`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;
}
- 編輯
#################################
# 更新資料
#
#################################
function op_update($sn = "") {
global $mysqli;
if (!$sn) {
redirect_header("index.php", 3000, "更新記錄錯誤!!");
}
#資料過濾
$_POST['sn'] = intval($_POST['sn']);
$_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 `creative_nav` 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 $sn;
}
- 刪除
#################################
# 刪除資料
#
#################################
function op_delete($sn = "") {
global $mysqli;
if (!$sn) {
redirect_header("index.php", 3000, "刪除記錄錯誤!!");
}
#
$sql = "delete
from `creative_nav`
where `sn`='{$sn}'"; //die($sql);
$mysqli->query($sql) or die(printf("Error: %s <br>" . $sql, $mysqli->sqlstate));
return;
}
- 列表
#################################
# 列表程式
#
#################################
function op_list() {
global $mysqli, $smarty;
#取得所有記錄
$sql = "select *
from `creative_nav`
where `kind`='nav_home'
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['url'] = htmlspecialchars($row['url'], ENT_QUOTES); // 轉換雙引號和單引號
$row['sort'] = intval($row['sort']);
$row['enable'] = intval($row['enable']);
$row['target'] = intval($row['target']);
$rows[] = $row;
}
$smarty->assign("rows", $rows);
return;
}
- 取得單筆記錄
########################################
#取得單筆記錄
########################################
function get_creative_nav($sn = "") {
global $mysqli;
if (!$sn) {
redirect_header("index.php", 3000, "查詢選單資料錯誤!!");
}
$sql = "select *
from `creative_nav`
where `sn`='{$sn}' and `kind`= 'nav_home'";
$result = $mysqli->query($sql) or die(printf("Error: %s <br>" . $sql, $mysqli->sqlstate));
$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'] = intval($row['enable']);
$row['target'] = intval($row['target']);
return $row;
}
三、樣板
- theme.html
<{if $WEB.file_name =="index.php"}>
<{include file="tpl/admin_index.html"}>
<{/if}>
- tpl/admin_index.html 列表
<{if $op == "op_list"}>
<link rel="stylesheet" href="<{$smarty.const.WEB_URL}>/class/sweetalert/sweetalert.css" type="text/css">
<script src="<{$smarty.const.WEB_URL}>/class/sweetalert/sweetalert.min.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="?op=op_delete&sn="+sn;
});
}
</script>
<div class="panel panel-default">
<div class="panel-heading">選單管理 - 列表</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="active">
<th class="col-md-3 text-center">標題</th>
<th class="col-md-3 text-center">網址</th>
<th class="col-md-1 text-center">外連</th>
<th class="col-md-1 text-center">狀態</th>
<th class="col-md-1 text-center"><a href="?op=op_form" class="btn btn-xs btn-primary">新增</a></th>
</tr>
</thead>
<tbody>
<{foreach from=$rows item=row}>
<tr>
<td class="text-center"><{$row.title}></td>
<td><{$row.url}></td>
<td class="text-center">
<{if $row.target == 1}>
<img src="<{$smarty.const.WEB_URL}>/images/yes.gif">
<{else}>
<img src="<{$smarty.const.WEB_URL}>/images/no.gif">
<{/if}>
</td>
<td class="text-center">
<{if $row.enable == 1}>
<img src="<{$smarty.const.WEB_URL}>/images/yes.gif">
<{else}>
<img src="<{$smarty.const.WEB_URL}>/images/no.gif">
<{/if}>
</td>
<td class="text-center">
<a href="?op=op_form&sn=<{$row.sn}>" class="btn btn-xs btn-success">
編輯
</a>
<a href="javascript:op_delete_js(<{$row.sn}>);" class="btn btn-xs btn-danger">
刪除
</a>
</td>
</tr>
<{/foreach}>
</tbody>
</table>
</div>
<!-- /.table-responsive -->
</div>
<!-- /.panel-body -->
</div>
<{/if}>
- admin/admin_index.html 表單
<{if $op == "op_form"}>
<div class="panel panel-primary">
<div class="panel-heading"><h3 class="panel-title">選單管理 - <{$row.form_title}></h3></div>
<div class="panel-body">
<form role="form" action="index.php" method="post" id="myForm">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>標題</label>
<input type="text" name="title" id="title" class="form-control" value="<{$row.title}>" placeholder="標題" >
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>網址</label>
<input type="text" name="url" id="url" class="form-control" value="<{$row.url}>" placeholder="網址">
</div>
</div>
<div class="col-md-1">
<div class="form-group">
<label>排序</label>
<input type="text" name="sort" id="sort" class="form-control" value="<{$row.sort}>">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label style="display:block;">外連狀態</label>
<input type="radio" name="target" id="target_1" value="1" <{if $row.target == 1}>checked<{/if}>>
<label for="target_1">外連</label>
<input type="radio" name="target" id="target_0" value="0" <{if $row.target == 0}>checked<{/if}>>
<label for="target_0">本站</label>
</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 $row.enable == 1}>checked<{/if}>>
<label for="enable_1">啟用</label>
<input type="radio" name="enable" id="enable_0" value="0" <{if $row.enable == 0}>checked<{/if}>>
<label for="enable_0">停用</label>
</div>
</div>
</div>
<hr>
<div class="text-center">
<input type="hidden" name="op" value="<{$row.op}>">
<input type="hidden" name="sn" value="<{$row.sn}>">
<input type="hidden" name="kind" value="<{$row.kind}>">
<button type="submit" class="btn btn-primary">送出</button>
<button type="reset" class="btn btn-danger">重設</button>
<button type="button" class="btn btn-warning" onclick="location.href='<{$smarty.session.op_list}>'">返回</button>
</div>
</form>
</div>
</div>
<{/if}>