CodeIgniter Forums
How to get 'id' from URL? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to get 'id' from URL? (/showthread.php?tid=60110)



How to get 'id' from URL? - El Forum - 01-17-2014

[eluser]tahakirmani[/eluser]
Hi

I am trying to get 'id' from URL in order to delete that specific selected product. I have used $this->uri->segment(3) but its not getting any value from URL. Although i can see the 'product_id' in my URL.
If i used $this->url->segment(2) it gives me the second value form URL, but i am unable to get product Id.
Following is my code. Kindly guide me.

Code:
//VIEW
        echo "<a href='delete_controller?product_id=$product_id'>";
        echo $name;
        echo "</a>";

The following URL is generating when i click on $name.
Code:
http://localhost/designs2/index.php/products_controller/delete_controller?product_id=70




Code:
public function delete_controller()
        {
            echo $product_id =$this->uri->segment(3);          
            echo "Taha";
            $this->load->view('delete_confirmation');
        }

Thanks,
Taha


How to get 'id' from URL? - El Forum - 01-17-2014

[eluser]noideawhattotypehere[/eluser]
Code:
class Example extends CI_Controller {

    public function example_deletion($id) {
        //deletion code... and whatever you want
    }

}

Then you access it like:
yourdomain.com/example/example_deletion/$id,
if you had more arguments in your method, it would look like
yourdomain.com/controller/method/$arg1/$arg2/... etc.,
but if you wanna keep it with GET approach you access it via
Code:
$this->input->get('name_of_your_get_value');



How to get 'id' from URL? - El Forum - 01-17-2014

[eluser]InsiteFX[/eluser]
or use CI uri_segment method



How to get 'id' from URL? - El Forum - 01-19-2014

[eluser]tahakirmani[/eluser]
Thank You So Much! You Guys are Great ! Smile


How to get 'id' from URL? - El Forum - 01-20-2014

[eluser]Scared[/eluser]
[quote author="noideawhattotypehere" date="1389946186"]
Code:
class Example extends CI_Controller {

    public function example_deletion($id) {
        //deletion code... and whatever you want
    }

}

Then you access it like:
yourdomain.com/example/example_deletion/$id,
if you had more arguments in your method, it would look like
yourdomain.com/controller/method/$arg1/$arg2/... etc.,
but if you wanna keep it with GET approach you access it via
Code:
$this->input->get('name_of_your_get_value');
[/quote]

Sorry, newbie question, am I right in thinking you have to setup the routing accordingly for this to work? What would the routing rule look like? Would it be this?:

Code:
$route['example/example_deletion/(:any)'] = 'example/example_deletion/$1';



How to get 'id' from URL? - El Forum - 01-21-2014

[eluser]noideawhattotypehere[/eluser]
[quote author="Scared" date="1390226886"][quote author="noideawhattotypehere" date="1389946186"]
Code:
class Example extends CI_Controller {

    public function example_deletion($id) {
        //deletion code... and whatever you want
    }

}

Then you access it like:
yourdomain.com/example/example_deletion/$id,
if you had more arguments in your method, it would look like
yourdomain.com/controller/method/$arg1/$arg2/... etc.,
but if you wanna keep it with GET approach you access it via
Code:
$this->input->get('name_of_your_get_value');
[/quote]

Sorry, newbie question, am I right in thinking you have to setup the routing accordingly for this to work? What would the routing rule look like? Would it be this?:

Code:
$route['example/example_deletion/(:any)'] = 'example/example_deletion/$1';
[/quote]

You could setup other route if you want in routes.php, but by default codeigniter urls looks like this:
base_url/controller/method/param1/param2/.../
this means that your example doesnt need any entries in routes.php


How to get 'id' from URL? - El Forum - 01-21-2014

[eluser]Scared[/eluser]
Thank you!


How to get 'id' from URL? - El Forum - 01-21-2014

[eluser]InsiteFX[/eluser]
Users Guide URI Class