Displayed the names of categories from the database in the menu, used the trace code

<?php $categories_q = mysqli_query($connection, "SELECT * FROM `articles_categories` "); $categories = array(); while( $cat = mysqli_fetch_assoc($categories_q) ) { $categories[] = $cat; } ?> <?php foreach ( $categories as $cat ) { ?> <li><a href="/articles.php?categorie=<?php echo $cat['id']; ?>"><?php echo $cat['title']; ?></a></li> <?php } ?> 

Please tell me how to implement that from the menu in categories opened all the goods in this category.

When opening, the link is http: //app/articles.php? Categorie = 1 And the category number at the end changes depending on the category

By registering the category id number, but how to make all the products of this category open from there, now the link is simply not found

  • at least describe the get request handler for the requested page. - Kirill Korushkin
  • updated, added everything I use - Alexey Sokolov

2 answers 2

See you have a articles.php page
add such code to it.

 $query = 'SELECT * FROM articles WHERE category_id =' . $_GET['categorie']; $articles_from_cat = mysqli_query($connection , $query); 

According to the ideas in $ articles_form_cat, an array will come with all the articles from the category that you submit after the question in the URL.
In the database in the articles table, you should have a category_id field that stores the id of the desired category.

PS Learn how to learn how)) He explained everything there. Listen carefully.

  • Yes, I learn how to do it by howdy)) I brought the goods out of a certain category, everything that I showed up there, did not work out at the end, where the output comes from the menu - Alexey Sokolov
  • I can of course stick and create a separate file for each category and prescribe $ articles = mysqli_query ($ connection, "SELECT * FROM articles WHERE categorie_id = 2 ORDER BY id DESC LIMIT 99"); and display categories like this - Alexey Sokolov
  • If you want you can add me to VC, I went through all these incomprehensible things, I will answer if something quickly vk.com/ozopactus - singlesly
  • but how to make it so that it doesn’t work out right away from the menu on the right product category - Alexey Sokolov
  • Add to VK I will tell you this whole topic. - singlesly

on the articles.php page:

 if ($_GET['categorie']) { // если запрос содержит переменную categorie (category/categories) $articles = mysqli_query($connection, "SELECT * FROM `articles` WHERE `category_id` = '" . (int)$_GET['categorie'] . "'"); // тащите посты с переданным id категории. } 

get / post howto