Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get id from URL in codeigniter?

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');
    }
like image 849
Taha Kirmani Avatar asked Jan 17 '14 07:01

Taha Kirmani


People also ask

How to pass id in url in php CodeIgniter?

PHP Code: $id = $this->getId();

What is Uri segment in CodeIgniter?

$this->uri->segment(n) Segment function allow you to retrieve a specific segment form URI string where n is a segment number. Segments are numbered from left to right. For example,if your URI like. By the above example URI segment function give result by n parameter.


5 Answers

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;
}
like image 110
Agha Umair Ahmed Avatar answered Nov 01 '22 04:11

Agha Umair Ahmed


Check the below code hope that you get your parameter

echo $this->uri->segment('3');
like image 40
Md.Jewel Mia Avatar answered Nov 01 '22 04:11

Md.Jewel Mia


$product_id = $this->input->get('id', TRUE);
echo $product_id;
like image 6
raj Avatar answered Nov 01 '22 03:11

raj


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

  }
like image 2
codeigniter leaner Avatar answered Nov 01 '22 03:11

codeigniter leaner


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.

like image 1
CodeCanyon Avatar answered Nov 01 '22 03:11

CodeCanyon