0

When browsing the binary tree I want to return an array of recursive functions. In particular when an element in binary tree reaching conditions (if statement) is inserted into the array. After all, the element returns an array of all. My code not work !??

function tree_view($clear_static = false,$conn,$index)
{
    static $count = 0;
    if ($clear_static) {
        $count = 0;
    }
    $q=mysql_query("SELECT user_id FROM thanhvien WHERE gioithieu='".$index."'",$conn);

    while($arr=mysql_fetch_assoc($q)) 
    { 
        if (truongban($conn,$arr["user_id"],1)==true){
            $mang[$count]=$arr["user_id"];
            $count++;
        }
        tree_view(false,$conn,$arr["user_id"]); 
    } 
    return $mang;
} 
$mang1=tree_view (true,$conn,anloc);
print_r($mang1);
3
  • Do you want to really return 'array of recursive functions'? Not just simple data? Commented Feb 25, 2012 at 0:30
  • yes ! im really return my function to array ! Plz help me Commented Feb 25, 2012 at 11:10
  • You want to return your function AM array, not an array of functions. Commented Feb 25, 2012 at 14:28

2 Answers 2

1

Well, the problem I see is that you are not doing anything with the array returned from the recursive call, here in this line:

tree_view(false,$conn,$arr["user_id"]);

I would recommend including both the array and the count on the method parameters (using a static variable is not recommended). So it would be like this:

function tree_view($conn, $index, $mang, &$count)
{
    $q=mysql_query("SELECT user_id FROM thanhvien WHERE gioithieu='".$index."'",$conn);

    while($arr=mysql_fetch_assoc($q)) 
    { 
        if (truongban($conn,$arr["user_id"],1)==true){
            $mang[$count]=$arr["user_id"];
            $count++;
        }
        tree_view($conn,$arr["user_id"], $mang, $count); 
    } 
    return $mang;
} 

And you would invoke your method like this:

$mang1[0] = "";
$count = 0;
$mang1 = tree_view ($conn, anloc, $mang1, $count); print_r($mang1);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you!me was trying to how to your, but it only display Array ( [0] => thidotn1 ). If exactly does it must be displayed 10 element
Oops, sorry, I forgot to pass $count by reference. I edited the code, it should work now.
when i edit your code. It display " Array ( [0] => [9] => thidotn1 ) " what prolem with it ! Thank you for help
0

First $mang isn't initialized, but likely should be:

  // Always initialize variables - good style
  $mang = array();

Instead of passing this ugly $count variable, just append newly discovered data:

  // Will append the right side value as a new last element to $mang
  $mang[] = $arr["user_id"];

Next, you need to pass the variable $anloc:

  $mang1=tree_view ( true, $conn, $anloc );

This version might work better:

function tree_view($conn, $index, $mang ) {

    $q=mysql_query(  'SELECT user_id '
                   . '  FROM thanhvien '
                   . ' WHERE gioithieu = "' . mysql_real_escape_string ( $index ) . '"', 
                   $conn 
                  );

    while( $arr = mysql_fetch_assoc( $q ) ) {

        // better compare type-safe
        if ( true === truongban( $conn, $arr["user_id"], 1 ) ){

            // append found element
            $mang[ ] = $arr["user_id"];

        }

        tree_view( $conn, $arr["user_id"], $mang ); 

    } 

    return $mang;

} // tree_view

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.