Relate category tables with subcategories

0

I want to relate two tables. For example, when you click on an item, all items related to it appear

?>
        <?php
    include "conexao.php";


$sql = "SELECT *FROM categorias ";


$resultado = pg_query($conexao, $sql);
$linhas = pg_num_rows($resultado);


      $nome_categoria = $linhas["nome_categoria"];
            $resultado.= "<option>".$nome_categoria."</option>";
    $categoria = "<select class='select_roupas'>". $resultado."</select>";

for($i = 0; $i < $linhas; $i++)
{

$registro = pg_fetch_array($resultado, $i);

$sql1 = "SELECT * FROM subcategorias ";
$resultado1= pg_query($conexao,$sql1);
$registro1=pg_fetch_array ($resultado1)  ;  

      $nome_subcategoria = $linhas["nome_subcategoria"];
            $resultado1.= "<option>".$nome_subcategoria."</option>";
    $subcategoria = "<select class='select_roupas'>". $resultado1."</select>";
}


?>
    
asked by anonymous 03.11.2014 / 23:41

2 answers

1

in the table subcategories vc needs to store the id of the categories table, thus:

SELECT * FROM subcategorias WHERE categoria_id = $id_categoria;

This way will get all the results of the subcategories table where the category is the "selected"

    
03.11.2014 / 23:55
0

I recommend that you search the web for Nested Combos , Dynamic Combos .

You will need PHP + Javascript to resolve this type of situation.

  

See this example and adapt to its use:

     

link

    
23.08.2016 / 15:16