Subcategory listing according to selected category

Asked

Viewed 573 times

1

I have the following code snippet:

<?php
      foreach($categorias as $cat){
          $id_cat = $cat['id'];
          $subcategorias = array();
          $sqlsub = Comando::Executar("SELECT * FROM subcategoria WHERE cat_pai = '$id_cat' ");
              while($subarray = $sqlsub->fetch_assoc()){
                   $subcategorias[] = $subarray;
              }
              echo '<select class="btn models '.$cat['nome'].'" name="p_subcategoria" id="p_subcategoria">';
              foreach($subcategorias as $subcat_array){
                  echo '<option value='.$subcat_array['id'].'>'.$subcat_array['nome'].'</option>';
              }
              echo '</select>';
}
?>

The Subcategories array is as follows :

Array
(
    [0] => Array
        (
            [id] => 6
            [nome] => Carnes
            [cat_pai] => 3
        )

    [1] => Array
        (
            [id] => 7
            [nome] => Massas
            [cat_pai] => 3
        )

    [2] => Array
        (
            [id] => 8
            [nome] => Sopas
            [cat_pai] => 3
        )

    [3] => Array
        (
            [id] => 9
            [nome] => Saladas
            [cat_pai] => 3
        )

)
Array
(
    [0] => Array
        (
            [id] => 10
            [nome] => Vinhos
            [cat_pai] => 4
        )

    [1] => Array
        (
            [id] => 11
            [nome] => Cervejas
            [cat_pai] => 4
        )

    [2] => Array
        (
            [id] => 12
            [nome] => Sucos
            [cat_pai] => 4
        )

)
Array
(
    [0] => Array
        (
            [id] => 13
            [nome] => Pudim
            [cat_pai] => 5
        )

    [1] => Array
        (
            [id] => 14
            [nome] => Doces
            [cat_pai] => 5
        )

    [2] => Array
        (
            [id] => 15
            [nome] => Geleias
            [cat_pai] => 5
        )

)
Array
(
    [0] => Array
        (
            [id] => 16
            [nome] => Balas
            [cat_pai] => 6
        )

    [1] => Array
        (
            [id] => 17
            [nome] => Aperitivos
            [cat_pai] => 6
        )

)

But whenever I go to check on the ID value that should appear in the OPTION value it returns to Bullet. First index of the latter array.

I’m making a mistake in logic but I still don’t know where.

1 answer

1

Try changing your code to:

foreach($categorias as $cat){
    $id_cat = $cat['id'];
    $subcategorias = array();
    $sqlsub = Comando::Executar("SELECT * FROM subcategoria WHERE cat_pai = '$id_cat' ");
    while($subarray = $sqlsub->fetch_assoc()){
        $temp = array(
            'id' => $subarray['id'],
            'nome' => $subarray['nome'],
            'cat_pai' => $subarray['cat_pai'],
        );
        $subcategorias[] = $temp;
    }
    echo '<select class="btn models '.$cat['nome'].'" name="p_subcategoria" id="p_subcategoria">';
    foreach($subcategorias as $subcat_array){
        echo '<option value='.$subcat_array['id'].'>'.$subcat_array['nome'].'</option>';
    }
    echo '</select>';
}

I imagine the result returned by the method fetch_assoc is a vector with values passed by reference.

  • Eai Functioned ?

  • Alexander thank you so much for your attention! To clarify, the way you gave me is more organized to insert this data into the perfect working array and the way I was doing it also works the same way. So the mistake was not there. It was something more idiotic and simple. The first loop there is a foreach so every time it ran it went back to the beginning and declared EMPTY ARRAY again . There was my mistake. That’s why it printed the value of BULLET. It was the last array being created. So declaring the array out of code solved my problem. :)

  • If the problem has been solved, post the answer here and as soon as possible, mark it as the answer.

Browser other questions tagged

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