Abhijit
Abhijit

Reputation: 131

how to update a particular row in codeigniter?

I am newbie in Code Igniter.i want to update a specific row on update.

Please help me.

My View file

<div id="wrapper">
<div id="form">
<?php $query=$this->mod1->about_display()?>
<?php if($query){  ?>
<?php   echo form_open('textarea/update_testimonial');   ?>
 <?php foreach($query as $row){ ?>
 <textarea name="update"><?php echo  $row->testi; ?></textarea>
 <input type="submit" name="sub" value="UPDATE" />
 </form>
 <?php } 

    }
?>
</div>
 </div>

My Controller file

function update_testimonial(){

   $id = $this->input->get('id');
   $this->load->model('mod1');
   $this->mod1->about_display($id);


}

my model file

   function about_display(){
 $id = $this->input->get('id'); 
$query=$this->db->where('id', $id);
    $this->db->get('testimonials');
    if($query->num_rows()>0){
        return TRUE;
    }else{
        return FALSE;
    }

}

Please give me a simple example so I can do it. I am very new in CI. Please help me out. Thank you

Upvotes: 0

Views: 4941

Answers (1)

David
David

Reputation: 3822

Controller:

function update_testimonial(){
   $update = $this->input->post('update', True); 
   $id = (int) $this->input->get('id', False); ### THIS MAY need to be a $this->input->post()
   $this->load->model('mod1');
   $this->mod1->update_display($update, $id);
}

Model:

function update_display($update, $id) {
   $this->load->database(); 
   $this->db->where('id', $id); 
   $update = array('testi', $update); 
   $this->db->update('testimonials', $update);
}

Basically you pull the $_POST variable from the form, send it to the model, and use Codeigniter's $this->db->update() function to update that single row. You may consider adding a <form type="hidden" name="id" /> to the form so you have both significant variables coming in through POST.

Upvotes: 1

Related Questions