0

I have created a recursive function to get parent products of a product. I'm almost sure I'm nearly there, but need help snapping out of recursion.

The result I am looking for is something like:

Product 1 (id:1, parent:none)
--- Product 2 (id:2, parent:1)
--- --- Product 3 (id:3, parent:2)
--- --- Product 4 (id:4, parent:2)
--- Product 5 (id:5, parent:1)
--- --- Product 6 (id:6, parent:5)
--- --- Product 7 (id:7, parent:5)

My function is as follows:

function get_parents ($pid, $found = array()) {
    array_push ($found, $pid);

    $sql = "SELECT * FROM products WHERE child_id = '$pid'";
    $result = mysql_query($sql) or die ($sql);

    if(mysql_num_rows($result)){
        while($row = mysql_fetch_assoc($result)){
            $found[] = get_parents($row['pid'], $found);
        }
    }
    return $found;
}

I call it using a simple:

$parents = get_parents($pid);

The problem I am having is that when I run it, it creates an infinite loop, which doesn't break.

I think whats happening is there are 8 results from the first query and then it does another 8 queries and another 8 queries and so on until php crashes.

I have attached the result of my array in text file.

Any help would be seriously appreciated :-)

Edited by hawx: typo

Attachments
Array
(
    [0] => 2
    [1] => Array
        (
            [0] => 2
            [1] => 1
        )

    [2] => Array
        (
            [0] => 2
            [1] => Array
                (
                    [0] => 2
                    [1] => 1
                )

            [2] => 1
        )

    [3] => Array
        (
            [0] => 2
            [1] => Array
                (
                    [0] => 2
                    [1] => 1
                )

            [2] => Array
                (
                    [0] => 2
                    [1] => Array
                        (
                            [0] => 2
                            [1] => 1
                        )

                    [2] => 1
                )

            [3] => 1
        )

    [4] => Array
        (
            [0] => 2
            [1] => Array
                (
                    [0] => 2
                    [1] => 1
                )

            [2] => Array
                (
                    [0] => 2
                    [1] => Array
                        (
                            [0] => 2
                            [1] => 1
                        )

                    [2] => 1
                )

            [3] => Array
                (
                    [0] => 2
                    [1] => Array
                        (
                            [0] => 2
                            [1] => 1
                        )

                    [2] => Array
                        (
                            [0] => 2
                            [1] => Array
                                (
                                    [0] => 2
                                    [1] => 1
                                )

                            [2] => 1
                        )

                    [3] => 1
                )

            [4] => 1
        )

    [5] => Array
        (
            [0] => 2
            [1] => Array
                (
                    [0] => 2
                    [1] => 1
                )

            [2] => Array
                (
                    [0] => 2
                    [1] => Array
                        (
                            [0] => 2
                            [1] => 1
                        )

                    [2] => 1
                )

            [3] => Array
                (
                    [0] => 2
                    [1] => Array
                        (
                            [0] => 2
                            [1] => 1
                        )

                    [2] => Array
                        (
                            [0] => 2
                            [1] => Array
                                (
                                    [0] => 2
                                    [1] => 1
                                )

                            [2] => 1
                        )

                    [3] => 1
                )

            [4] => Array
                (
                    [0] => 2
                    [1] => Array
                        (
                            [0] => 2
                            [1] => 1
                        )

                    [2] => Array
                        (
                            [0] => 2
                            [1] => Array
                                (
                                    [0] => 2
                                    [1] => 1
                                )

                            [2] => 1
                        )

                    [3] => Array
                        (
                            [0] => 2
                            [1] => Array
                                (
                                    [0] => 2
                                    [1] => 1
                                )

                            [2] => Array
                                (
                                    [0] => 2
                                    [1] => Array
                                        (
                                            [0] => 2
                                            [1] => 1
                                        )

                                    [2] => 1
                                )

                            [3] => 1
                        )

                    [4] => 1
                )

            [5] => 1
        )

    [6] => Array
        (
            [0] => 2
            [1] => Array
                (
                    [0] => 2
                    [1] => 1
                )

            [2] => Array
                (
                    [0] => 2
                    [1] => Array
                        (
                            [0] => 2
                            [1] => 1
                        )

                    [2] => 1
                )

            [3] => Array
                (
                    [0] => 2
                    [1] => Array
                        (
                            [0] => 2
                            [1] => 1
                        )

                    [2] => Array
                        (
                            [0] => 2
                            [1] => Array
                                (
                                    [0] => 2
                                    [1] => 1
                                )

                            [2] => 1
                        )

                    [3] => 1
                )

            [4] => Array
                (
                    [0] => 2
                    [1] => Array
                        (
                            [0] => 2
                            [1] => 1
                        )

                    [2] => Array
                        (
                            [0] => 2
                            [1] => Array
                                (
                                    [0] => 2
                                    [1] => 1
                                )

                            [2] => 1
                        )

                    [3] => Array
                        (
                            [0] => 2
                            [1] => Array
                                (
                                    [0] => 2
                                    [1] => 1
                                )

                            [2] => Array
                                (
                                    [0] => 2
                                    [1] => Array
                                        (
                                            [0] => 2
                                            [1] => 1
                                        )

                                    [2] => 1
                                )

                            [3] => 1
                        )

                    [4] => 1
                )

            [5] => Array
                (
                    [0] => 2
                    [1] => Array
                        (
                            [0] => 2
                            [1] => 1
                        )

                    [2] => Array
                        (
                            [0] => 2
                            [1] => Array
                                (
                                    [0] => 2
                                    [1] => 1
                                )

                            [2] => 1
                        )

                    [3] => Array
                        (
                            [0] => 2
                            [1] => Array
                                (
                                    [0] => 2
                                    [1] => 1
                                )

                            [2] => Array
                                (
                                    [0] => 2
                                    [1] => Array
                                        (
                                            [0] => 2
                                            [1] => 1
                                        )

                                    [2] => 1
                                )

                            [3] => 1
                        )

                    [4] => Array
                        (
                            [0] => 2
                            [1] => Array
                                (
                                    [0] => 2
                                    [1] => 1
                                )

                            [2] => Array
                                (
                                    [0] => 2
                                    [1] => Array
                                        (
                                            [0] => 2
                                            [1] => 1
                                        )

                                    [2] => 1
                                )

                            [3] => Array
                                (
                                    [0] => 2
                                    [1] => Array
                                        (
                                            [0] => 2
                                            [1] => 1
                                        )

                                    [2] => Array
                                        (
                                            [0] => 2
                                            [1] => Array
                                                (
                                                    [0] => 2
                                                    [1] => 1
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Morta
0

Hiiii,check that code:

function get_parents ($pid, $found = array()) {
    array_push ($found, $pid);

    $sql = "SELECT * FROM products WHERE child_id = '$pid'";
    $result = mysql_query($sql) or die ($sql);

    if(mysql_num_rows($result)>0){
        while($row = mysql_fetch_array($result)){
            $found[] = get_parents($row['pid'], $found);
        }
    }
    return $found;
}

%^^%Murtada%^^%

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.