資料刪除
<?php
$del = "老鷹";
echo $del;
//print 消失的老鷹 XD
?>
開玩笑!!
我們回到controllers/news.php
今天增加一個方法
//get 傳送 id
function DelNews($id){
/* 使用陣列的方式處理,是給$this->db->where();
* $this->db->where('欄位','條件');//一般
* $this->db->where($array);//也支援陣列
*/
$array = array('id'=>$id);
$this->sql->where_del($array,'news');
redirect(site_url().'news/index');
}
models/sql.php
function where_del($array,$table){
//delete('資料表'); 記得給條件 不然整張給你砍了
$this->db->where($array)->delete($table);
}
views/news.php
<?php
/*---------------------------------------*/
//........ 此頁面請放在view資料夾底下.......//
/*---------------------------------------*/
$link = 'http://localhost/test/';
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>公佈欄</title>
<!--以下是 Bootstrap 的文件包-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="<?=$link;?>css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="<?=$link;?>css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="<?=$link;?>js/jquery.js"></script>
<script src="<?=$link;?>js/bootstrap.min.js"></script>
<style>
#heig{
height:10px;
}
#color{
background-color: #E5E5E5;
}
</style>
<!--"container"為固定置中,請參考 http://kkbruce.tw/Bootstrap/Scaffolding 佈局篇-->
<div class="container">
<!--class="table table-bordered table-condensed"請參考 http://kkbruce.tw/Bootstrap/BaseCSS#tables -->
<table width="1200" border="0" class="table table-bordered table-condensed">
<tr>
<td colspan="2" bgcolor="#CCCCCC"><div align="center"><strong>公佈欄</strong></div></td>
</tr>
<tr>
<td>
<!-- 請參考 http://kkbruce.tw/Bootstrap/BaseCSS#forms -->
<div id="heig">
<form action="" method="post" name="like" class="form-inline" id="like">
<strong>搜尋標題: </strong>
<label>
<input type="text" name="title"/>
</label>
<button class="btn" type="submit">搜尋</button>
</form>
</div>
</td>
<td>
<div align="center">
<!-- 請參考 http://kkbruce.tw/Bootstrap/BaseCSS#buttons -->
<a href="<?=$link;?>index1.php/news/AddNews"><button class="btn" type="button">新增公告</button></a>
</div></td>
</tr>
<tr>
<td colspan="2"><table width="1200" border="0" class="table table-bordered table-condensed table-striped table-hover">
<tr>
<td id="color"><div align="center"><strong>項次</strong></div></td>
<td id="color"><div align="center"><strong>分類</strong></div></td>
<td id="color"><div align="center"><strong>標題</strong></div></td>
<td id="color"><div align="center"><strong>時間</strong></div></td>
<td id="color"><div align="center"><strong>管理</strong></div></td>
</tr>
<?php
/** result_array() 參考 http://www.codeigniter.org.tw/user_guide/database/results.html **/
foreach($query->result_array() as $key=>$row):
?>
<tr>
<td><div align="center"><?=$key+1;?></div></td>
<td><div align="center"><?=$row['class'];?></div></td>
<td><div align="center"><?=$row['title'];?></div></td>
<td><div align="center"><?=$row['date'];?></div></td>
<td><div align="center">
<button class="btn">修改</button>
<a href="<?=$link;?>index1.php/news/DelNews/<?=$row['id'];?>"><button class="btn" type="button" onclick="if(confirm('您確定刪除<?=$row['title'];?>嗎?')) return true;else return false">刪除</button></a>
</div></td>
</tr>
<?php endforeach; ?>
</table></td>
</tr>
</table>
</div>
示意圖
消失了XD
待續....