Welcome Guest, Not a member yet? Register   Sign In
Database Update Question
#1

[eluser]ehalber[/eluser]
Currently, I'm using a hidden form field to capture the id of the database record I want to update. Next I save the post vars in my controller and send them to my model. Is this the standard way to perform this operation? Are there alternative ways, better ways?

Code from controller
Code:
function update_news()
{

    $update_info = array(
                        'article' => $this->input->post('article'),
                        'order_date' => $this->input->post('order_date'),
                        'date' => $this->input->post('date')
                       );
  
    $id = $this->input->post('id');
  
    $this->Drift_model->update_entry($id, $update_info);

    redirect('admin/control_panel');
}

Code from model
Code:
function update_entry($id, $update_info)
{  
    $query = $this->db->update('news', $update_info, array('id' => $id));    
}

Thanks
#2

[eluser]Derek Allard[/eluser]
Not a very helpful response from me, but that's nearly exactly how I do it - I don't see a more elegant way off hand.
#3

[eluser]LuckyFella73[/eluser]
Another approach would be to set the id as the last URL segment
of your form/action-parameter and get the id in the method by
using the url_segment function. But like I said, it's just an
other approach and not meant to be more elegant than using the
hidden form field.
#4

[eluser]eggshape[/eluser]
I took an alternative approach. I saved the id of the target table to the cookie (and sessions table) and used an inherited method to update the table.

Note, this involves modifying the Sessions class to accept non-default fields and the Model superclass, so that any derived classes inherit the method. In each model's constructor, I retrieve the id and place it in a protected property. So when I call $this->$model->update(), there is no need to pass arguments and retrieve hidden-data.
#5

[eluser]ehalber[/eluser]
@Derek, thanks for the quick reply. I also wanted to say thanks for the great tutorial you have on your site, it really helped me to see how to integrate Code Igniter with JavaScript, my first love. I just started a blog of my own, codelicker.com. My second entry was a short note on available code igniter tutorials. While the list is short, I think it definitely covers the basics for those needing a quick introduction. Collecting these tutorials and others like them into one place on the code igniter site might be a great resource for newbies.

Well, I just finished driftstudio.org, it's a very simple site showcasing work and news by five collaborating members. One is my brother, Geoff Halber, who is a senior designer at Dwell magazine.

The tagline reads: Drift is a cross-disciplinary design studio working in the fields of architecture and graphic design.

While there's no work yet, I'm hoping they'll have some time to get their contributions together soon.

Anyway, I'm excited about getting to know code igniter better and exploring some of its other features in future projects.
#6

[eluser]ehalber[/eluser]
@luckyfella73

I originally was trying to do that, but since I was using a submit button, there is no uri segment. Am I missing something?
#7

[eluser]Negligence[/eluser]
You're doing it the typical way. Just make sure you're performing some validation to see if you're working with a valid record (ID) before you use it in a query.
#8

[eluser]LuckyFella73[/eluser]
What I meant was:

In your view file you have the form tag:
Code:
<?= form_open('controller_update/method_update/'.$id_to_update);?>

By submitting the form via your button, the form send the post data
to your controller - for example:

www.yoursite.com/controller_update/method_update/12

In your method you get the value using:
Code:
$id_to_update = $this->uri->segment(3)
#9

[eluser]ehalber[/eluser]
@lucky

Wow, that was a really simple solution, thank you. :coolsmile:

Now one more question. Is this type of thing a matter of style or is one method considered more appropriate?
#10

[eluser]LuckyFella73[/eluser]
@ehalber

your are welcome!

Regarding to your last question:
If Derek uses to take the hidden-form-field approach I would tend
to do it his way Wink
But maybe it's just a matter of style.

Your question is interesting for me too so it would be great
if other members would post some advantages and disadvantages
for both mehtods.




Theme © iAndrew 2016 - Forum software by © MyBB