How can I show certain products by clicking on a link to another page?

1

I'm having a problem, no matter how hard I try, I can not solve it. The problem is:

<form action="" method="POST">
    <div class="containercompras">
        <div class="box">
            <div class="imgBox">
                <img class="imagem" src="images/imgbox1.jpg">
            </div>
            <div class="descricao">
                <div class="contentdescr">
                    <h2>Sombras</h2>
                    <p><a href="aaa.php" name="versombras">Ver mais</a></p>
                </div>
            </div>      
        </div>
        <div class="box">
            <div class="imgBox">
                <img class="imagem" src="images/imgbox2.jpg">
            </div>
            <div class="descricao">
                <div class="contentdescr">
                    <h2>Rimel</h2>
                    <p><a href="">Ver mais</a></p>
                </div>
            </div>
        </div>

In the part where I have to see more supposedly when loading should go to another page and show certain products, I will fetch them from the database. They on the other page works perfectly, I just can not do that by clicking on "See more" go to the other page and show only those products of a single category ..

On the other page I did so for all categories and gave:

<?php
header('Content-Type: text/html;charset=UTF-8');
include("ligabd.php");

if (isset($_POST['sombras'])) {
    $procura = "SELECT * FROM produtos WHERE id_categoria='1'";
   ...
?>
    
asked by anonymous 15.05.2018 / 15:42

1 answer

2

You can do this by placing parameters in your href to pick up the page where you want them to list certain items. For example like this:

I'll use the piece of your "Shadows" item code

<div class="descricao">
                <div class="contentdescr">
                    <h2>Sombras</h2>
                    <p><a href="aaa.php" name="versombras">Ver mais</a></p>
                </div>

In the href of line <p><a href="aaa.php" name="versombras">Ver mais</a></p> you add an ID parameter, for example, I will put the parameter ? id = 1 , thus:

<p><a href="aaa.php?id=1" name="versombras">Ver mais</a></p>

And so you add in as many links as you have (but I recommend a cont ++ or another attribute if it's multiple types of items).

Then on the "aaa.php" page you may notice that clicking See more will link to "aaa.php? id = 1". So you use this line in your page

$id = (int) $_GET['id'];

This line will get the value (integer) corresponding to your URL parameter (in the case of shadows, it will get value 1), so you put this in your sql query,

    $procura = "SELECT * FROM produtos WHERE id_categoria='$id'";
   ...

So whenever you click on a link, the page where you're going to list those items will search the database using the URL ID parameter to make the specific request, and it will list right on the page.

The complete code looks like this:

index.php:

<form action="" method="POST">
    <div class="containercompras">
        <div class="box">
            <div class="imgBox">
                <img class="imagem" src="images/imgbox1.jpg">
            </div>
            <div class="descricao">
                <div class="contentdescr">
                    <h2>Sombras</h2>
                    <p><a href="aaa.php?id=1" name="versombras">Ver mais</a></p>
                </div>
            </div>      
        </div>
        <div class="box">
            <div class="imgBox">
                <img class="imagem" src="images/imgbox2.jpg">
            </div>
            <div class="descricao">
                <div class="contentdescr">
                    <h2>Rimel</h2>
                    <p><a href="aaa.php?id=2">Ver mais</a></p>
                </div>
            </div>
        </div>

aaa.php :

<?php
//inicia session
session_start();

//header
header('Content-Type: text/html;charset=UTF-8');

//inclui o arquivo do banco
include("ligabd.php");

//pega o id do item no link
$id = (int) $_GET['id'];

//listar as compras
  $procura = "SELECT * FROM produtos WHERE id_categoria='$id' ";

//query para imprimir as buscas
  $faz_procura = mysqli_query($link, $procura);
?>
<html>
  <body>
      <table id="itens" border="2">
          <thead>
              <tr>
                <td>ID</td>
                <td>Item</td>
                <td>Preço</td>
              </tr>
          </thead>
        <tbody>
    <?php

//while para listar todos os dados baseados no ID_CATEGORIA
  while ($row = mysqli_fetch_assoc($faz_procura)){
    ?>

    <tr>
          <td><?php echo $row['id_produto'] ?> </td>
          <td><?php echo $row['nome_produto'] ?> </td>
          <td><?php echo $row['preco_produto'] ?> </td>
    </tr>
  <?php }; ?>

    </tbody>
   </table>
  </body>
 </html>
    
15.05.2018 / 15:56