我想添加簡單的編輯功能,但不幸的是沒有涵蓋在嘖嘖,我想有一個編輯按鈕,打開窗體和更新信息。
我按照教程從這裏: CRUD TUTORIAL如何編輯Codeigniter中的MySql數據庫記錄?
這裏是我的控制器:
<?php
class Site extends CI_Controller
{
function index()
{
$data = array();
if($query = $this->site_model->get_records())
{
$data['records'] = $query;
}
//$this->load->library('table');
$this->load->view('options_view',$data);
}
function create()
{
$data = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content')
);
$this->site_model->add_record($data);
$this->index();
}
function update()
{
$data = array (
'title' => 'My NEW UPDATED title',
'content' => 'NEW UPDATED content; UPDATED'
);
$this->site_model->update_record($data);
}
function delete()
{
$this->site_model->delete_row();
$this->index();
}
}
這裏是我的模型:
<?php
class Site_model extends CI_Model {
function get_records()
{
$query = $this->db->get('assets');
return $query->result();
// $query = $this->db->query('SELECT * FROM assets');
//echo $this->table->generate($query);
}
function add_record($data)
{
$this->db->insert('assets', $data);
return;
}
function update_record($data)
{
$this->db->where('id', 3);
$this->db->update('assets', $data);
}
function delete_row()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('assets');
}
}
這是我的看法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled</title>
<style type="text/css" media="screen">
label{display:block;}
</style>
</head>
<body>
<h2>Create</h2>
<?php echo form_open('site/create');?>
<p>
<label for="title">Title:</label>
<input type="text" name="title" id="title" />
</p>
<p>
<label for="content">Content:</label>
<input type="text" name="content" id="content" />
</p>
<p>
<input type="submit" value="submit" />
</p>
<?echo form_close(); ?>
<hr />
Read
<table>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<?php echo anchor("site/delete/$row->Id", $row->title); ?>
<td>
<td><?php echo $row->content; ?> </td>
<tr>
<td></td><td></td><td></td><td></td><td>edit</td>
</tr>
</tr>
<?php endforeach; ?>
</table>
<?php else : ?>
<h2>No records returned.</h2>
<?php endif; ?>
<hr />
<h2>Delete</h2>
<p>To sample the delete method, click on on of the headings above.
A delete query will automatically run.
</p>
</body>
</html>
您聯繫本教程是從2009年(8年前!),並使用代碼點火器v1。我做了一個快速谷歌,並有很多代碼點火器3的CRUD示例,這是更近的。遵循編程教程時,請始終檢查日期。 – hdifen
現在還是一樣。您可以在Codeigniter官方文檔中查看它。 –
從現在起8小時後,我會給你答案代碼。因爲我現在真的很困。 –