Welcome Guest, Not a member yet? Register   Sign In
How can I dynamically get the ID from a row in my database?
#1

[eluser]cvandal[/eluser]
Hello,

Can someone please explain to me how I can dynamically get the ID from a row in my database rather than the hardcode method I have below?

I've tried, '$this->db->where('id', $this->uri->segment(3));' but nothing happens. I don't get any errors either.

The Model:
Code:
function update_entry($data)
    {
        $this->db->where('id', 103);
        $this->db->update('pages', $data);
    }

The Controller:
Code:
function update_page()
    {
        $data = array(
            'title' => $this->input->post('title'),
            'content' => $this->input->post('content')
        );
        $this->crud_model->update_entry($data);
        $this->index();
    }
#2

[eluser]jedd[/eluser]
I'm confused by your code and your words.

When you say 'get the ID from a row in my database' do you mean get the ID from a row in your database, or do you mean take the ID as given in the URL?

If your controller (shown above) was called Page, say, and your method was update(), then you need to have this kind of construct in there:
Code:
function update ( $page_id = 0 )    {
        if ($page_id == 0)
             // bomb out somehow ;
        $data = array(
            'title' => $this->input->post('title'),
            'content' => $this->input->post('content')
            );
        $this->crud_model->update_entry($page_id, $data);
        ...

Your URL would be http://..../page/update/27

Your model now takes two parameters - the first is the row.id to manipulate.

Is this the kind of thing you mean?
#3

[eluser]cvandal[/eluser]
Hi Jedd,

To clairfy, I have two views. One which displays "pages" from my database and provides an option to either delete or update that "page". If a user clicks on the update anchor, the second view is loaded. The second view is simply a form that when submitted will update the corresponding "page".

So let's say there are 3 rows in my pages table, each has it's own ID (100, 101 and 103). At the moment 103 is hardcoded in my Model for updating. What i'm trying to do is have that ID depend on what "page" in selected by the user instead of me hardcoding it.

I hope that makes sense Smile
#4

[eluser]jedd[/eluser]
[quote author="cvandal" date="1252783073"]
One which displays "pages" from my database and provides an option to either delete or update that "page". If a user clicks on the update anchor, the second view is loaded.
[/quote]

Okay, in that first view, your anchor for the 'update link' needs to take you to /page/update/{pageid}.

Presumably the first view knows the ID of the page that it's showing, so it's a simple matter to put that into the anchor - yes?

The call from the controller to the model to do the database side of the updating simply takes the id that comes in on that url - this is the bit I've shown above.


Your model needs to cope with parameters for a) the id, b) the content

You'll also need to wrap some security around this, of course, so people can't just write their own URL's to modify posts they're not meant to.
#5

[eluser]cvandal[/eluser]
Thanks Jedd!

I've got it working now! You're a champion.




Theme © iAndrew 2016 - Forum software by © MyBB