12

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.

VIEW

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

The following URL is generating when i click on $name.

http://localhost/designs2/index.php/products_controller/delete_controller?product_id=70

Controller

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

9 Answers 9

23

Check the below code hope that you get your parameter

echo $this->uri->segment('3');
Sign up to request clarification or add additional context in comments.

1 Comment

this is old way and it's not good practice to use uri because in this case you bount it on second or thired segment. but @agha's way is greate.
22

if you have to pass the value you should enter url like this

localhost/yoururl/index.php/products_controller/delete_controller/70

and in controller function you can read like this

function delete_controller( $product_id = NULL ) {
  echo $product_id;
}

1 Comment

Thanks! i removed ?product_id= from <a> tag, and it gave me the perfect URL.
6
$product_id = $this->input->get('id', TRUE);
echo $product_id;

Comments

2

View page

  <a href="<?php echo base_url();?>products_controller/delete_controller/<?php echo $product_id;?>"><?php echo $name; ?></a>

controller page

  function delete_controller( $product_id) {

         echo $product_id;
         //add your logic

  }

Comments

1

In codeigniter you can't pass parameters in the url as you are doing in core php.So remove the "?" and "product_id" and simply pass the id.If you want more security you can encrypt the id and pass it.

Comments

1

I think you're using the CodeIgniter URI routing wrong: http://ellislab.com/codeigniter%20/user-guide/general/routing.html

Basically if you had a Products_controller with a method called delete($id) and the url you created was of the form http://localhost/designs2/index.php/products_controller/delete/4, the delete function would receive the $id as a parameter.

Using what you have there I think you can get the id by using $this->input->get('product_id);

Comments

1

A bit late but this worked for me

  $data_id = $this->input->get('name_of_field');

Comments

0
$CI =& get_instance();

if($CI->input->get('id'){
    $id = $CI->input->get('id');

}

1 Comment

It would be nice to explain your thoughts and process, instead of just showing code. Will probably result in helping more people :)
0

n is your last sagment of you url

$id = $this->uri->segment(n);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.