I created the following php code and wondering if there is a better way to make my coding better and for unlimited subcategories?
<?php
$queryrootc = mysql_query("SELECT * FROM categories ORDER BY order_by ASC");
echo "<ul id=\"root\" class=\"menu\">";
$r=100;
while($querymc = mysql_fetch_array($queryrootc)){
if ($querymc['parentid'] == 0){
echo "<li><a href=\"javascript:void(0);\" childid=\"c_".$r."\" class=\"cat_close category\"> </a>
<a href=\"javascript:void(0);\">".$querymc['category']."</a>";
//Get all results that have the parent id of the root
$querychksubs = mysql_query("SELECT * FROM categories WHERE parentid = '".$querymc['categoryid']."' ORDER BY order_by ASC");
$querycntchksubc = mysql_num_rows($querychksubs);
if ($querycntchksubc){
echo "<ul id=\"c_".$r."\">";
$g = 1;
while($querychksubsresults = mysql_fetch_array($querychksubs)){
$querysubs2 = mysql_query("SELECT * FROM categories WHERE parentid = '".$querychksubsresults['categoryid']."' ORDER BY order_by ASC");
$querycntsubs = mysql_num_rows($querysubs2);
if ($querycntsubs){
echo "<li><a href=\"javascript:void(0);\" childid=\"c_".$g."\" class=\"cat_close category\"> </a>
<a href=\"javascript:void(0);\">".$querychksubsresults['category']."</a><ul id=\"c_".$g."\">";
while($getsubs = mysql_fetch_array($querysubs2)){
echo "<li>".$getsubs['category']."</li>";
}
echo "</ul></li>";
} else {
echo "<li>".$querychksubsresults['category']."</li>";
}
$g++;
}
echo "</ul></li>";
} else {
echo "</li>";
}
}
$r++;
}
echo "</ul>";
?>
Thanks for looking and any input 🙂