ozzy t
asked on
How to edit record in codeigniter?
I did tutorial located here:, what i would like to do is add edit functionality, i would like to have an edit button and be able to edit record, the tutorial did no cover this, i have researched but i just cant wrap my head around it, i am new to codeigniter as you can tell.
here is my controller:
Here is my model:
Here is my view:
here is my controller:
<?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();
}
}
?>
Here is my model:
<?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');
}
}
Here is my view:
<!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>
<?php echo form_close(); ?>
<hr />
<!-- <h2>Read</h2> -->
<h2>Read</h2>
<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; ?>
<!-- <?php if(isset($records)) : foreach ($records as $row) : ?>
<h2><?php echo anchor("site/delete/$row->Id", $row->title); ?> </h2>
<div><?php echo $row->content; ?> </div>
<?php endforeach; ?>
<?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>
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Upon submission of the form, now you will have the modified data along with the respective record ID and need only perform your update in your model with your record ID as the where clause for the update.
Hope this helped.