標籤:style http color 使用 os io 資料 for
一、控制器
<?php
if (!defined(‘BASEPATH‘))
exit(‘No direct script access allowed‘);
class Topics extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(‘url‘);
$this->load->helper(‘form‘);
//注意,database、 session已經自動載入,在以後的過程中,直接使用即可
}
function index()
{
$this->db->order_by(‘id‘, ‘asc‘);
$query = $this->db->get(‘topics‘, 20);
if ($query->num_rows() > 0)
{
$data[‘result‘] = $query->result();
}
else
{
$data[‘result‘] = ‘‘;
}
$this->load->view(‘/topics/index‘, $data);
}
public function add()
{
$this->load->view(‘topics/add‘);
}
public function addpost()
{
if (trim($this->input->post(‘content‘)) != ‘‘ && trim($this->input->post(‘title‘)) != ‘‘)
{
//方法一
// $sql = ‘insert into TOPICS(id,title,content) values(?,?,?)‘;
// $bool = $this->db->query($sql, array($this->input->post(‘id‘),
// $this->input->post(‘title‘),
// $this->input->post(‘content‘))
// );
// if ($bool)
// {
// redirect(‘topics/index‘, ‘location‘, 301);
// }
// 方法二:
$data = array(
‘id‘=> $this->input->post(‘id‘),
‘content‘ => $this->input->post(‘content‘),
‘title‘ => $this->input->post(‘title‘));
if ($this->db->insert(‘topics‘, $data))
{
redirect(‘topics/index‘, ‘location‘, 301);
}
}
}
function edit()
{
//此處直接從地址欄中取得參數,即ID參數
$id = $this->uri->segment(3);
if ($id != ‘‘)
{
if (!$this->input->post(‘title‘))
{
$query = $this->db->get_where(‘topics‘, array(‘id‘ => $id));
$data[‘result‘] = $query->result();
$this->load->view(‘topics/edit‘, $data);
}
else
{
$this->db->where(‘id‘, $id);
if ($this->db->update(‘topics‘, $_POST))//注意,此處直接使用了$_POST系統變數
{
redirect(‘topics/index‘, ‘location‘);
}
}
}
}
//此語句可以操作oracle
function del_topics($id)
{
if (!$id)
{
redirect(‘topics/index‘);
}
else
{
$this->db->where(‘id‘, $id);
if ($this->db->delete(‘topics‘))
{
redirect(‘topics/index‘);
}
}
}
//此語句可以操作oracle
function drop_table()
{
$this->load->helper(‘url‘);
if ($this->db->empty_table(‘topics‘))
{
redirect(‘topics/index‘);
}
}
}
?>
二、視圖add.php edit.php
(一)add.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Topics</title>
</head>
<body>
<h3>Topics:</h3>
<?php echo form_open(‘topics/addpost‘); ?>
<p>ID:<input type=‘text‘ name=‘id‘ value=‘‘ /></p>
<p>Title:<input type=‘text‘ name=‘title‘ value=‘‘ /></p>
<p>Content:<input type=‘text‘ name=‘content‘ value=‘‘ /></p>
<input type=‘hidden‘ name=‘createtime‘ value="" />
<p><input type=‘submit‘ value=‘Add Topics‘ /></p>
</form>
<p>
<?php echo anchor(‘topics/index‘, "Back to Topics‘s index page"); ?>
</p>
</body>
</html>
(二)edit.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Topics</title>
</head>
<body>
<?php echo form_open(‘topics/edit/‘ . $this->uri->segment(3)); ?>
<p>ID:<input type=‘text‘ name=‘id‘ value="<?php echo $this->uri->segment(3) ?>" /></p>
<p>Title:<input type=‘text‘ name=‘title‘ value="<?php echo $result[0]->TITLE ?>" /></p>
<p>Content:<input type=‘text‘ name=‘content‘ value="<?php echo $result[0]->CONTENT ?>" /></p>
<p>Createtime:<input type=‘text‘ name=‘createtime‘ value="<?php echo $result[0]->CREATETIME ?>" /></p>
<p><input type=‘submit‘ value=‘Save Topics‘ /></p>
</form>
<p><?php echo anchor(‘topics/index‘, "Back to topics‘s Index page"); ?></p>
</body>
</html>