Subcategory listing according to selected category

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 Subcategory 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 value of the ID that should appear in the OPTION value it returns the Bullet. First index of last array .

I'm wrong in logic but I still do not know where.

    
asked by anonymous 02.02.2015 / 22:26

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 that the result returned by the fetch_assoc method is a vector with values passed by reference .

    
03.02.2015 / 13:23