[1032]PHP入門
<?php
/*----引入檔案----*/
require_once "config.php";
require_once "function.php";
/*----整理傳進來的變數或變數初始值----*/
$op=isset($_REQUEST['op'])?$_REQUEST['op']:"";
$sn=isset($_REQUEST['sn'])?intval($_REQUEST['sn']):"";
$toolbar='<a href="post.php" class="btn btn-primary btn-block"><i class="fa fa-pencil"></i> 發布新聞</a>';
$error_msg=$news_list="";
/*----流程控制----*/
switch ($op) {
case 'db_error':
$error_msg=error_msg("資料庫連線錯誤!!請檢查資料庫帳號、密碼是否正確!");
break;
case 'view':
//列出單一內容
$news_list=view($sn);
$toolbar.="<a href='{$_SERVER['PHP_SELF']}' class='btn btn-success btn-block'><i class='fa fa-home'></i> 回新聞列表</a>";
break;
case 'delete':
//刪除單一內容
delete($sn);
header("location: {$_SERVER['PHP_SELF']}");
break;
default:
//列出所有內容
$news_list=list_news();
break;
}
/*----輸出----*/
show_page('list_tpl');
/*----所有函數----*/
//列出所有內容
function list_news(){
link_db();
//讀取eznews資料表所有欄位(日期大到小排列)
//$sql="select * from eznews order by post_time desc";
$sql="select a.*,b.cate_title from eznews as a
left join eznews_cate as b on a.cate_sn=b.cate_sn
order by a.post_time desc";
//傳回值存到 $result 以供抓取資料用
$result=mysql_query($sql) or die("{$sql}<br>".mysql_error());
$news_list="
<script>
function del_func(sn){
var sure = window.confirm('確定要刪除此資料?');
if (!sure) return;
location.href='index.php?op=delete&sn=' + sn;
}
</script>
<h1>列出所有新聞</h1>
<table class='table table-striped table-bordered'>
<tr>
<th class='col-md-2 text-center'>分類</th>
<th class='col-md-5 text-center'>新聞標題</th>
<th class='col-md-3 text-center'>發布時間</th>
<th class='col-md-2 text-center'>功能</th>
</tr>
";
//取回資料庫一筆資料,並以欄位名稱為索引的資料陣列
while($news=mysql_fetch_assoc($result)){
$title=empty($news['news_title'])?"無標題":$news['news_title'];
$news_list.="
<tr>
<td>
{$news['cate_title']}
</td>
<td>
<a href='{$_SERVER['PHP_SELF']}?op=view&sn={$news['sn']}'>{$title}</a>
</td>
<td class='text-center'>{$news['post_time']}</td>
<td class='text-center'>
<a href='javascript:del_func({$news['sn']})' class='btn btn-xs btn-danger'>刪除</a>
<a href='post.php?op=modify&sn={$news['sn']}' class='btn btn-xs btn-warning'>編輯</a>
</td>
</tr>
";
//$news_list=$news_list."<li>{$news['news_title']}</li>";
}
$news_list.="</table>";
return $news_list;
}
//顯示單一頁面
function view($sn=""){
link_db();
//讀取eznews資料表所有欄位,並指定某一筆特定資料
$sql="select * from eznews where sn='$sn'";
//傳回值存到 $result 以供抓取資料用
$result=mysql_query($sql) or die("{$sql}<br>".mysql_error());
$news=mysql_fetch_assoc($result);
$news_content=$news['news_content'];
$one_news="<h1>{$news['news_title']}</h1>
<div class='well'>
{$news_content}
</div>";
return $one_news;
}
//刪除單一頁面
function delete($sn=""){
link_db();
//刪除指定某一筆特定資料
$sql="delete from eznews where sn='$sn'";
mysql_query($sql) or die("{$sql}<br>".mysql_error());
}
?>