简体   繁体   English

如何从codeigniter中的URL获取id?

[英]How to get id from URL in codeigniter?

I am trying to get 'id' from URL in order to delete that specific selected product.我试图从 URL 获取“id”以删除特定的选定产品。 I have used $this->uri->segment(3) but its not getting any value from URL.我使用了$this->uri->segment(3)但它没有从 URL 获得任何价值。 Although i can see the 'product_id' in my URL.虽然我可以在我的 URL 中看到“product_id”。

If i used $this->url->segment(2) it gives me the second value form URL, but i am unable to get product Id.如果我使用$this->url->segment(2)它会给我第二个值表单 URL,但我无法获得产品 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.当我单击 $name 时会生成以下 URL。

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');
    }

检查下面的代码希望你得到你的参数

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

if you have to pass the value you should enter url like this如果你必须传递值,你应该像这样输入 url

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;
}
$product_id = $this->input->get('id', TRUE);
echo $product_id;

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

  }

I think you're using the CodeIgniter URI routing wrong: http://ellislab.com/codeigniter%20/user-guide/general/routing.html我认为您使用的 CodeIgniter URI 路由错误: 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.基本上,如果您有一个 Products_controller 的方法称为delete($id)并且您创建的 url 的格式为http://localhost/designs2/index.php/products_controller/delete/4 ,则删除函数将收到$id作为参数。

Using what you have there I think you can get the id by using $this->input->get('product_id);使用你拥有的东西,我认为你可以通过使用$this->input->get('product_id);来获取 id $this->input->get('product_id);

In codeigniter you can't pass parameters in the url as you are doing in core php.So remove the "?"在 codeigniter 中,您不能像在核心 php 中那样在 url 中传递参数。所以删除“?” and "product_id" and simply pass the id.If you want more security you can encrypt the id and pass it.和“product_id”并简单地传递id。如果你想要更多的安全性,你可以加密id并传递它。

有点晚了,但这对我有用

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

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM