Welcome Guest, Not a member yet? Register   Sign In
How to edit record codeigniter CRUD?
#1

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>
Reply


Messages In This Thread
How to edit record codeigniter CRUD? - by ozzy - 06-26-2017, 12:09 PM



Theme © iAndrew 2016 - Forum software by © MyBB