How to pass data via get?

Asked

Viewed 1,989 times

1

I have a problem. I need to create a page that lists all records with the selected category.

Code where the person selects the category and when clicking, send to page. (This code is working in perfect condition, just put for better understanding.)

<h2 class="titulos">Categorias</h2>
   <div class="cat-lista list-group">
      <?php  
         $contador = mysql_query("SELECT DISTINCT categoria, count(id) as catqtde FROM postagens group by categoria");

         while($prod = mysql_fetch_array($contador)) { 

      ?>

     <a href="categoria.php" name="categoria">
        <?php echo $prod['categoria']; ?>
           <span class="badge bt-span badge-pill"><?php echo $prod['catqtde']; ?></span>
     </a>
        <?php } ?>
</div>

Page code that should list the data with such a category.

<?php 
    $ident = $_GET['categoria'];

$sqlstring = "SELECT * from postagens where categoria = $ident order by id DESC ";
  $query = @mysqli_query($mysql->con, $sqlstring);



  while ($dados = @mysqli_fetch_array($query)){
      setlocale(LC_TIME,'pt_BR','pt_BR.utf-8','portuguese');
      date_default_timezone_set('America/Sao_Paulo');
      $datinha = date('d/m/Y', strtotime($dados['agendado']));
?>


        <div class="col-lg-3 col-md-6 col-sm-12 col-all-post" > 
            <div class="card post-item shadow">
                <a href="postagem.php?id=<?php echo $dados['id'] ?>&<?php echo slug($dados['titulo'])  ?>"><img class="card-img-top post-item-img" src="<?="admin/img/".$dados['foto']?>"></a>
                <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div class="categoria-span cat-span-post">    
                        <a href="#"><span><?=$dados['categoria']?></span></a>
                    </div><!--categoria-span-->
                    <div class="info-span-blog">
                        <span><img src="assets/img/icon-cal.png"><?= $datinha ?>  </span>
                    </div><!--info-span-->
                </div>
                    <a href="postagem/<?php echo $dados['id'] ?>/<?php echo slug($dados['titulo'])  ?>"><h4 class="card-title d-flex align-items-end"><?=$dados['titulo'];?></h4></a>
                        <a href="postagem/<?php echo $dados['id'] ?>/<?php echo slug($dados['titulo'])  ?>"><h5 class="card-title"><?=$dados['subtitulo']?></h5></a>
                </div><!--card-body-->
            </div><!--card-->
        </div><!--col-lg-->



 <?php   

}
$mysql->fechar();

?>

    </div>

The mistake I see is this:

Notice: Undefined index: categoria in D: Usbwebserver files 2 root site categoria.php on line 10

2 answers

0

In this stretch here:

<a href="categoria.php" name="categoria">

do so:

<a href="categoria.php?categoria=<?php $prod['categoria']; ?> " name="categoria">

Let’s say you have a category called Test, the url will look like this:

categoria.php?categoria=teste, thus, this passage $ident = $_GET['categoria']; will recognize the GET variable.

Explanation

When you want to pass something by GET, always put it in the URL like this:

url.php?nomedoparamento=valor

If you want more than one parameter, pass it using &:

url.php?parametro1=teste&parametro2=teste2

0


To get the data sent by GET you need to put the parameters in your href. See:

<a href="categoria.php?categoria=valorDoParametro" name="categoria">
    <?php echo $prod['categoria']; ?>
       <span class="badge bt-span badge-pill"><?php echo $prod['catqtde']; ?></span>
 </a>

So on the other page you take this amount the way you did it

$ident = $_GET['categoria'];

What you are missing is when declaring the parameter via link, it doesn’t work if you just put a "name" in your href.

Browser other questions tagged

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