3
Hi, I created a menu that searches the information in the database. I have a table with the categories and another one with the subcategories, up to there beauty, my difficulty is to make each subcategory is listed within their respective category (example below)...
But I’m not succeeding, it is as an example below...
Below I am attaching the code used for the menu...
<?php
include "../conexao.php";
$codigo = $_POST['codigo'];
$nome = $_POST['nome'];
$query = mysql_query("SELECT * FROM categoria order by nome")or die(mysql_error());
while($res = mysql_fetch_array($query)){
?>
<ul>
<li><a href="#"> <?php echo $res['nome'];?> </a>
<?php
}
?>
<?php
include "../conexao.php";
$nome = $_POST['nome'];
$query = mysql_query("SELECT * FROM sub_categoria order by nome")or die(mysql_error());
while($res = mysql_fetch_array($query)){
?>
<ul>
<li><a href="prod_index_subcategoria.php?codsubcategoria=<?php echo $res['nome'];?>"><?php echo $res['nome'];?></a></li>
</ul>
</li>
<?php
}
?>
</ul>
Below the structures of the tables used... Categories:
If friends can help me by telling me where I’m going wrong, or even what to do to get to my goal, which are the Subcategories being listed within their respective Categories.
Waiting for the help of friends, and thanking everyone for their attention to my problem.
I’m editing the Post with the new line of code below with Inner Join...
<?php
include "../conexao.php";
$codigo = $_POST['codigo'];
$nome_cat = $_POST['nome_cat'];
$nome = $_POST['nome'];
$query = mysql_query("SELECT * FROM categoria INNER JOIN sub_categoria ON categoria.nome_cat = sub_categoria.nome_cat")or die(mysql_error());
while($res = mysql_fetch_array($query)){
?>
<ul>
<li><a href="#"> <?php echo $res['nome_cat'];?> </a>
<ul>
<li><a href="prod_index_subcategoria.php?codsubcategoria=<?php echo $res['nome'];?>"><?php echo $res['nome'];?></a></li>
</ul>
</li>
<?php
}
?>
</ul>
And I am also attaching the images of the structures of Categories and Sub-Categories that I made a mistake earlier and published the category in duplicity.
Category:
Sub-category:
could post the table structure category and subcategories?
– Hebert Lima
OK, Hebert de Lima, I just edited the Post with the structures used. I hope it helps. Hugs.
– Murilo Cabral