How do I pass an id through the url using CodeIgniter?

0

I need to pass the id of a product through the url using the GET method, in codeIgniter what would be the solution?

    
asked by anonymous 11.10.2018 / 16:50

2 answers

1

Depending on what solution you want, you can address directly through the URL itself. You have to bring the id by query and add in your reference. Ex:

<a href="<?= base_url('menu/produto') ?>?id=<?= $id ?>">Produto01</a>

And then you just retrieve the id on the page where you made the reference:

idProduto = $_GET['id'];

I hope to have help Eduarda ...

    
11.10.2018 / 19:39
1

Your route file should have two routes: One to list all products and one to list a specific product by product ID.

routes.php

//Lista produto
$route['produto'] = 'produto/index';

//Lista produto especifico
$route['produto/:id'] = 'produto/detalhe';

Product (Controller of your application)

<?php
class Produto extends CI_Controller {

        public function index()
        {
            $produtos[]  = [];
            $produtos[] = ['id' => 1, 'nome' => 'Televisão', 'descricao' => 'Televisao com 32 Polegadas']
            $produtos[] = ['id' => 2, 'nome' => 'Blusa', 'descricao' => 'Blusa verde tamanho M']
            $produtos[] = ['id' => 3, 'nome' => 'SmartPhone', 'descricao' => 'SmartPhone LG K10']

            $this->view('index', compact('$produtos');
        }
}

Notice that in view you have to go through the list of products so that it can be listed in your view

index.php (View of your application)

<?php foreach($p as $produtos) { ?>
    <div class="container">
        <div>
            <?= $p->nome; ?>
        </div>
        <div>
            <?= $p->descricao; ?>
        </div>
        <a href="/produto/<?= $p->id; ?>">Detalhes do Produto</a>
    </div>
<?php } ?>

The big balcony here is that you use the href (a link) concatenated with the id of your product so that by clicking this link you can be redirected to the route you specified via GET . So yes, do not create a detail method in your controller to treat this id product as best you can.

    
11.10.2018 / 17:09