How to make a Loop work within another Loop in the same code

Asked

Viewed 192 times

-3

I’m trying to list two categories registered in the comic in the table "CATEGORY", as shown below:

Table Category

inserir a descrição da imagem aqui

But whenever I enable the page "pag_presentation.php" where I list the products, the category list is limited to only the category "Pivoting Doors", ignoring the other with the name "Tamps", and bringing me all the products registered in the Table "PRODUCTS" as shown below:

inserir a descrição da imagem aqui

When I disable the page "pag_presentation.php", the two categories are shown, but none of the products registered in the products table is listed, when selected any of the categories.

I ask friends, a tip on how to make me can list the two categories, and bring only the products for the selected category.

Below I’m posting the codes I’m using:

Listing the Categories:

<!-- Listando os Categorias -->
<?php
$codigo = $_POST['codigo'];
$nome_cat = $_POST['nome_cat'];
$imagem = $_POST['imagem'];
$query = mysql_query("SELECT * FROM categoria");
while($res = mysql_fetch_array($query)){
?>
<div style="float:left; width:112px; height:133px; padding:2px 10px;" id="cats-list">
    <a class="cat-link" href="javascript:;" title="<?php echo $res['nome_cat']; ?>">
        <img style="position:relative; top:50%; transform:translateY(-50%);" src="img_cate/<?php echo $res['imagem']; ?>" width="100" title="<?php echo $res['nome_cat']; ?>" />
    </a>
    <div style="background:#2f2140; margin-left:-300px; width:880px;z-index:999;border-radius:25px;" class="single-cat">
        <h1 align="center" style="width:830px; margin-top:-40px;"><?php echo $res['nome_cat']; ?></h1>
        <div style="margin-left:357px;width:50px;" class="cat-links">
            <a class="close-btn" href="javascript:;" title="Voltar">
                <img src="img/fechar.jpg" />
            </a>
        </div>
<!-- Listando os Produtos por Categoria -->
<?php include "pag_lista_produtos.php"; ?>                  
    </div>
</div>
<?php
}
?>

Listing Products By Selected Category: "pag_lista_produtos.php"

<!-- Listando os produtos -->
<?php
include "conexao.php";
$codigo = $_POST['codigo'];
$imagem = $_POST['imagem'];
$titulo = $_POST['titulo'];
$descricao = $_POST['descricao'];
$query = mysql_query("SELECT * FROM produtos");
while($res = mysql_fetch_array($query)){
?>
<div style="float:left; width:112px; height:133px; padding:2px 10px;" id="songs-list">
    <a class="song-link" href="javascript:;" title="">
        <img style="position:relative; top:50%; transform:translateY(-50%);" src="img_prod/<?php echo $res['imagem']; ?>" width="100" title="<?php echo $res['titulo']; ?>" />
    </a>
    <div style=" margin-top:120px;z-index:999;border-bottom-right-radius:25px;border-top-right-radius:25px;" class="single-song">
        <div style="width:50px;margin-left:300px;" class="song-links">
            <a class="close-btn" href="javascript:;" title="Voltar">
                <img src="img/fechar.jpg" />
            </a>
            <div class="song-sides">
                <img style="margin:40px 0 0 -142px;" src="img_prod/<?php echo $res['imagem']; ?>" width="369" />
            </div>
        </div>
        <h1> <?php echo $res['titulo']; ?> </h1>
        <div class="entry" style="overflow-y: hidden; padding: 0px; width: 100px; background:#fff;">
            <p style="font-family:Verdana, Geneva, sans-serif; size:14px; font-weight:bold; color:#ccc;">
            <?php echo nl2br($res['descricao']); ?>
            </p>
            <div class="jspContainer" style="width: 100px; height: auto;">
            <div class="jspPane" style="padding: 0px; top: 0px; width: 100px;">
            </div></div></div>
            <span class="song-sides left-side"></span>
        </div>
    </div>
<?php
}
?>

I would be very grateful if friends could help me solve these problems.

  • You are using the "fetch_array" function and taking the field by the key name... Try using take by column number $res[0] or see if it works by swapping for "fetch_assoc"

  • A tip: You created the category table but you’re saving their name in the product table. The correct is to store the category code in the product table...

1 answer

2


Simple, you can make a loop inside another loop and name the variable as in the outside loop. What happens is that the variables $query, $code and $image replace the loop of the categories. Try renaming the second loop variables as $query_product, $product code, etc.

  • Good morning @Samuel Pacheco, I followed your tip and it worked, now I can bring and view the two categories registered in the comic book. Abusing a little more, would have as you explain me how I can direct to the products referring to the selected category, because for now whenever I select any of the two, the result is always the same, bringing all products registered in the table "PRODUCTS". I tried to do the href thus HREF, with a Get thus GET, but without success

  • The address to check this at the moment is: (http://www.lccinformatica.com.br/). If the friend can give me more help, how to direct the categories to their relative products, I will be grateful. And from now on my thanks for the attention paid to my problem.

  • I can’t understand your code because it’s so messed up. As you are starting to learn, I suggest trying with something simpler, such as just listing categories and products, then try to implement the design and scripts js.

  • Thanks @Samuel Pacheco, BRIGADÃO.

Browser other questions tagged

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