Question

I am using uri segment to delete info in my database:

anchor('site/delete_note/'.$row->id, 'Delete')

Model:

function delete_note()
{
    $this->db->where('id', $this->uri->segment(3));
    $this->db->delete('note');
}

It works fine, but I want to do the same for updating my info and can't get it work So this is link in view:

anchor('site/edit_note/'.$row->id, 'Edit')  

My controller:

function edit_note()
{
    $note_id = $this->uri->segment(3);
    $data['main_content'] = 'edit_note';
    $this->load->view('includes/template', $data);

    $this->load->library('form_validation');

    $this->form_validation->set_rules('content', 'Message', 'trim|required');


    if($this->form_validation->run() == TRUE)
    {
        $this->load->model('Note_model');
        $this->Note_model->edit_note($note_id);
        redirect('site/members_area'); 

    }

}

My model:

function edit_note($note_id)
{
    $content = $this->input->post('content');
    $data = array('content' => $content);
    $this->db->where('id', $note_id);
    $this->db->update('note', $data);
}

My view of edit_note:

<?php

    echo form_open('site/edit_note');
    echo form_textarea('content', set_value('content', 'Your message'));

    echo form_submit('submit', 'Change');
    echo anchor('site/members_area', 'Cancel');


    echo validation_errors('<p class="error">');        ?>

Edit doesn't work as delete, when i am trying to get segment directly in edit model, as I used in delete model.

If I set $note_id to a number in my controller, instead of this '$this->uri->segment(3)', it updates my database. But if I use getting segment it doesn't work. I thought uri segments are available in controller as in model, but there is something I don't know.

Was it helpful?

Solution

Better yet, instead of manually reading the IDs via the segments, you could change your functions to be:

function delete_note($note_id)

and

function edit_note($note_id)

And remove the $note_id = $this->uri->segment(3); lines.

And as silly as it'll sound, the generated URL is definitely correct, right?

And last question, have you done anything with routes?

Edit

I've also noticed that in edit, you use this in your form:

echo form_open('site/edit_note');

So when the form submits, the URL it submits to is site/edit_note instead of site/edit_note/{SOME_ID}. So once you make your changes, and the form submits, there won't be a 3rd URL segment!

OTHER TIPS

In my view it's a 'bad' approach to use uri segments in your models... you should pass an id as a parameter from your controller functions ..

function delete_note()
{
    $this->db->where('id', $this->uri->segment(3));
    $this->db->delete('note');
}

What if you want to re-use this delete method? e.g. deleting notes from an admin panel, via a cron job etc then the above relies upon the uri segment and you will need to create additional delete methods to do the job. Also, if you were to continue with the same you don't even need a model then .. just call these lines in your controllers if you know what I mean ...

$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');

so best is to change it to similar to your edit_note() model function.

Well there are some logical errors in your code.

function edit_note()
{
    $note_id = $this->uri->segment(3);
    $data['main_content'] = 'edit_note';
    $this->load->view('includes/template', $data);
//// What is the use of loadig a view when you are editing

    $this->load->library('form_validation');

    $this->form_validation->set_rules('content', 'Message', 'trim|required');


    if($this->form_validation->run() == TRUE)
    {
        $this->load->model('Note_model');
        $this->Note_model->edit_note($note_id);
        redirect('site/members_area'); 

    }

}

Instead do it like this

function edit_note()
{
    if($this->input->post()){

        $this->load->library('form_validation');

        $this->form_validation->set_rules('content', 'Message', 'trim|required');


        if($this->form_validation->run() == TRUE)
        {
            $this->load->model('Note_model');
            $this->Note_model->edit_note($note_id);
            redirect('site/members_area'); 
        }
    }else{
        $note_id = $this->uri->segment(3);
        $data['main_content'] = 'edit_note';
        $this->load->view('includes/template', $data);
    }
}

MOST IMPORTANT
And the other thing you should note that you are using anchor to access edit note but not actually submitting a form so it is not getting any post data to update.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top