1

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)

0

1 Answer 1

1

no, it shouldn't be that, because when you do this:

echo "<li>".$r['title'].m_gen($r['id'])."</li>\n";

first it executes the m_gen function (and then again recursively if there are more levels), at the last level it echoes (because there is an echo in the recursive function), and then comes back the "parent", the function that called it, that one echoes, and so on.

you should do this:

echo "<li>".$r['title'];
m_gen($r['id']);
echo "</li>\n";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.