How to display certain products when clicking a link to another page?

Asked

Viewed 98 times

1

I’m having a problem, no matter how hard I try, I can’t fix it.. The problem is in:

<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>

The part where I have to see more I’m supposed to load and go to another page and show certain products, which I’ll get from the database. They on the other page works perfectly, I just can’t do that by pressing "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'";
   ...
?>

1 answer

2


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

I’m going to use the piece of your code from the items "Shadows"

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

On the href of the line <p><a href="aaa.php" name="versombras">Ver mais</a></p> you add an identification parameter, as an example, I will put the parameter ?id=1, being like this:

<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 other attribute if it’s multiple types of items).

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

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

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

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

So whenever you click on a link, the page where you will list these items, will do a database search using the URL identification parameter to make the specific request, and will list neat and neat on the page.

The whole code goes 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>
  • Thanks for the help!! But in the code part --> $search = "SELECT * FROM products WHERE id_categoria='$id' "; (Pag. aaa.php) how does he get the category 1 products, where it is "id_categoria='$id' "? From what you said, would you have to add this line of code, or change the one I mentioned in the question? (pág aaa.php)

  • So Ana, did you see that the link is aaa.php? id=1 ?? Did you see that you have after php (?) a parameter? In php you can pass values through the link, and inside the page you can call the value of this parameter, so it’s WHERE id_category ='$id' ", because the $id is worth 1(pq on the link is aaa.php?id=1 <-) and php is reading this, you see id_categoria = '$id', but PHP knows that the $id is worth 1, so it sees id_categoria = 1, got it?

  • Do a test, on the aaa.php page, type just below the $id = (int) $_GET['id']; this one: echo $id; Go to the two "see more" buttons and you will see that it shows on the screen, the number 1 in "Shadows" and the number 2 in "Rimel"

  • Yes I tested and gave, but only if you delete the rest of the code " if (isset($_POST['shadows'])) { $search = "SELECT * FROM products WHERE id_categoria='1'"; }elseif (isset($_POST['rimel'])) { $search = "SELECT * FROM products WHERE id_categoria='2'"; ------ And I need this code for when to go to page appear all products, but however as you said it worked, my question now is, how I integrate that into my code so that I don’t erase what I’ve been doing?

  • I at this point simply put the part of the code you suggested over the "if(isset($_POST['shadows'])) { $search= ... }

  • Yes yes, but it was intentional to remove this part, because in my understanding you used it to see if when you selected the "see more" of the "shadows" you searched by id_categoria=1, and consequently by 2. This search will only search equivalent to the item you clicked on index.php. I don’t know if I made myself clear, but you don’t have to use the isset anymore.

  • Do so, tell me what is the ID equivalent to the item Shadows in your database.

  • The ID of the item shadows in my database is 1 You’ve helped me a lot so far, I’m not being able to integrate into the code! But if I don’t have isset by pressing the button no product appears ...

  • Let’s do it this way, post all your code from aaa.php, just so I can analyze all your syntax.. I think there must be a "}" lost.. Hence the syntax error and total PHP failure.. So you don’t list anything.. Update your question with all of aaa.php to set it up.

  • I already posted, if you can help me, I’d really appreciate it, this is my final exam! I posted as an answer to my own question, I forgot to update the question...

Show 5 more comments

Browser other questions tagged

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