My function:
function m_gen($n) {
$q = mysql_query("select * from nume where parentid = '$n'");
if (mysql_num_rows($q) > 0) {
echo "<ul>\n";
while ($r = mysql_fetch_assoc($q)) {
echo "<li>".$r['title'].m_gen($r['id'])."</li>\n";
}
echo "</ul>\n";
} else {
echo "";
}
}
calling the function like this:
m_gen(0);
generates the following result:

When I call the function, the HTML is not written correctly.
The result should be:
<li>main3
<ul>
<li>sub main3 > main1</li>
<li>sub main3 > main2</li>
<li>sub main3 > main3</li>
<li>sub main3 > main4</li>
<li>sub main3 > main5</li>
<li>sub main3 > main6</li>
</ul>
</li>
but, what happens is like this:
<ul>
<li>sub main3 > main1</li>
<li>sub main3 > main2</li>
<li>sub main3 > main3</li>
<li>sub main3 > main4</li>
<li>sub main3 > main5</li>
<li>sub main3 > main6</li>
</ul>
<li>main3</li>
What is wrong with the function?
I've tried to find a solution, but I did not get the results
MySQL source (pass:nume)