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

Asked

Viewed 454 times

0

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

2 answers

1


Depending on what solution you want, you can deal directly with 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 just retrieve the id on the page where you made the reference:

idProduto = $_GET['id'];

I hope to have help Eduarda...

1

Your route archive must 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');
        }
}

Note that in the view you have to pass the list of products, so 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 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 that you specified via GET. Ai yes no create a method detail in your controller to treat this id of the product in the best possible way.

  • Thank you for your help!

Browser other questions tagged

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