1. PHP入門 2. 使用者輸入介面設計 2-1 上課範例:index.php 2-2 上課範例:save.php 3. PHP變數與常數 3-1 上課範例:a1.php 4. PHP資料類型與建立資料表 4-1 上課範例:add_class.php 5. 連線資料庫及運算元 5-1 上課範例:save_class.php 5-1 上課範例:add_class.php 6. 讀取資料庫及PHP陣列 6-1 上課範例:add_class.php 6-2 上課範例:save_class.php 6-3 上課範例:index.php 7. 編輯與刪除功能 7-1 上課範例:index.php 7-2 上課範例:add_class.php 7-3 上課範例:save_class.php 8. 函數與小強佈景 8-1 上課範例:index.php 8-2 上課範例:add_class.php 8-3 上課範例:save_class.php 9. 流程控制與迴圈 9-1 上課範例:add_class.php 9-2 上課範例:theme.html 樣板檔 9-3 上課範例:save_class.php 9-4 上課範例:index.php 9-5 上課範例:header.php 9-6 上課範例:function.php 10. 練習練習再練習! 10-1 上課範例:index.php 10-2 上課範例:add_class.php 10-3 上課範例:save_class.php 10-4 上課範例:index.php(整合版) 10-5 上課範例:header.php(整合版) 11. 建立學習收藏的發布頁 11-1 上課範例:class.php 11-2 上課範例:header.php 11-3 上課範例:tad0616.sql 11-4 上課範例:blank.php 空白樣板 11-5 上課範例:index.php 12. 兩個資料表的結合 12-1 index.php 13. 資料檢查 13-1 上課範例:index.php 13-2 上課範例:header.php 13-3 上課範例:function.php 13-4 上課範例:class.php 14. 請完成基本功能囉~ 14-1 上課範例:index.php(完成版) 15. 認證與session 15-1 上課範例:class.php 15-2 上課範例:config.php 15-3 上課範例:header.php 15-4 上課範例:index.php 16. 分頁與編輯器 16-1 上課範例:index.php 16-2 上課範例:header.php 17. 用PHP上傳檔案 17-1 上課範例:index.php 18. 資料搜尋 18-1 上課範例:index.php 18-2 上課範例:theme.html
17-1
上課範例:index.php
003
require_once
"header.php"
;
006
$op
=isset(
$_REQUEST
[
'op'
])?
$_REQUEST
[
'op'
]:
""
;
007
$sn
= isset(
$_REQUEST
[
'sn'
])?
intval
(
$_REQUEST
[
'sn'
]) :
""
;
013
$main
= (
$_SESSION
[
'isLeader'
]) ? list_article(
$_SESSION
[
'isLeader'
]) : login_form();
017
leader_login(
$_POST
[
'class_sn'
],
$_POST
[
'pass'
]);
018
header(
"location:index.php?op=admin"
);
022
$_SESSION
[
'isLeader'
]=null;
023
header(
"location:index.php"
);
028
header(
"location:{$_SERVER['PHP_SELF']}"
);
032
$main
=article_form(
$sn
);
038
header(
"location:{$_SERVER['PHP_SELF']}"
);
043
header(
"location:{$_SERVER['PHP_SELF']}"
);
047
$main
=
empty
(
$sn
)?list_article():show_article(
$sn
);
057
function
login_form(){
058
$now_seme
=get_seme();
059
$sql
=
"select class_sn,class_name from tncomu_class where access='1' and seme='{$now_seme}' "
;
060
$result
=mysql_query(
$sql
)
or
die
(
$sql
);
062
while
(list(
$class_sn
,
$class_name
) = mysql_fetch_row(
$result
)){
063
$opt
.=
"<option value='$class_sn'>$class_name</option>"
;
067
<form action=
'{$_SERVER['
PHP_SELF
']}'
method=
'post'
>
068
<select name=
'class_sn'
>
072
密碼:<input type=
'password'
name=
'pass'
>
073
<input type=
'hidden'
name=
'op'
value=
'login'
>
074
<input type=
'submit'
value=
'登入'
>
080
function
leader_login(
$class_sn
=
''
,
$pass
=
''
){
081
if
(
empty
(
$class_sn
)
or
empty
(
$pass
))
return
;
084
$sql
=
"select passwd from `tncomu_class` where class_sn='{$class_sn}'"
;
085
$result
=mysql_query(
$sql
)
or
die
(
"無法執行:"
.mysql_error());
086
list(
$passwd
)=mysql_fetch_row(
$result
);
089
$_SESSION
[
'isLeader'
]=
$class_sn
;
095
function
show_article(
$sn
=null){
096
$now_seme
=get_seme();
098
$sql
=
"update `tncomu_article` set `counter`=`counter`+1 where sn='$sn'"
;
099
mysql_query(
$sql
)
or
die
(
"無法執行:"
.mysql_error());
102
$sql
="select a.* , b.class_name
103
from `tncomu_article`
as
a left join `tncomu_class`
as
b on a.class_sn=b.class_sn
104
where a.enable=
'1'
and
a.sn=
'$sn'
";
107
$result
= mysql_query(
$sql
)
or
die
(
"無法執行:"
.mysql_error());
109
$data
=mysql_fetch_assoc(
$result
);
111
$data
[
'content'
]=(
$data
[
'mode'
]==
"圖片"
)?
"<img src='pic/{$data['content']}'>"
:
$data
[
'content'
];
114
<h1>「{
$data
[
'stud_name'
]}」的學習收藏</h1>
115
<div style=
'text-align:right;margin:10px 0px;'
>{
$data
[
'class_name'
]}</div>
116
<div>{
$data
[
'content'
]}</div>
117
<div style=
'text-align:right;margin:10px 0px;'
>{
$data
[
'post_time'
]}</div>
125
function
list_article(
$class_sn
=null){
126
require_once
"pagebar.php"
;
127
$now_seme
=get_seme();
129
$and_class_sn
=
empty
(
$class_sn
)?
""
:
"and a.class_sn='$class_sn'"
;
132
$sql
="select a.* , b.class_name
133
from `tncomu_article`
as
a left join `tncomu_class`
as
b on a.class_sn=b.class_sn
134
where a.enable=
'1'
and
b.seme=
'$now_seme'
$and_class_sn
135
order by a.post_time desc";
139
$total
=mysql_affected_rows();
140
$navbar
=
new
PageBar(
$total
, 5, 10);
141
$mybar
=
$navbar
->makeBar();
142
$bar
=
"<p align='center'>{$mybar['left']}{$mybar['center']}{$mybar['right']}</p>"
;
147
$result
= mysql_query(
$sql
)
or
die
(
"無法執行:"
.mysql_error());
150
if
(
$_SESSION
[
'isLeader'
]){
153
function
delete_func(sn){
154
var
sure = window.confirm(
'確定要刪除此資料?'
);
156
location.href=
'{$_SERVER['
PHP_SELF
']}?op=delete&sn='
+ sn;
175
while
(
$data
=mysql_fetch_assoc(
$result
)){
177
$color
=(
$i
% 2)?
"white"
:
"#D0D0D0"
;
180
$tool
=(
$_SESSION
[
'isLeader'
]==
$data
[
'class_sn'
]
and
!
empty
(
$_SESSION
[
'isLeader'
]))?
"| <a href='javascript:delete_func({$data['sn']})'>刪除</a>"
:
""
;
183
<tr style=
'background-color:$color;'
>
184
<td>{
$data
[
'class_name'
]}</td>
185
<td><a href=
'{$_SERVER['
PHP_SELF
']}?sn={$data['
sn
']}'
>{
$data
[
'stud_name'
]}</a></td>
186
<td>{
$data
[
'post_time'
]}</td>
187
<td>{
$data
[
'counter'
]}</td>
188
<td><a href =
'{$_SERVER['
PHP_SELF
']}?sn={$data['
sn
']}&op=edit'
>編輯</a>{
$tool
}</td>
199
function
article_form(
$sn
=
''
){
204
$data
[
'stud_name'
] =
$data
[
'class_sn'
] =
$data
[
'content'
] =
$data
[
'enable'
] =
$radio1
=
$radio0
=
""
;
208
$sql
=
"select * from `tncomu_article` where sn='{$sn}'"
;
211
$result
=mysql_query(
$sql
)
or
die
(
"無法執行:"
.mysql_error());
214
$data
=mysql_fetch_assoc(
$result
);
218
$radio1
=(
$data
[
'enable'
]==
"1"
)?
"checked"
:
""
;
219
$radio0
=(
$data
[
'enable'
]==
"0"
)?
"checked"
:
""
;
224
$now_seme
=get_seme();
226
$sql
=
"select class_sn,class_name from tncomu_class where access='1' and seme='{$now_seme}' "
;
227
$result
=mysql_query(
$sql
)
or
die
(
$sql
);
229
while
(list(
$class_sn
,
$class_name
) = mysql_fetch_row(
$result
)){
230
$selected
= (
$class_sn
==
$data
[
'class_sn'
])?
"selected"
:
""
;
231
$opt
.=
"<option value='$class_sn' $selected>$class_name</option>"
;
235
$main
="<h3 style=
'color:#0066CC'
>輸入學習收藏</h3>
236
<script type=
'text/javascript'
src=
'ckeditor/ckeditor.js'
></script>
237
<form action=
'{$_SERVER['
PHP_SELF
']}'
method=
'post'
enctype=
'multipart/form-data'
>
241
<td><input type=
'text'
name=
'stud_name'
size=
'10'
value=
'{$data['
stud_name
']}'
></td>
242
<th>{
$now_seme
}班級:</th>
244
<select name=
'class_sn'
>
245
<option value=
''
>請選擇{
$now_seme
}班級</option>
251
<td colspan=4><textarea name=
'content'
class
=
'ckeditor'
cols=50 rows=8>{
$data
[
'content'
]}</textarea></td>
256
<td colspan=3><input type=
'file'
name=
'pic'
accept=
'image/*'
></td>
261
<td><input type=
'text'
name=
'text_passwd'
size=
'10'
></td>
264
<input type=
'radio'
name=
'enable'
value=
'1'
id=
'enable'
$radio1
><label
for
=
'enable'
>發布</label>
265
<input type=
'radio'
name=
'enable'
value=
'0'
id=
'unable'
$radio0
><label
for
=
'unable'
>暫不發布</label>
266
<input type=
'hidden'
name=
'sn'
value=
'$sn'
>
267
<input type=
'hidden'
name=
'op'
value=
'$next_op'
>
268
<input type=
'submit'
value=
'儲存'
>
279
function
insert_article(){
281
$stud_name
=trim(
$_POST
[
'stud_name'
]);
282
$stud_name
=
strip_tags
(
$stud_name
);
283
$stud_name
= (! get_magic_quotes_gpc()) ?
addslashes
(
$stud_name
) :
$stud_name
;
284
$stud_name
=htmlspecialchars(
$stud_name
);
286
$_POST
[
'content'
] = (! get_magic_quotes_gpc()) ?
addslashes
(
$_POST
[
'content'
]) :
$_POST
[
'content'
];
287
$_POST
[
'content'
]=htmlspecialchars(
$_POST
[
'content'
]);
289
$_POST
[
'text_passwd'
] = (! get_magic_quotes_gpc()) ?
addslashes
(
$_POST
[
'text_passwd'
]) :
$_POST
[
'text_passwd'
];
291
$class_sn
=
intval
(
$_POST
[
'class_sn'
]);
293
$sql
=
"INSERT INTO `tncomu_article`(`stud_name`, `content`, `post_time`, `enable`, `class_sn`, `mode`, `text_passwd`) VALUES ('{$stud_name}' , '{$_POST['content']}' , now(), '{$_POST['enable']}', '{$class_sn}', '文字', '{$_POST['text_passwd']}')"
;
294
mysql_query(
$sql
)
or
die
(mysql_error().
$sql
);
296
$sn
=mysql_insert_id();
298
if
(
$_FILES
[
'pic'
][
'name'
]){
299
$ext
=
strtolower
(
strrchr
(
$_FILES
[
'pic'
][
'name'
],
"."
));
300
move_uploaded_file(
$_FILES
[
'pic'
][
'tmp_name'
],
"pic/{$sn}{$ext}"
);
302
$sql
=
"update `tncomu_article` set `mode`='圖片',content='{$sn}{$ext}' where `sn`='{$sn}'"
;
303
mysql_query(
$sql
)
or
die
(mysql_error().
$sql
);
309
function
update_article(
$sn
=
''
){
313
$sql
=
"select text_passwd from `tncomu_article` where sn='{$sn}'"
;
315
$result
=mysql_query(
$sql
)
or
die
(
"無法執行:"
.mysql_error());
317
list(
$text_passwd
)=mysql_fetch_row(
$result
);
319
if
(
$text_passwd
!=
$_POST
[
'text_passwd'
]
or
empty
(
$_POST
[
'text_passwd'
])){
325
$stud_name
=trim(
$_POST
[
'stud_name'
]);
326
$stud_name
=
strip_tags
(
$stud_name
);
327
$stud_name
= (! get_magic_quotes_gpc()) ?
addslashes
(
$stud_name
) :
$stud_name
;
328
$stud_name
=htmlspecialchars(
$stud_name
);
330
$_POST
[
'content'
] = (! get_magic_quotes_gpc()) ?
addslashes
(
$_POST
[
'content'
]) :
$_POST
[
'content'
];
331
$_POST
[
'content'
]=htmlspecialchars(
$_POST
[
'content'
]);
333
$_POST
[
'text_passwd'
] = (! get_magic_quotes_gpc()) ?
addslashes
(
$_POST
[
'text_passwd'
]) :
$_POST
[
'text_passwd'
];
335
$class_sn
=
intval
(
$_POST
[
'class_sn'
]);
337
$sql
=
"update `tncomu_article` set `stud_name`='{$stud_name}', `content`='{$_POST['content']}', `post_time`=now(), `enable`='{$_POST['enable']}', `class_sn`='{$_POST['class_sn']}' where `sn`='{$sn}'"
;
338
mysql_query(
$sql
)
or
die
(mysql_error().
$sql
);
343
function
delete_article(
$sn
=null){
346
$sql
=
"delete from `tncomu_article` where sn='{$sn}'"
;
349
mysql_query(
$sql
)
or
die
(
"無法執行:"
.mysql_error());
352
header(
"location: {$_SERVER['PHP_SELF']}"
);
1. PHP入門 2. 使用者輸入介面設計 2-1 上課範例:index.php 2-2 上課範例:save.php 3. PHP變數與常數 3-1 上課範例:a1.php 4. PHP資料類型與建立資料表 4-1 上課範例:add_class.php 5. 連線資料庫及運算元 5-1 上課範例:save_class.php 5-1 上課範例:add_class.php 6. 讀取資料庫及PHP陣列 6-1 上課範例:add_class.php 6-2 上課範例:save_class.php 6-3 上課範例:index.php 7. 編輯與刪除功能 7-1 上課範例:index.php 7-2 上課範例:add_class.php 7-3 上課範例:save_class.php 8. 函數與小強佈景 8-1 上課範例:index.php 8-2 上課範例:add_class.php 8-3 上課範例:save_class.php 9. 流程控制與迴圈 9-1 上課範例:add_class.php 9-2 上課範例:theme.html 樣板檔 9-3 上課範例:save_class.php 9-4 上課範例:index.php 9-5 上課範例:header.php 9-6 上課範例:function.php 10. 練習練習再練習! 10-1 上課範例:index.php 10-2 上課範例:add_class.php 10-3 上課範例:save_class.php 10-4 上課範例:index.php(整合版) 10-5 上課範例:header.php(整合版) 11. 建立學習收藏的發布頁 11-1 上課範例:class.php 11-2 上課範例:header.php 11-3 上課範例:tad0616.sql 11-4 上課範例:blank.php 空白樣板 11-5 上課範例:index.php 12. 兩個資料表的結合 12-1 index.php 13. 資料檢查 13-1 上課範例:index.php 13-2 上課範例:header.php 13-3 上課範例:function.php 13-4 上課範例:class.php 14. 請完成基本功能囉~ 14-1 上課範例:index.php(完成版) 15. 認證與session 15-1 上課範例:class.php 15-2 上課範例:config.php 15-3 上課範例:header.php 15-4 上課範例:index.php 16. 分頁與編輯器 16-1 上課範例:index.php 16-2 上課範例:header.php 17. 用PHP上傳檔案 17-1 上課範例:index.php 18. 資料搜尋 18-1 上課範例:index.php 18-2 上課範例:theme.html