Kaushtuv
Kaushtuv

Reputation: 489

Adding dynamic id in the url using codeigniter

I have an edit page with url

example.com/product/edit/11

Now if the validation is false I intend to reload the edit page but can't figure how to add that id '11' in the url

Things I have done so far

In routes.php

$route['product/edit/(:num)'] = "product/edit/$1";

Now how do I pass that id in the url from the controller

I tried this

$this->load->view('product/edit/'.$edit_id,$data);

Edit: I am not trying to pass edit_id to the view file

I am trying to simulate this:

<a href="example.com/product/edit/11">Edit</a>

So that the function in my controller

function edit($edit_id)
{
//some code
}

Will have have that $edit_id to work with

It didn't work as I suspected.

I know I can pass that id to the view file and make it work, but I wanted to if I can pass that id in the url.

Thanks

Edit: I figured that even if I managed to pass it in the url. I will end up with more problem like setting the form attributes and other stuff. What I did was

redirect('product/edit/'.$edit_id);

and gave an error message by setting the flashdata.

Upvotes: 0

Views: 7884

Answers (1)

safarov
safarov

Reputation: 7804

Your Product.php controller

function edit($edit_id) {

    //some codes

    //passing edit_id to view
    $data['edit_id'] = $edit_id;
    $this->load->view('product/edit', $data);
}

And in view/product folder, edit.php view file

 //some codes
 echo $edit_id

Or directly from url in view file

echo $this->uri->segment(3);

Upvotes: 4

Related Questions