Please help me filter by category in PHP?

Asked

Viewed 100 times

0

have 2 tables , the articles table and the table categorie_article in the database, what I want is the following, that appear all the names of registered categories (ex CATEGORIA1, categoria2, etc) and when clicking the category goes to a category.php file where it displays all the posts of the category I clicked, regardless of the category clicked goes in this document, could help me please look below the code I made only appears the name and ID of the category, based on it I complete the code and the category.php by kindness ? Cod below =

<?php
                    $result_categoria = "SELECT * FROM artigos WHERE sts_situacoe_id=1 ORDER BY id DESC LIMIT 6";
                    $resultado_categoria = mysqli_query($conn, $result_categoria);
                    ?>
                    <div class="p-3">
                        <h4>Categorias</h4>
                        <ol class="list-unstyled" data-categoria="comida">>

                            <?php
                            while($row_categoria = mysqli_fetch_assoc($resultado_categoria)){
                                echo "<li><a href='".pg."/artigo/".$row_categoria['id']."'>".$row_categoria['nome']."</a></li>";
                            }
                            ?>

                        </ol>
                    </div>

1 answer

-1

can resolve this issue by passing your category id via GET url, and in your category.php file, validate and take the GET value that would be the id of the category and perform a query in the articles table by taking all the articles in that category that I believe is your foreign key in the articles table.

In...

<?php
     while($row_categoria = mysqli_fetch_assoc($resultado_categoria)){
       echo "<li><a href='".pg."/artigo/".$row_categoria['id']."'>".$row_categoria['nome']."</a></li>";
     }
?>

Modify your href to pass the GET parameter this way...

echo "<li><a href='".pg."/artigo/categoria.php?categoria=".$row_categoria['id']."'>".$row_categoria['nome']."</a></li>";

That done you already have the possibility to validate this in the file, here I am considering that your query is performed in the file category.php, if this validation is performed in another file, The logic is the same, only needing to change the place where you will send the GET to validation. In the category.php file take the value of the parameter and query your table this way...

// verificar se o parâmetro GET existe e se não é vazio
if(isset($_GET['categoria']) && !empty($_GET['categoria'])){
   $id = $_GET['categoria'];
   //.... Query para realizar a consulta e listar os resultados
} else {
   //... parâmetro GET não foi setado ou é vazio, faça o que desejar como mostrar aviso ou redirecionar para outra página
}

That should solve your question simply. I hope I’ve helped, good studies!

EDITION:

Leticia, I will change a little the way you wrote yours, it is the same thing but in a different way of writing in html php, it left more readable and I realized that it is concatenating ". pg". in its condition, without the $symbol, this pg is a global constant? If it is, just take the $, follow the change...

<div class="p-3"> <h4>Categorias</h4> 
  <ol class="list-unstyled"> 
    <?php while($row_categoria = mysqli_fetch_assoc($resultado_categoria)): ?>
    <li><a href='<?php echo $pg; ?>/artigo/categoria.php?categoria=<?php echo $row_categoria['id'] ?>' ><?php echo $row_categoria['nome']; ?></a></li>
    <?php endwhile; ?>
  </ol>
<div>
  • Thanks Junior for the help, but I tried here and it’s giving error, there is no value appearing,I want to appear category names , I tried to see but I could not enter the error, I did some tests and it did not work, .. can you see for me ? and yes the table articles have a key rule article_categoria_id to get the value of the table category.

  • Could give a print_r($resultado_category) so I can help you better.

  • edited there, I accidentally added the >.. Oops! , but it looks like nothing came out. I changed my question above, because I was wrong > haha sorry

  • <?php $result_categoria = "SELECT * FROM articles where sts_situacoe_id=1 ORDER BY id DESC LIMIT 6"; $resultado_categoria = mysqli_query($Conn, $result_categoria); ?>

  • <div class="p-3"> <H4>Categories</H4> <ol class="list-unstyled"> <?php while($row_categoria = mysqli_fetch_assoc($resultado_categoria)){ echo "<li><a href='".pg."/article/category.php? category=". $row_category['id']." '>". $row_category['name']." </a></li>";

  • } ? > </ol> </div>

  • Leticia, put this print_r in the post editing it.

  • ?php $result_category = "SELECT * FROM articles WHERE sts_situacoe_id=1 ORDER BY id DESC LIMIT 6"; $resultado_category = mysqli_query($Conn, $result_category); ? > <div class="p-3"> <H4>Categories</H4> <ol class="list-unstyled"> <? php while($row_categoria = mysqli_fetch_assoc($resultado_categoria)){ echo "<li><a href='".pg."/article/category.php? category=". $row_categoria['id']." '>". $row_categoria['name']." </a></li>"; } ? > </ol> </div>

  • Ready Cod complete above

  • I edited my reply with the corrections I thought necessary.

  • Too bad we can not chat by chat Junior, now appeared the categories uhuhu, but is giving page not found now in the category.php <- as if there was no Uch! If I learn this I can do and finish the rest of the project, that’s pretty much all that’s missing oh Gosh! I’ll have to leave now, tomorrow I’ll continue, but if you know the reason and can warn

  • If the page is not found, you need to check the path to it in href, enter your code in <li> and see what it shows in href of them, analyze to see where the error is, if you prefer, copy the path and paste in the url in the browser and see if the page opens.

  • Good morning Junior, I managed in the address url on the page I took the $ before the pg and the category=, directs however it is not displaying the posts, within the query in the category.php I put for normal display, but nothing. ah sorry work, if you want to give up helping me all right I understand, worse than after learning if you say "puutz so simple" had things that I delayed after I learned it was super easy... this one I’m suffering

  • check if your query is bringing results, check what value appears in the GET variable, you are handling possible errors in your query? if not, print your query string, copy and in phpmyadmin go to the table and make an SQL with your query and see what the phpmyadmin panel gives you as an answer.

Show 9 more comments

Browser other questions tagged

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