Skip to Content
PHPCODE
CodeIgniter Article Updated.
codeigniter code / August 2, 2018

Article Update in Codeigniter

Step 1:- Created Update function in controller
public function update_article($article_id)
{
$this->load->library(‘form_validation’);
$this->form_validation->set_error_delimiters(“<p class=’text-danger’>”,”</p>”);
if($this->form_validation->run(‘add_article_rules’))
{
$post=$this->input->post();
//$article_id=$post[‘article_id’];
//unset($post[‘article_id’]);
$this->load->model(‘articlesmodel’,’articles’);
if($this->articles->update_article($article_id,$post))
{
//flash message insert successfull
$this->session->set_flashdata(‘feedback’,”Article Updated successfull.”);
$this->session->set_flashdata(‘feedback_class’,’alert-success’);
}
else
{
//insert failed.
$this->session->set_flashdata(‘feedback’,”Article failed To Update, Please Try Again.”);
$this->session->set_flashdata(‘feedback_class’,’alert-danger’);
}
return redirect(‘admin/dashboard’);
}
else
{
$this->load->view(‘admin/edit_article’);
}
}
Step 2:- created articlesmodel in model

public function update_article($article_id,Array $article)
{
return $this->db
->where(‘id’,$article_id)
->update(‘articles’,$article);
}

step 3: Created edit_article in view

<?php include_once(‘admin_header.php’); ?>

<?php echo form_open(“admin/update_article/{$article->id}”);
//echo form_hidden(‘article_id’,$article->id);
?>
<div class=”container”>
<fieldset>
<legend>Edit Article</legend>
<div class=”row”>
<div class=”col-lg-12″>
<?php echo form_label(‘Article Title’);?>
</div>
</div>
<div class=”row”>
<div class=”col-lg-6″>
<div class=”form-group”>
<?php
echo form_input([‘class’=>’form-control’,’name’=>’title’,’placeholder’=>’Article Title’,’value’=>set_value(‘title’,$article->title)]);
?>
</div>
</div>
<div class=”col-lg-6″>
<?php echo form_error(‘title’);?>
</div>
</div>
<div class=”row”>
<div class=”col-lg-12″>
<?php echo form_label(‘Article Body’);?>
</div>
</div>
<div class=”row”>
<div class=”col-lg-6″>
<div class=”form-group”>
<?php
echo form_textarea([‘class’=>’form-control’,’name’=>’body’,’placeholder’=>’Article Body’,’value’=>set_value(‘body’,$article->body)]);
?>
</div>
</div>
<div class=”col-lg-6″>
<?php echo form_error(‘body’);?>
</div>
</div>
<?php
echo form_submit([‘class’=>’btn btn-primary’,’value’=>’SUBMIT’]),
form_reset([‘class’=>’btn btn-default’,’value’=>’RESET’]);
?>
</fieldset>
<?php echo form_close();?>
</div>
<?php include_once(‘admin_footer.php’);?>

PHPCODE © 2024