0

I have a recursive function that returns a value.

    public function getparent ($user)
    {
        $referrer=''; $pos=0;$this->setFieldNames();
        $result=mysql_query("select * FROM ". $this->dbtablename ." WHERE ".$this->id_field . " = '$user'", $this->dbConnectionID);
        while($row=mysql_fetch_array($result, MYSQL_ASSOC))
        {
            $referrer=$row['referrer'];
            $export=array();
            foreach($this->tablefields as $val){
                $export[$val] = $row[$val];
            }
        }
        if($referrer != "0" && $referrer != "")
        {
            $result2=mysql_query("select * from " . $this->dbtablename . " where " . $this->id_field . " ='$referrer'", $this->dbConnectionID);
            while($row2=mysql_fetch_array($result2, MYSQL_ASSOC))
            {
                $export=array();
                foreach($this->tablefields as $val){
                    $export[$val] = $row2[$val];
                }
            }
        }

        $result2=mysql_query("select * from " . $this->dbtablename . " where " . $this->parent_id_field . " ='$referrer' order by id asc", $this->dbConnectionID);
        while($row2=mysql_fetch_array($result2, MYSQL_ASSOC))
        {
            $usernames[]=$row2['username'];
        }
        $pos=array_search($user, $usernames);
        if( $referrer == '0' || $pos >=1 )
        {echo 'xxxxxxx';
            return $export;
        }
        else
        {
            $this->getparent($referrer);
        }
    }

It seems that when the $referrer is zero it doesnt return any value, yet it echoes 'xxxxxxx'. What can the problem be!?

2 Answers 2

2

There's a problem with:

    while($row=mysql_fetch_array($result, MYSQL_ASSOC))
    {
        $referrer=$row['referrer'];
        $export=array();
        foreach($this->tablefields as $val){
            $export[$val] = $row[$val];
        }
    }

You're replacing your export array on every iteration - is that your intent? The data is being thrown away for all but the last row. Then you do it again in this block:

    if($referrer != "0" && $referrer != "")
    {
        $result2=mysql_query("select * from " . $this->dbtablename . " where " . $this->id_field . " ='$referrer'", $this->dbConnectionID);
        while($row2=mysql_fetch_array($result2, MYSQL_ASSOC))
        {
            $export=array();
            foreach($this->tablefields as $val){
                $export[$val] = $row2[$val];
            }
        }
    }

Where you're (a) only getting the last row of data and (b) overriding the data from the first block (perhaps that latter is desired).

And finally if you ever hit this block (referrer isn't zero the first time through your code):

    else
    {
        $this->getparent($referrer);
    }

You never reutrn a value from the recursive call, it should be:

    else
    {
        return $this->getparent($referrer);
    }

Which I suspect is the actual issue. But I've got to question whether this recursive approach is best, rather than an iterative one.

Sign up to request clarification or add additional context in comments.

1 Comment

The $export array i overwrite is actually intentional, though each while loop returns only one row. Your last suggestion did the magic, but i was thinking its nt compulsory it must return a value at that point. Pls kindly explain the iterative approach to me.
0

Try to declare $export before the while loop

1 Comment

i just did that, i removed the declaration from the while loops and put on top, yet it didnt work, i even tried to return a static string yet it didnt return anything.

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.