0

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.

Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
tarja
  • 28
  • 2
  • 8

3 Answers3

1

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.

Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
  • I load view, that user can have a form, there he edits message. I need to show old message in that text area too, but haven't done that yet – tarja Sep 20 '12 at 08:15
  • print_r($this->input->post()) to see if the post data is coming just for debugging purpose – Muhammad Raheel Sep 20 '12 at 08:18
  • I wrote before: "updating works if I change this "$note_id = $this->uri->segment(3)" to "$note_id = 24" (I use id that exist in my database) in my controller" so I guess it gets post data :) – tarja Sep 20 '12 at 08:26
  • then do it echo $this->uri->segment(3); because 3rd segment might not having any value also check your view file anchor for update it should have been 'site/members_area/some_id' instead of 'site/members_area/' – Muhammad Raheel Sep 20 '12 at 08:53
1

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!

manavo
  • 1,839
  • 2
  • 14
  • 17
  • I use this echo in my edit_note view to check if I get that number I need: `$note_id = $this->uri->segment(3); echo "$note_id";` It prints that I need – tarja Sep 20 '12 at 08:37
  • Is this when you open the edit form (after you click the edit link) or after the form is submitted and you are trying to save it? – manavo Sep 20 '12 at 08:39
  • Yeah, so stupid mistake, I changed it to `echo form_open('site/edit_note/' .$this->uri->segment(3));` in my edit_note view. And it works! – tarja Sep 20 '12 at 08:47
  • The other solution would have been to just leave that blank (so `echo form_open();`). Then the browser will always submit the form to whatever the current URL is (so you don't have to manually reconstruct it every time) :) – manavo Sep 20 '12 at 08:50
  • So it is enough to do the same as I have done in my delete method now. There is no need to do all the segment passing from controller to model, I can access it directly from my model – tarja Sep 20 '12 at 08:52
  • Although it would work, it's much better if you do pass it in from the controller. You might want to call the delete method of a model from somewhere different at some point, and the actual ID based on which you delete might not necessarily come from the 3rd segment of the URL! – manavo Sep 20 '12 at 08:54
  • Great advice about that blank form :) I will sure use it. For a beginner like me, there is always something to know, even if it is just as simple... – tarja Sep 20 '12 at 08:55
  • Oh ok, I thought it was more simple to do it this way. So what you say is as @HappyApe said about this, I will do that this way :) Thanks for your advice! – tarja Sep 20 '12 at 08:58
0

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.

TigerTiger
  • 10,590
  • 15
  • 57
  • 72
  • But I thought model is for operations with database. It is not recommended to use controller for operations with data. – tarja Sep 20 '12 at 08:10
  • Yes, that's correct but you're binding your model with URI in your code above - and as per your current logic there isn't any control over who can delete this data etc .. I can just go to yourwebsite.com/delete_note/1 .. /delete_note/2, /3/, /4 and mess all your data .. but if you're passing these ID via controller you can sanitise this value, check if the user has permissions to delete the note and many more things as required. Hope it makes sense. – TigerTiger Sep 20 '12 at 08:46
  • Yeah it makes sense now. I will surely use it in the future, thanks! – tarja Sep 20 '12 at 09:01