How do I receive the $_GET value of a URL in Codeigniter

Asked

Viewed 6,755 times

4

I am sending a value get through a link for example:

<a href="/admin/editar_user/5">Editar</a>

And there in the method of my controller calling the view I get:

public function editar_user(){
    $this->load->view('admin/editar_user');
}

So far he’s calling the view the way I wanted, but I want to know how I get that value sent via get which in that case would be 5

for example:

public function editar_user(){
        // comando pegar valor get  5
        $this->load->view('admin/editar_user');
    }

How I take the amount get?

3 answers

7

if you are using
website.com.br/index.php/products/shoes/type/123
will be a parameter in the controller
creating the method like this:

public function sapatos($tipo, $id){
    echo $tipo;
    echo $id;
}

if you use the url like this:
like: site.com.br/index.php/products/shoes/? type=1&id=123
The method has to stay like this:

public function sapatos(){
    echo $this->input->get('tipo'); // recupera a informação via get
    echo $this->input->get('id');
} 

7


You can use the class:

URI Class

The URI Class provides functions that help you Retrieve information from your URI strings. If you use URI routing, you can also Retrieve information about the re-routed Segments.

Basically it helps you to recover segments from a URL.

$this->uri->segment(n)

Where n is the number of the segment you want to return.

Passing parameters in the Controller

Example of URL:

site.com.br/index.php/produtos/sapatos/tipo/123

Controller

<?php
class Produtos extends CI_Controller {
    public function sapatos($tipo, $id)
    {
        echo $tipo;
        echo $id;
    }
}
?>
  • Thanks, I’m doing some tests when it works :)

  • @Silvioandorinha I haven’t used CI for a long time, I believe it is. But I have a certain passion for being a first framework. rs

  • Okay... it worked

2

In the editar_user() define an argument for example $id, when the user clicks on the link the value 5 will be assigned to $id

public function editar_user($id){
        echo $id;
        $this->load->view('admin/editar_user')
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.