Rajan
Rajan

Reputation: 2425

Why does Edit URI not work properly Codeigniter Code?

I am trying to edit a record. Now In my Button i am passing the the EMP_ID as uri but it takes id and edits on that basis:

See this is my code:

public function edit($emp_id = NULL)
{


    if ($emp_id) 
                {
                    $this->data['user'] = $this->user_m->get($emp_id);
                    count($this->data['user']) || $this->data['errors'][] = 'User could not be found';


                    echo $emp_id;



                    // Set up the form
                    $rules = $this->user_m->rules_admin;
                    $id || $rules['password']['rules'] .= '|required';
                    $this->form_validation->set_rules($rules);

                            // Process the form
                    if ($this->form_validation->run() == TRUE) 
                    {

                        $data = $this->user_m->array_from_post(array('emp_id','name','last_name','email','password','phone','gender','designation','user_type','blood_group','date_birth','status','address'));


                        $data['password'] = $this->user_m->hash($data['password']);

                        $key=$this->user_m->save($data, $id);

                        redirect('admin/user/index');
                    }


                }

                // Load the view

                echo $this->db->last_query();
                $this->data['subview'] = 'employee/profile/edit';
                $this->load->view('employee/_layout_main', $this->data);
                }

And this is my view:

        <table class="table table-striped table-hover">
            <thead>
                <tr>


                    <th>Employee_Id</th>
                    <th>Name</th>
                    <th>Last_Name</th>
                    <th>Email</th>
                    <th>Phone</th>
                    <th>Gender</th> 
                    <th>Designation</th>
                    <th>User Type</th>
                    <th>Blood Group</th>
                    <th>Date of Birth</th>
                    <th>Status</th>

                    <th>Edit</th>



                </tr>
            </thead>
            <tbody>
    <?php if(count($users)): foreach($users as $user): ?>   
               <tr class="active">


        <td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->emp_id); ?></td>   
        <td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->name); ?></td>
        <td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->last_name); ?></td>
        <td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->email); ?></td>
        <td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->phone); ?></td>
        <td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->gender); ?></td>
        <td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->designation); ?></td>
        <td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->user_type); ?></td>
        <td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->blood_group); ?></td>
        <td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->date_birth); ?></td>
        <td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->status); ?></td>


                    <td><?php echo btn_edit('employee/profile/edit/' . $user->emp_id); ?></td>

                </tr>
        <?php endforeach; ?>
        <?php else: ?>
                <tr>
                    <td colspan="3">We could not find any users.</td>
                </tr>
        <?php endif; ?> 
                </tbody>
            </table>
        </section>


        <?php $this->load->view('admin/components/page_tail') ?>

Now when i click on my edit button the url is correct.

http://127.0.0.1/project/admin/user/edit/21

but i edits the row with id= 21 instead of emp_id=21.

I can't understand the bug in my code please help

MY_MODEL

:

public function get($id = NULL, $single = FALSE){

    if ($id != NULL) {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->where($this->_primary_key, $id);
        $method = 'row';
    }
    elseif($single == TRUE) {
        $method = 'row';
    }
    else {
        $method = 'result';
    }


    return $this->db->get($this->_table_name)->$method();
}

public function get_by($where, $single = FALSE){
    $this->db->where($where);
    return $this->get(NULL, $single);
}

public function save($data, $id = NULL){




    // Set timestamps
    if ($this->_timestamps == TRUE) {
        $now = date('Y-m-d H:i:s');
        $id || $data['created'] = $now;
        $data['modified'] = $now;
    }

    // Insert
    if ($id === NULL) {
        !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
        $this->db->set($data);
        $this->db->insert($this->_table_name);
        $id = $this->db->insert_id();
    }
    // Update
    else {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->set($data);
        $this->db->where($this->_primary_key, $id);
        $this->db->update($this->_table_name);
    }

    return $id;
}

Upvotes: 0

Views: 54

Answers (2)

Preetham Hegde
Preetham Hegde

Reputation: 849

please check the $users array in the controller

echo print_r ($users);

it will print whole user array

It may help you to track exact data

Upvotes: 1

Rakib Roni
Rakib Roni

Reputation: 244

You can write this

<td><?php echo btn_edit('employee/profile/edit/' . $user->emp_id); ?></td>

instead of

<td><?php echo btn_edit('employee/profile/edit/' . $user->id); ?></td>

Upvotes: 0

Related Questions