function submenu($parentid = 0)
{
$sql = mysql_query("SELECT * FROM test WHERE refid=" . $parentid);
echo '<ul>';
while($row = mysql_fetch_assoc($sql))
{
if($row['refid'] == 0)
{
echo '<li>';
echo $row['name'];
submenu($row['id']);
echo '</li>';
}
else
{
echo '<li>';
echo "<a href=\"{$row['userdefined']}\">{$row['name']}</a>";
submenu($row['id']);
echo '</li>';
}
}
echo '</ul>';
}
submenu();
Though I recommend you moving away from these mysql_functions and converting your code to PDO as those functions are deprecated since 5.5.0 and are completely removed in PHP 7.0.0 (according to the PHP manual)