لینک ببین.
این کد یه نوع سادش که با space درستش کردم
function underScore($counter)
{
	$return = '';
	for ($i = 0; $i < $counter; $i++)
	{
		$return .= ' ';
	}
	return $return;
}
function categoryList($parent, $array, $level = 0)
{
    foreach ($array as $value)
    {
        if ($value['parent'] == $parent)
        {
        	if ($value['parent'])
        	{
        		$level = ($value['parent'] == $level ? $level : $level + 1);
        	}
        	else
        	{
        		$level = 0;
        	}
            echo "<li>". underScore($level * 4) . $value['name'] ."</li>";
            categoryList($value['id'], $array, $level);
            $level--;
        }
    }
}
mysql_connect('localhost', 'root', '');
mysql_select_db('test');
mysql_query('set names utf8');
$result = mysql_query("select * from cat");
while ($row = mysql_fetch_assoc($result))
{
    $array[$row['id']] = array('id'=>$row['id'], 'name'=>$row['name'], 'parent'=>$row['parent']);
}
echo "<ul style='list-style: none;'>";
categoryList(0, $array);
echo "</ul>";