CodeIgniter Forums

Full Version: How to edit record codeigniter CRUD?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would like to add simple edit functionality which unfortunately was not covered in tut,I would like to have an edit button that opens a form and update info.
I followed tut from here:https://code.tutsplus.com/articles/codei...--net-6504

Here is my Contrroller:

Code:
<?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:

Code:
<?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:

Code:
<!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; ?>




<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>
The difference between creating a record and updating one is knowing the record ID that you want to update.

When updating, you can reuse the form that is used with the create methods.

So just copy all the steps needed for creating a record. Add a method the get the record you want to update, and change the form so it excepts a data array containing the DB record information.
Last week we need a decision to use either Zend3 or CI3. In order to test it I used the Zend3 skeleton module which is a simple CRUD project.

here my edit function from the controller
Code:
public function edit($id = NULL)
   {
       
        $data['title']   = "My Album";        
       
        //get the album data
        $data['album']   = $this->albums_model->getByID($id);
        
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('artist', 'Artist', 'required');
        
       if ($this->form_validation->run() == FALSE)
       {
            //show the form when not valid
            $this->load->view( get_class($this) . '/add', $data);
       }
       else
       {
            //add the data
            $data = array(
                'title' => $this->input->post('title'),
                'artist' => $this->input->post('artist')
            );
            $this->db->where('id', $id);
            $this->db->update('album', $data);
            redirect('/albums/');
       }

       
   }

And here the function from the model
Code:
public function getByID($id)
    {        
    
        $query = $this->db->get_where('album', array('id' => $id));
        if ( $query->num_rows() > 0){
            return $query->row();
        } else {
            return false;    
        }
    
    }


This is all very simple but hope it helps.

(We did decide to use CI3 rather then Zend)